Skip to content

Commit 25950f3

Browse files
committed
refactor: AutoRelayFeeder with exp. backoff
It starts at feeding peers ever 15s, then backs off each time until it is done once an hour Should be acceptable until we have smarter mechanism in go-lib2p 0.20
1 parent 195d394 commit 25950f3

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

core/node/libp2p/routing.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package libp2p
22

33
import (
44
"context"
5-
"fmt"
65
"sort"
76
"time"
87

@@ -20,6 +19,7 @@ import (
2019
record "github.com/libp2p/go-libp2p-record"
2120
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
2221

22+
"github.com/cenkalti/backoff/v4"
2323
"go.uber.org/fx"
2424
)
2525

@@ -189,22 +189,28 @@ func AutoRelayFeeder(lc fx.Lifecycle, h host.Host, peerChan chan peer.AddrInfo,
189189
go func() {
190190
defer close(done)
191191

192-
t := time.NewTicker(time.Minute / 3) // TODO: this is way too frequent. Should probably use some kind of backoff
192+
// Feed peers more often right after the bootstrap, then backoff
193+
bo := backoff.NewExponentialBackOff()
194+
bo.InitialInterval = 15 * time.Second
195+
bo.Multiplier = 3
196+
bo.MaxInterval = 1 * time.Hour
197+
bo.MaxElapsedTime = 0 // never stop
198+
t := backoff.NewTicker(bo)
193199
defer t.Stop()
194200
for {
195201
select {
196202
case <-t.C:
197203
case <-ctx.Done():
198204
return
199205
}
200-
// TODO: refactor AutoRelayFeeder, for now we skip it if dht missing to fix sharness panics
201206
if dht == nil {
202-
fmt.Println("AutoRelayFeeder: noop due to missing dht.WAN")
207+
/* noop due to missing dht.WAN. happens in some unit tests,
208+
not worth fixing as we will refactor this after go-libp2p 0.20 */
203209
continue
204210
}
205211
closestPeers, err := dht.WAN.GetClosestPeers(ctx, h.ID().String())
206212
if err != nil {
207-
fmt.Println(err)
213+
// usually 'failed to find any peer in table', no no-op
208214
continue
209215
}
210216
for _, p := range closestPeers {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require (
44
bazil.org/fuse v0.0.0-20200117225306-7b5117fecadc
55
contrib.go.opencensus.io/exporter/prometheus v0.4.0
66
github.com/blang/semver/v4 v4.0.0
7+
github.com/cenkalti/backoff/v4 v4.1.2
78
github.com/ceramicnetwork/go-dag-jose v0.1.0
89
github.com/cespare/xxhash v1.1.0
910
github.com/cheggaaa/pb v1.0.29
@@ -135,7 +136,6 @@ require (
135136
github.com/beorn7/perks v1.0.1 // indirect
136137
github.com/btcsuite/btcd v0.22.0-beta // indirect
137138
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
138-
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
139139
github.com/cespare/xxhash/v2 v2.1.2 // indirect
140140
github.com/cheekybits/genny v1.0.0 // indirect
141141
github.com/containerd/cgroups v1.0.3 // indirect

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,6 @@ github.com/ipfs/go-metrics-prometheus v0.0.2 h1:9i2iljLg12S78OhC6UAiXi176xvQGiZa
607607
github.com/ipfs/go-metrics-prometheus v0.0.2/go.mod h1:ELLU99AQQNi+zX6GCGm2lAgnzdSH3u5UVlCdqSXnEks=
608608
github.com/ipfs/go-mfs v0.2.1 h1:5jz8+ukAg/z6jTkollzxGzhkl3yxm022Za9f2nL5ab8=
609609
github.com/ipfs/go-mfs v0.2.1/go.mod h1:Woj80iuw4ajDnIP6+seRaoHpPsc9hmL0pk/nDNDWP88=
610-
github.com/ipfs/go-namesys v0.4.1-0.20220426174228-2e37afbc2bf2 h1:Vr44R5BWU1iGDnloo66+O9OKJcCCiZB5ZVOqdWaHVtw=
611-
github.com/ipfs/go-namesys v0.4.1-0.20220426174228-2e37afbc2bf2/go.mod h1:KLFhk7IN1TB+WqtB6hxuCQFfBi+4b6nsaKgRuJjYUtA=
612610
github.com/ipfs/go-namesys v0.4.1-0.20220427151138-605965e675aa h1:PheaiRiAxx2A6OyOmB9fsuT/MJ1QOR31ipbyNOJgND8=
613611
github.com/ipfs/go-namesys v0.4.1-0.20220427151138-605965e675aa/go.mod h1:KLFhk7IN1TB+WqtB6hxuCQFfBi+4b6nsaKgRuJjYUtA=
614612
github.com/ipfs/go-path v0.0.7/go.mod h1:6KTKmeRnBXgqrTvzFrPV3CamxcgvXX/4z79tfAd2Sno=

0 commit comments

Comments
 (0)