-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_discovery.go
434 lines (384 loc) · 12.8 KB
/
protocol_discovery.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package discovery
import (
"context"
"errors"
"strings"
"sync"
"time"
ma "github.com/multiformats/go-multiaddr"
"github.com/rambollwong/rainbowbee/core/discovery"
"github.com/rambollwong/rainbowbee/core/handler"
"github.com/rambollwong/rainbowbee/core/host"
"github.com/rambollwong/rainbowbee/core/peer"
"github.com/rambollwong/rainbowbee/core/protocol"
"github.com/rambollwong/rainbowbee/core/safe"
"github.com/rambollwong/rainbowbee/service/discovery/pb"
"github.com/rambollwong/rainbowbee/util"
"github.com/rambollwong/rainbowcat/types"
catutil "github.com/rambollwong/rainbowcat/util"
"github.com/rambollwong/rainbowlog"
"google.golang.org/protobuf/proto"
)
const (
protocolDiscoveryPIDPrefix = "/protocol-discovery/v0.0.1/"
DefaultSearchInterval = time.Minute
DefaultSearchMaxPeerSize = 10
DefaultSearchAsksForNumberOfPeers = 3
)
var (
_ discovery.Discovery = (*ProtocolDiscovery)(nil)
ErrSearching = errors.New("already searching")
)
type ProtocolDiscoveryOption func(pd *ProtocolDiscovery)
// WithMaxSearchSize sets the maximum number of peers to search for.
func WithMaxSearchSize(max int) ProtocolDiscoveryOption {
return func(pd *ProtocolDiscovery) {
pd.maxSearchSize = max
}
}
// WithSearchAsksForNumberOfPeers sets the number of peers to ask for during a search.
func WithSearchAsksForNumberOfPeers(num int) ProtocolDiscoveryOption {
return func(pd *ProtocolDiscovery) {
pd.asksForPeers = num
}
}
// WithSearchTimeout sets the timeout for a search operation.
func WithSearchTimeout(timeout time.Duration) ProtocolDiscoveryOption {
return func(pd *ProtocolDiscovery) {
pd.searchTimeout = timeout
}
}
// WithSearchInterval sets the interval between consecutive search operations.
func WithSearchInterval(interval time.Duration) ProtocolDiscoveryOption {
return func(pd *ProtocolDiscovery) {
pd.searchInterval = interval
}
}
// ProtocolDiscovery is a struct that represents a protocol discovery mechanism.
// It is responsible for discovering peers supporting specific protocols and initiating search requests.
type ProtocolDiscovery struct {
h host.Host
svcS *types.Set[string] // Set of service names to be discovered
mu sync.RWMutex // Mutex for concurrent access to the searching map
searchingM map[string]chan []ma.Multiaddr // Map of service names to channels for receiving discovered addresses
maxSearchSize, asksForPeers int // Maximum search size and number of peers to ask for
searchTimeout, searchInterval time.Duration // Search timeout and interval between search requests
logger *rainbowlog.Logger // Logger for logging messages and errors
}
// NewProtocolDiscovery creates a new instance of ProtocolDiscovery.
func NewProtocolDiscovery(h host.Host, opts ...ProtocolDiscoveryOption) *ProtocolDiscovery {
pd := &ProtocolDiscovery{
h: h,
svcS: types.NewSet[string](),
mu: sync.RWMutex{},
searchingM: make(map[string]chan []ma.Multiaddr),
maxSearchSize: DefaultSearchMaxPeerSize,
asksForPeers: DefaultSearchAsksForNumberOfPeers,
searchTimeout: -1,
searchInterval: DefaultSearchInterval,
logger: h.Logger().SubLogger(rainbowlog.WithLabels("PROTOCOL-DISC")),
}
pd.applyOptions(opts...)
return pd
}
func (p *ProtocolDiscovery) Announce(ctx context.Context, serviceName string) error {
serviceName = strings.TrimSpace(serviceName)
// create protocol ID
protocolID := p.createProtocolIDWithServiceName(serviceName)
if !p.svcS.Exist(serviceName) {
// create protocol msg handler
h := p.createProtocolMsgHandler(serviceName, protocolID)
// register discovery protocol to host
err := p.h.RegisterMsgPayloadHandler(protocolID, h)
if err != nil {
return err
}
p.svcS.Put(serviceName)
}
// send announce msg to all peers who support the protocol
allPeers, err := p.h.PeerProtocols([]protocol.ID{protocolID})
if err != nil {
return err
}
if len(allPeers) == 0 {
return nil
}
localAddresses := p.h.LocalAddresses()
localAddresses = util.ExcludeUnspecifiedAndLoopBack(localAddresses)
announceMsg := &pb.DiscoveryMsg{
Type: pb.DiscoveryMsg_Announce,
PInfos: []*pb.PeerInfo{
{
Pid: p.h.ID().String(),
Addr: catutil.SliceTransformType(localAddresses, func(_ int, item ma.Multiaddr) string {
return util.PIDAndNetAddrToMultiAddr(p.h.ID(), item).String()
}),
},
},
}
bz, err := proto.Marshal(announceMsg)
if err != nil {
p.logger.Debug().Msg("failed to marshal discovery msg.").Err(err).Done()
return err
}
for _, rp := range allPeers {
rPID := rp.PID
safe.LoggerGo(p.logger, func() {
err := p.h.SendMsg(protocolID, rPID, bz)
if err != nil {
p.logger.Warn().
Msg("failed to send discovery msg.").
Str("type", pb.DiscoveryMsg_Announce.String()).
Err(err).Done()
}
})
}
return nil
}
func (p *ProtocolDiscovery) SearchPeers(ctx context.Context, serviceName string) (<-chan []ma.Multiaddr, error) {
serviceName = strings.TrimSpace(serviceName)
p.mu.RLock()
_, ok := p.searchingM[serviceName]
p.mu.RUnlock()
if ok {
return nil, ErrSearching
}
searchC := make(chan []ma.Multiaddr)
p.mu.Lock()
p.searchingM[serviceName] = searchC
p.mu.Unlock()
safe.LoggerGo(p.logger, func() {
p.searchTask(ctx, serviceName)
})
return searchC, nil
}
// createProtocolIDWithServiceName creates a protocol ID based on the service name.
func (p *ProtocolDiscovery) createProtocolIDWithServiceName(serviceName string) protocol.ID {
return protocol.ID(protocolDiscoveryPIDPrefix + serviceName)
}
// handleSearchReq handles the search request.
func (p *ProtocolDiscovery) handleSearchReq(senderPID peer.ID, msg *pb.DiscoveryMsg, protocol protocol.ID) {
p.logger.Debug().
Msg("search request.").
Str("searcher", senderPID.String()).
Str("protocol", protocol.String()).
Any("infos", msg.PInfos).
Done()
var (
knownPeers = types.NewSet[peer.ID]() // Create a set to store known peers.
searchSize = int(msg.Size) // Get the requested search size.
)
knownPeers.Put(senderPID) // Add the sender to the set of known peers.
for _, info := range msg.PInfos {
knownPeers.Put(peer.ID(info.Pid)) // Add each peer ID in the message to the set of known peers.
}
if searchSize > p.maxSearchSize {
searchSize = p.maxSearchSize // Limit the search size to the maximum allowed size.
}
peersFound := p.h.PeerStore().PeersSupportingAllProtocols(protocol) // Find peers supporting the specified protocol.
pInfoList := make([]*pb.PeerInfo, 0, len(peersFound)) // Create a list to store the peer information.
for _, pid := range peersFound {
if knownPeers.Exist(pid) {
continue // Skip known peers.
}
netAddresses := p.h.PeerStore().GetAddresses(pid) // Get the network addresses of the peer.
if len(netAddresses) == 0 {
continue // Skip peers with no network addresses.
}
pInfoList = append(pInfoList, &pb.PeerInfo{
Pid: pid.String(),
Addr: catutil.SliceTransformType(netAddresses, func(_ int, item ma.Multiaddr) string {
return item.String()
}),
})
if len(pInfoList) >= searchSize {
break // Stop adding peer information if the requested search size is reached.
}
}
resMsg := &pb.DiscoveryMsg{
Type: pb.DiscoveryMsg_SearchRes,
PInfos: pInfoList,
}
bz, err := proto.Marshal(resMsg) // Marshal the response message.
if err != nil {
p.logger.Error().Msg("failed to marshal discovery msg.").Err(err).Done()
return
}
err = p.h.SendMsg(protocol, senderPID, bz) // Send the response message.
if err != nil {
p.logger.Debug().
Msg("failed to send discovery search response msg.").
Str("remote-pid", senderPID.String()).
Err(err).
Done()
return
}
}
// handleSearchRes handles the search response.
func (p *ProtocolDiscovery) handleSearchRes(serviceName string, msg *pb.DiscoveryMsg) {
p.logger.Debug().Msg("search response.").Str("service", serviceName).Any("infos", msg.PInfos).Done()
if len(msg.PInfos) == 0 {
return // If no peer information is included in the message, return.
}
for _, pInfo := range msg.PInfos {
pid := peer.ID(pInfo.Pid)
if p.h.ConnectionManager().Connected(pid) || !p.h.ConnectionManager().Allowed(pid) || p.h.ID() == pid {
continue // Skip connected peers, disallowed peers, and self.
}
addrs := make([]ma.Multiaddr, 0, len(pInfo.Addr))
for _, s := range pInfo.Addr {
netAddr, err := ma.NewMultiaddr(s) // Parse the network address.
if err != nil {
p.logger.Debug().Msg("failed to parse addr to multiaddr.").Err(err).Done()
return
}
addrs = append(addrs, util.PIDAndNetAddrToMultiAddr(pid, netAddr)) // Convert the peer ID and network address to a multiaddress.
}
addrs = catutil.SliceUnionBy(func(_ int, item ma.Multiaddr) string {
return item.String()
}, addrs)
p.mu.RLock()
c, ok := p.searchingM[serviceName] // Get the channel associated with the service name.
if !ok {
p.mu.RUnlock()
return // If the channel does not exist, return.
}
c <- addrs // Send the discovered addresses to the channel.
p.mu.RUnlock()
}
}
// createProtocolMsgHandler creates a message payload handler for the given service name and protocol ID.
func (p *ProtocolDiscovery) createProtocolMsgHandler(
serviceName string,
protocolID protocol.ID,
) handler.MsgPayloadHandler {
return func(senderPID peer.ID, msgPayload []byte) {
// unmarshal payload
msg := &pb.DiscoveryMsg{}
err := proto.Unmarshal(msgPayload, msg)
if err != nil {
p.logger.Error().Msg("failed to unmarshal discovery msg.").Err(err).Done()
return
}
switch msg.Type {
case pb.DiscoveryMsg_Announce:
if len(msg.PInfos) == 0 {
p.logger.Debug().
Msg("empty peer infos.").Str("type", pb.DiscoveryMsg_Announce.String()).Done()
return
}
pid := peer.ID(msg.PInfos[0].Pid)
if p.h.PeerStore().GetFirstAddress(pid) != nil {
if err := p.h.PeerStore().AddProtocol(pid, protocolID); err != nil {
p.logger.Debug().
Msg("failed to add protocol id.").
Str("type", pb.DiscoveryMsg_Announce.String()).
Err(err).
Done()
return
}
}
case pb.DiscoveryMsg_SearchReq:
p.handleSearchReq(senderPID, msg, protocolID)
case pb.DiscoveryMsg_SearchRes:
p.handleSearchRes(serviceName, msg)
default:
p.logger.Warn().
Msg("unknown discovery msg type.").
Str("type", msg.Type.String()).
Done()
}
}
}
// sendSearchReqToOthers sends a search request message to other peers supporting the specified protocol.
func (p *ProtocolDiscovery) sendSearchReqToOthers(protocolID protocol.ID) {
// Find peers supporting the specified protocol
peersFound := p.h.PeerStore().PeersSupportingAllProtocols(protocolID)
if len(peersFound) == 0 {
return
}
p.logger.Debug().Msg("sending search request to others.").Any("others", peersFound).Done()
// Transform peer IDs to PeerInfo struct
pInfos := catutil.SliceTransformType(peersFound, func(_ int, item peer.ID) *pb.PeerInfo {
return &pb.PeerInfo{
Pid: item.String(),
Addr: nil,
}
})
// Create search request message
req := &pb.DiscoveryMsg{
Type: pb.DiscoveryMsg_SearchReq,
PInfos: pInfos,
Size: uint32(p.maxSearchSize),
}
// Marshal the message into bytes
bz, err := proto.Marshal(req)
if err != nil {
p.logger.Error().Msg("failed to marshal discovery msg.").Err(err).Done()
return
}
// Shuffle the list of peers
peersFound = catutil.SliceShuffle(peersFound)
// Send search request to selected peers
for i, pid := range peersFound {
if p.asksForPeers > 0 && i == p.asksForPeers {
break
}
err = p.h.SendMsg(protocolID, pid, bz)
if err != nil {
p.logger.Debug().
Msg("failed to send search request msg.").
Str("remote-pid", pid.String()).
Err(err).
Done()
return
}
}
}
// searchTask is a task that performs the search operation for a specific service.
// It continuously sends search requests to other peers at a specified interval.
func (p *ProtocolDiscovery) searchTask(ctx context.Context, serviceName string) {
defer func() {
p.mu.Lock()
defer p.mu.Unlock()
// Clean up the search task
close(p.searchingM[serviceName])
delete(p.searchingM, serviceName)
}()
// Create the protocol ID for the specified service name
protocolID := p.createProtocolIDWithServiceName(serviceName)
ticker := time.NewTicker(time.Second)
if p.searchTimeout > 0 {
for {
select {
case <-time.After(p.searchTimeout):
return
case <-ctx.Done():
return
case <-ticker.C:
ticker.Stop()
// Send search request to other peers
p.sendSearchReqToOthers(protocolID)
ticker.Reset(p.searchInterval)
}
}
} else {
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
ticker.Stop()
// Send search request to other peers
p.sendSearchReqToOthers(protocolID)
ticker.Reset(p.searchInterval)
}
}
}
}
// applyOptions applies the provided options to the ProtocolDiscovery object.
func (p *ProtocolDiscovery) applyOptions(opts ...ProtocolDiscoveryOption) {
for _, opt := range opts {
opt(p)
}
}