Skip to content

Commit f27dc6d

Browse files
committed
feat(AutoRelay): prioritize Peering.Peers
This ensures we feed trusted Peering.Peers in addition to any peers discovered over DHT.
1 parent 25950f3 commit f27dc6d

File tree

2 files changed

+72
-46
lines changed

2 files changed

+72
-46
lines changed

core/node/groups.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {
174174
maybeProvide(libp2p.BandwidthCounter, !cfg.Swarm.DisableBandwidthMetrics),
175175
maybeProvide(libp2p.NatPortMap, !cfg.Swarm.DisableNatPortMap),
176176
maybeProvide(libp2p.AutoRelay(cfg.Swarm.RelayClient.StaticRelays, peerChan), enableRelayClient),
177-
maybeInvoke(libp2p.AutoRelayFeeder, enableRelayClient),
177+
maybeInvoke(libp2p.AutoRelayFeeder(cfg.Peering), enableRelayClient),
178178
autonat,
179179
connmgr,
180180
ps,

core/node/libp2p/routing.go

+71-45
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package libp2p
22

33
import (
44
"context"
5+
"fmt"
6+
"runtime/debug"
57
"sort"
68
"time"
79

810
"github.com/ipfs/go-ipfs/core/node/helpers"
911

12+
config "github.com/ipfs/go-ipfs/config"
1013
"github.com/ipfs/go-ipfs/repo"
1114
"github.com/libp2p/go-libp2p-core/host"
1215
"github.com/libp2p/go-libp2p-core/peer"
@@ -182,56 +185,79 @@ func PubsubRouter(mctx helpers.MetricsCtx, lc fx.Lifecycle, in p2pPSRoutingIn) (
182185
}, psRouter, nil
183186
}
184187

185-
func AutoRelayFeeder(lc fx.Lifecycle, h host.Host, peerChan chan peer.AddrInfo, dht *ddht.DHT) {
186-
ctx, cancel := context.WithCancel(context.Background())
187-
188-
done := make(chan struct{})
189-
go func() {
190-
defer close(done)
191-
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)
199-
defer t.Stop()
200-
for {
201-
select {
202-
case <-t.C:
203-
case <-ctx.Done():
204-
return
205-
}
206-
if dht == nil {
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 */
209-
continue
210-
}
211-
closestPeers, err := dht.WAN.GetClosestPeers(ctx, h.ID().String())
212-
if err != nil {
213-
// usually 'failed to find any peer in table', no no-op
214-
continue
188+
func AutoRelayFeeder(cfgPeering config.Peering) func(fx.Lifecycle, host.Host, chan peer.AddrInfo, *ddht.DHT) {
189+
return func(lc fx.Lifecycle, h host.Host, peerChan chan peer.AddrInfo, dht *ddht.DHT) {
190+
ctx, cancel := context.WithCancel(context.Background())
191+
done := make(chan struct{})
192+
193+
defer func() {
194+
if r := recover(); r != nil {
195+
fmt.Println("Recovering from unexpected error in AutoRelayFeeder:", r)
196+
debug.PrintStack()
215197
}
216-
for _, p := range closestPeers {
217-
addrs := h.Peerstore().Addrs(p)
218-
if len(addrs) == 0 {
219-
continue
220-
}
198+
}()
199+
go func() {
200+
defer close(done)
201+
202+
// Feed peers more often right after the bootstrap, then backoff
203+
bo := backoff.NewExponentialBackOff()
204+
bo.InitialInterval = 15 * time.Second
205+
bo.Multiplier = 3
206+
bo.MaxInterval = 1 * time.Hour
207+
bo.MaxElapsedTime = 0 // never stop
208+
t := backoff.NewTicker(bo)
209+
defer t.Stop()
210+
for {
221211
select {
222-
case peerChan <- peer.AddrInfo{ID: p, Addrs: addrs}:
212+
case <-t.C:
223213
case <-ctx.Done():
224214
return
225215
}
216+
217+
// Always feed trusted IDs (Peering.Peers in the config)
218+
for _, trustedPeer := range cfgPeering.Peers {
219+
if len(trustedPeer.Addrs) == 0 {
220+
continue
221+
}
222+
select {
223+
case peerChan <- trustedPeer:
224+
case <-ctx.Done():
225+
return
226+
}
227+
}
228+
229+
// Additionally, feed closest peers discovered via DHT
230+
if dht == nil {
231+
/* noop due to missing dht.WAN. happens in some unit tests,
232+
not worth fixing as we will refactor this after go-libp2p 0.20 */
233+
continue
234+
}
235+
closestPeers, err := dht.WAN.GetClosestPeers(ctx, h.ID().String())
236+
if err != nil {
237+
// no-op: usually 'failed to find any peer in table' during startup
238+
continue
239+
}
240+
for _, p := range closestPeers {
241+
addrs := h.Peerstore().Addrs(p)
242+
if len(addrs) == 0 {
243+
continue
244+
}
245+
dhtPeer := peer.AddrInfo{ID: p, Addrs: addrs}
246+
select {
247+
case peerChan <- dhtPeer:
248+
case <-ctx.Done():
249+
return
250+
}
251+
}
226252
}
227-
}
228-
}()
253+
}()
229254

230-
lc.Append(fx.Hook{
231-
OnStop: func(_ context.Context) error {
232-
cancel()
233-
<-done
234-
return nil
235-
},
236-
})
255+
lc.Append(fx.Hook{
256+
OnStop: func(_ context.Context) error {
257+
cancel()
258+
<-done
259+
return nil
260+
},
261+
})
262+
}
237263
}

0 commit comments

Comments
 (0)