Skip to content

Commit

Permalink
refactor: AutoRelayFeeder with exp. backoff
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lidel committed Apr 27, 2022
1 parent 195d394 commit 25950f3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
16 changes: 11 additions & 5 deletions core/node/libp2p/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package libp2p

import (
"context"
"fmt"
"sort"
"time"

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

"github.com/cenkalti/backoff/v4"
"go.uber.org/fx"
)

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

t := time.NewTicker(time.Minute / 3) // TODO: this is way too frequent. Should probably use some kind of backoff
// Feed peers more often right after the bootstrap, then backoff
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 15 * time.Second
bo.Multiplier = 3
bo.MaxInterval = 1 * time.Hour
bo.MaxElapsedTime = 0 // never stop
t := backoff.NewTicker(bo)
defer t.Stop()
for {
select {
case <-t.C:
case <-ctx.Done():
return
}
// TODO: refactor AutoRelayFeeder, for now we skip it if dht missing to fix sharness panics
if dht == nil {
fmt.Println("AutoRelayFeeder: noop due to missing dht.WAN")
/* noop due to missing dht.WAN. happens in some unit tests,
not worth fixing as we will refactor this after go-libp2p 0.20 */
continue
}
closestPeers, err := dht.WAN.GetClosestPeers(ctx, h.ID().String())
if err != nil {
fmt.Println(err)
// usually 'failed to find any peer in table', no no-op
continue
}
for _, p := range closestPeers {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
bazil.org/fuse v0.0.0-20200117225306-7b5117fecadc
contrib.go.opencensus.io/exporter/prometheus v0.4.0
github.com/blang/semver/v4 v4.0.0
github.com/cenkalti/backoff/v4 v4.1.2
github.com/ceramicnetwork/go-dag-jose v0.1.0
github.com/cespare/xxhash v1.1.0
github.com/cheggaaa/pb v1.0.29
Expand Down Expand Up @@ -135,7 +136,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/containerd/cgroups v1.0.3 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,6 @@ github.com/ipfs/go-metrics-prometheus v0.0.2 h1:9i2iljLg12S78OhC6UAiXi176xvQGiZa
github.com/ipfs/go-metrics-prometheus v0.0.2/go.mod h1:ELLU99AQQNi+zX6GCGm2lAgnzdSH3u5UVlCdqSXnEks=
github.com/ipfs/go-mfs v0.2.1 h1:5jz8+ukAg/z6jTkollzxGzhkl3yxm022Za9f2nL5ab8=
github.com/ipfs/go-mfs v0.2.1/go.mod h1:Woj80iuw4ajDnIP6+seRaoHpPsc9hmL0pk/nDNDWP88=
github.com/ipfs/go-namesys v0.4.1-0.20220426174228-2e37afbc2bf2 h1:Vr44R5BWU1iGDnloo66+O9OKJcCCiZB5ZVOqdWaHVtw=
github.com/ipfs/go-namesys v0.4.1-0.20220426174228-2e37afbc2bf2/go.mod h1:KLFhk7IN1TB+WqtB6hxuCQFfBi+4b6nsaKgRuJjYUtA=
github.com/ipfs/go-namesys v0.4.1-0.20220427151138-605965e675aa h1:PheaiRiAxx2A6OyOmB9fsuT/MJ1QOR31ipbyNOJgND8=
github.com/ipfs/go-namesys v0.4.1-0.20220427151138-605965e675aa/go.mod h1:KLFhk7IN1TB+WqtB6hxuCQFfBi+4b6nsaKgRuJjYUtA=
github.com/ipfs/go-path v0.0.7/go.mod h1:6KTKmeRnBXgqrTvzFrPV3CamxcgvXX/4z79tfAd2Sno=
Expand Down

0 comments on commit 25950f3

Please sign in to comment.