generated from RTradeLtd/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
52 lines (43 loc) · 994 Bytes
/
util.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
package discovery
import (
"context"
"time"
"github.com/RTradeLtd/libp2px-core/peer"
)
// FindPeers is a utility function that synchronously collects peers from a Discoverer.
func FindPeers(ctx context.Context, d Discoverer, ns string, opts ...Option) ([]peer.AddrInfo, error) {
var res []peer.AddrInfo
ch, err := d.FindPeers(ctx, ns, opts...)
if err != nil {
return nil, err
}
for pi := range ch {
res = append(res, pi)
}
return res, nil
}
// Advertise is a utility function that persistently advertises a service through an Advertiser.
func Advertise(ctx context.Context, a Advertiser, ns string, opts ...Option) {
go func() {
for {
ttl, err := a.Advertise(ctx, ns, opts...)
if err != nil {
if ctx.Err() != nil {
return
}
select {
case <-time.After(2 * time.Minute):
continue
case <-ctx.Done():
return
}
}
wait := 7 * ttl / 8
select {
case <-time.After(wait):
case <-ctx.Done():
return
}
}
}()
}