-
Notifications
You must be signed in to change notification settings - Fork 376
/
watchdogs_discoverer.go
243 lines (197 loc) · 5.87 KB
/
watchdogs_discoverer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package tinder
import (
"context"
"reflect"
"sync"
"time"
p2p_discovery "github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/host"
p2p_peer "github.com/libp2p/go-libp2p-core/peer"
discovery "github.com/libp2p/go-libp2p-discovery"
"go.uber.org/zap"
"berty.tech/berty/v2/go/internal/logutil"
)
type findPeersTimer struct {
Ctx context.Context
T *time.Timer
}
type BackoffOpts struct {
StratFactory discovery.BackoffFactory
DiscoveryOptions []discovery.BackoffDiscoveryOption
}
type watchdogsDiscoverer struct {
rootctx context.Context
logger *zap.Logger
disc p2p_discovery.Discoverer
resetInterval time.Duration
findpeers map[string]*findPeersTimer
mufindpeers sync.Mutex
}
func newWatchdogsDiscoverer(ctx context.Context, l *zap.Logger, h host.Host, resetInterval time.Duration, backoff *BackoffOpts, drivers []*Driver) (*watchdogsDiscoverer, error) {
mdisc := newMultiDriverDiscoverer(l, h, drivers...)
// compose discovery for backoff
var disc p2p_discovery.Discovery = &DriverDiscovery{
Advertiser: NoopDiscovery,
Discoverer: mdisc,
}
// add backoff strategy if provided
if backoff != nil && backoff.StratFactory != nil {
// wrap backoff/cache discovery
var err error
if disc, err = discovery.NewBackoffDiscovery(disc, backoff.StratFactory, backoff.DiscoveryOptions...); err != nil {
return nil, err
}
}
return &watchdogsDiscoverer{
rootctx: ctx,
resetInterval: resetInterval,
findpeers: make(map[string]*findPeersTimer),
logger: l,
disc: disc,
}, nil
}
func (w *watchdogsDiscoverer) FindPeers(_ context.Context, ns string, opts ...p2p_discovery.Option) (<-chan p2p_peer.AddrInfo, error) {
// override context with our context
ctx := w.rootctx
w.mufindpeers.Lock()
defer w.mufindpeers.Unlock()
if ft, ok := w.findpeers[ns]; ok {
// already running find peers
if !ft.T.Stop() {
<-ft.T.C
}
ft.T.Reset(w.resetInterval)
// return alreay existing chan
return w.disc.FindPeers(ft.Ctx, ns, opts...)
}
timer := time.Now()
ctx, cancel := context.WithCancel(ctx)
w.logger.Debug("watchdogs looking for peers", logutil.PrivateString("ns", ns))
c, err := w.disc.FindPeers(ctx, ns, opts...)
if err != nil {
cancel()
return nil, err
}
t := time.AfterFunc(w.resetInterval, func() {
cancel()
w.logger.Debug("findpeers expired",
logutil.PrivateString("ns", ns),
zap.Duration("duration", time.Since(timer)),
)
w.mufindpeers.Lock()
delete(w.findpeers, ns)
w.mufindpeers.Unlock()
})
w.findpeers[ns] = &findPeersTimer{ctx, t}
return c, nil
}
type multiDriverDiscoverer struct {
logger *zap.Logger
drivers []*Driver
host host.Host
}
type driverChan struct {
driver *Driver
cc <-chan p2p_peer.AddrInfo
topic string
}
func newMultiDriverDiscoverer(logger *zap.Logger, h host.Host, drivers ...*Driver) *multiDriverDiscoverer {
return &multiDriverDiscoverer{
logger: logger,
drivers: drivers,
host: h,
}
}
func (s *multiDriverDiscoverer) ProtectPeer(id p2p_peer.ID) {
s.host.ConnManager().Protect(id, TinderPeer)
}
func (s *multiDriverDiscoverer) FindPeers(ctx context.Context, ns string, opts ...p2p_discovery.Option) (<-chan p2p_peer.AddrInfo, error) {
s.logger.Debug("find peers started", logutil.PrivateString("key", ns), zap.Int("drivers", len(s.drivers)))
cc := make(chan p2p_peer.AddrInfo)
if len(s.drivers) == 0 {
close(cc)
return cc, nil
}
ctx, cancel := context.WithCancel(ctx)
cdrivers := []*driverChan{}
for _, driver := range s.drivers {
ch, err := driver.FindPeers(ctx, ns, opts...)
if err != nil {
s.logger.Warn("failed to run find peers",
zap.String("driver", driver.Name),
logutil.PrivateString("ns", ns),
zap.Error(err))
continue
}
s.logger.Debug("findpeer for driver started", zap.String("driver", driver.Name), logutil.PrivateString("ns", ns))
cdrivers = append(cdrivers, &driverChan{
cc: ch,
driver: driver,
topic: ns,
})
}
go func() {
defer cancel()
defer close(cc)
// @TODO(gfanton): use optimized method for few drivers
err := s.selectFindPeers(ctx, cc, cdrivers)
s.logger.Debug("find peers done", logutil.PrivateString("ns", ns), zap.Error(err))
}()
return cc, nil
}
func (s *multiDriverDiscoverer) selectFindPeers(ctx context.Context, out chan<- p2p_peer.AddrInfo, in []*driverChan) error {
nsels := len(in) + 1 // number of drivers + context
selDone := nsels - 1 // context index
selCases := make([]reflect.SelectCase, nsels)
for i, driver := range in {
selCases[i] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(driver.cc),
}
}
selCases[selDone] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ctx.Done()),
}
n := len(selCases) - 1 // ignore context selector
for n > 0 {
sel, value, ok := reflect.Select(selCases)
if sel == selDone {
return ctx.Err()
}
if !ok {
// The chosen channel has been closed, so zero out the channel to disable the case
selCases[sel].Chan = reflect.ValueOf(nil)
n--
continue
}
driver := in[sel].driver
// we can safly get our peer
peer := value.Interface().(p2p_peer.AddrInfo)
// skip self
if peer.ID == s.host.ID() {
continue
}
// @gfanton(TODO): filter addrs by drivers should be made
// directly on the advertiser side, but since most drivers take
// addrs directly from host, it's easier (for now) to filter it
// here
if addrs := driver.AddrsFactory(peer.Addrs); len(addrs) > 0 {
filterpeer := p2p_peer.AddrInfo{
ID: peer.ID,
Addrs: addrs,
}
topic := in[sel].topic
// protect this peer to avoid to be pruned
s.ProtectPeer(peer.ID)
s.logger.Debug("found a peer",
zap.String("driver", driver.Name),
logutil.PrivateString("peer", filterpeer.ID.String()),
logutil.PrivateString("ns", topic),
zap.Any("addrs", filterpeer.Addrs))
// forward the peer
out <- filterpeer
}
}
return nil
}