From 82e6227b344d07cb34510580e771732127460bc2 Mon Sep 17 00:00:00 2001 From: Sukun Date: Thu, 8 Jun 2023 22:08:03 +0530 Subject: [PATCH] swarm: make smart-dialing opt in (#2340) --- config/config.go | 9 ++++++--- options.go | 12 ++++++++---- p2p/net/swarm/dial_ranker.go | 4 ++-- p2p/net/swarm/dial_ranker_test.go | 2 +- p2p/net/swarm/dial_worker.go | 2 +- p2p/net/swarm/dial_worker_test.go | 2 +- p2p/net/swarm/swarm.go | 9 --------- 7 files changed, 19 insertions(+), 21 deletions(-) diff --git a/config/config.go b/config/config.go index 7a6d7fa908..0eb2b0d97a 100644 --- a/config/config.go +++ b/config/config.go @@ -124,7 +124,7 @@ type Config struct { DisableMetrics bool PrometheusRegisterer prometheus.Registerer - NoDelayNetworkDialRanker bool + DialRanker network.DialRanker } func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swarm, error) { @@ -175,9 +175,11 @@ func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swa if cfg.MultiaddrResolver != nil { opts = append(opts, swarm.WithMultiaddrResolver(cfg.MultiaddrResolver)) } - if cfg.NoDelayNetworkDialRanker { - opts = append(opts, swarm.WithNoDialDelay()) + dialRanker := cfg.DialRanker + if dialRanker == nil { + dialRanker = swarm.NoDelayDialRanker } + opts = append(opts, swarm.WithDialRanker(dialRanker)) if enableMetrics { opts = append(opts, swarm.WithMetricsTracer(swarm.NewMetricsTracer(swarm.WithRegisterer(cfg.PrometheusRegisterer)))) @@ -410,6 +412,7 @@ func (cfg *Config) NewNode() (host.Host, error) { Reporter: cfg.Reporter, PeerKey: autonatPrivKey, Peerstore: ps, + DialRanker: swarm.NoDelayDialRanker, } dialer, err := autoNatCfg.makeSwarm(eventbus.NewBus(), false) diff --git a/options.go b/options.go index 41057a7180..a124a2e27b 100644 --- a/options.go +++ b/options.go @@ -575,11 +575,15 @@ func PrometheusRegisterer(reg prometheus.Registerer) Option { } } -// NoDelayNetworkDialRanker configures libp2p to disable dial prioritisation and dial -// all addresses of the peer without any delay -func NoDelayNetworkDialRanker() Option { +// DialRanker configures libp2p to use d as the dial ranker. To enable smart +// dialing use `swarm.DefaultDialRanker`. use `swarm.NoDelayDialRanker` to +// disable smart dialing. +func DialRanker(d network.DialRanker) Option { return func(cfg *Config) error { - cfg.NoDelayNetworkDialRanker = true + if cfg.DialRanker != nil { + return errors.New("dial ranker already configured") + } + cfg.DialRanker = d return nil } } diff --git a/p2p/net/swarm/dial_ranker.go b/p2p/net/swarm/dial_ranker.go index f0b6ca621f..479db7ff9d 100644 --- a/p2p/net/swarm/dial_ranker.go +++ b/p2p/net/swarm/dial_ranker.go @@ -24,8 +24,8 @@ const ( RelayDelay = 250 * time.Millisecond ) -// noDelayRanker ranks addresses with no delay. This is useful for simultaneous connect requests. -func noDelayRanker(addrs []ma.Multiaddr) []network.AddrDelay { +// NoDelayDialRanker ranks addresses with no delay. This is useful for simultaneous connect requests. +func NoDelayDialRanker(addrs []ma.Multiaddr) []network.AddrDelay { return getAddrDelay(addrs, 0, 0, 0) } diff --git a/p2p/net/swarm/dial_ranker_test.go b/p2p/net/swarm/dial_ranker_test.go index 070932541f..8bc347ed03 100644 --- a/p2p/net/swarm/dial_ranker_test.go +++ b/p2p/net/swarm/dial_ranker_test.go @@ -53,7 +53,7 @@ func TestNoDelayRanker(t *testing.T) { } for _, tc := range testCase { t.Run(tc.name, func(t *testing.T) { - res := noDelayRanker(tc.addrs) + res := NoDelayDialRanker(tc.addrs) if len(res) != len(tc.output) { log.Errorf("expected %s got %s", tc.output, res) t.Errorf("expected elems: %d got: %d", len(tc.output), len(res)) diff --git a/p2p/net/swarm/dial_worker.go b/p2p/net/swarm/dial_worker.go index 8d574eba7f..5688494f49 100644 --- a/p2p/net/swarm/dial_worker.go +++ b/p2p/net/swarm/dial_worker.go @@ -415,7 +415,7 @@ func (w *dialWorker) dispatchError(ad *addrDial, err error) { // dial all addresses immediately without any delay func (w *dialWorker) rankAddrs(addrs []ma.Multiaddr, isSimConnect bool) []network.AddrDelay { if isSimConnect { - return noDelayRanker(addrs) + return NoDelayDialRanker(addrs) } return w.s.dialRanker(addrs) } diff --git a/p2p/net/swarm/dial_worker_test.go b/p2p/net/swarm/dial_worker_test.go index 903a9e500c..89130eeeb2 100644 --- a/p2p/net/swarm/dial_worker_test.go +++ b/p2p/net/swarm/dial_worker_test.go @@ -787,7 +787,7 @@ func TestCheckDialWorkerLoopScheduling(t *testing.T) { s4 := makeSwarmWithNoListenAddrs(t) defer s4.Close() // invalid ranking logic to trigger an error - s3.dialRanker = noDelayRanker + s3.dialRanker = NoDelayDialRanker err = checkDialWorkerLoopScheduling(t, s3, s4, tc) require.Error(t, err) } diff --git a/p2p/net/swarm/swarm.go b/p2p/net/swarm/swarm.go index eaae4bcfc8..0bffab99a2 100644 --- a/p2p/net/swarm/swarm.go +++ b/p2p/net/swarm/swarm.go @@ -100,15 +100,6 @@ func WithResourceManager(m network.ResourceManager) Option { } } -// WithNoDialDelay configures swarm to dial all addresses for a peer without -// any delay -func WithNoDialDelay() Option { - return func(s *Swarm) error { - s.dialRanker = noDelayRanker - return nil - } -} - // WithDialRanker configures swarm to use d as the DialRanker func WithDialRanker(d network.DialRanker) Option { return func(s *Swarm) error {