Browse Source

swarm: make smart-dialing opt in (#2340)

pull/2344/head
Sukun 1 year ago
committed by GitHub
parent
commit
82e6227b34
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      config/config.go
  2. 12
      options.go
  3. 4
      p2p/net/swarm/dial_ranker.go
  4. 2
      p2p/net/swarm/dial_ranker_test.go
  5. 2
      p2p/net/swarm/dial_worker.go
  6. 2
      p2p/net/swarm/dial_worker_test.go
  7. 9
      p2p/net/swarm/swarm.go

9
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)

12
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
}
}

4
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)
}

2
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))

2
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)
}

2
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)
}

9
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 {

Loading…
Cancel
Save