-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
55 lines (46 loc) · 1.37 KB
/
api.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
package autopeering
import (
"context"
"time"
"github.com/brendoncarroll/go-p2p/s/multiswarm"
"github.com/brendoncarroll/stdctx/logctx"
"github.com/inet256/inet256/pkg/inet256"
"github.com/inet256/inet256/pkg/peers"
)
type TransportAddr = multiswarm.Addr
// AddrSource returns a list of addresses suitable for advertisement.
type AddrSource = func() []TransportAddr
type Params struct {
// Outbound
LocalAddr inet256.Addr
AddrSource AddrSource
// Inbound
PeerStore peers.Store[TransportAddr]
ParseAddr func([]byte) (TransportAddr, error)
}
// Service manages the peers in a PeerStore, adding to and removing from them automatically
// as peers are discovered and lost.
type Service interface {
// Run should run until the context is cancelled, calling params.AddrSource to
// get the local addresses, and storing discovered addresses in params.PeerStore
Run(ctx context.Context, params Params) error
}
func RunForever(ctx context.Context, srv Service, params Params) {
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
for {
logctx.Infoln(ctx, "starting autopeering service")
if err := srv.Run(ctx, params); err != nil {
if err == context.Canceled {
logctx.Infoln(ctx, "stopping autopeering service")
return
}
logctx.Errorf(ctx, "error in autopeering service %v", err)
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}