From 7f93290484218180a48546c80fea39128d38d2fc Mon Sep 17 00:00:00 2001 From: sukun Date: Fri, 14 Jul 2023 11:08:31 +0530 Subject: [PATCH 1/3] swarm: deprecate libp2p.DialRanker option --- config/config.go | 8 +++----- options.go | 1 + p2p/net/swarm/swarm.go | 3 +++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index b529ec1c51..48f2421b6f 100644 --- a/config/config.go +++ b/config/config.go @@ -163,6 +163,7 @@ func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swa } opts := cfg.SwarmOpts + if cfg.Reporter != nil { opts = append(opts, swarm.WithMetrics(cfg.Reporter)) } @@ -178,12 +179,9 @@ func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swa if cfg.MultiaddrResolver != nil { opts = append(opts, swarm.WithMultiaddrResolver(cfg.MultiaddrResolver)) } - - dialRanker := cfg.DialRanker - if dialRanker == nil { - dialRanker = swarm.DefaultDialRanker + if cfg.DialRanker != nil { + opts = append(opts, swarm.WithDialRanker(cfg.DialRanker)) } - opts = append(opts, swarm.WithDialRanker(dialRanker)) if enableMetrics { opts = append(opts, diff --git a/options.go b/options.go index 3e072b950c..a96e147c80 100644 --- a/options.go +++ b/options.go @@ -579,6 +579,7 @@ func PrometheusRegisterer(reg prometheus.Registerer) 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. +// Deprecated: use SwarmOpts(swarm.WithDialRanker(d)) func DialRanker(d network.DialRanker) Option { return func(cfg *Config) error { if cfg.DialRanker != nil { diff --git a/p2p/net/swarm/swarm.go b/p2p/net/swarm/swarm.go index 9dfab2d9c0..ca3bc6824b 100644 --- a/p2p/net/swarm/swarm.go +++ b/p2p/net/swarm/swarm.go @@ -103,6 +103,9 @@ func WithResourceManager(m network.ResourceManager) Option { // WithDialRanker configures swarm to use d as the DialRanker func WithDialRanker(d network.DialRanker) Option { return func(s *Swarm) error { + if d == nil { + return errors.New("dial ranker cannot be nil") + } s.dialRanker = d return nil } From 6fe9da380ee923094dcf505fefa2ea46525902d8 Mon Sep 17 00:00:00 2001 From: sukun Date: Fri, 14 Jul 2023 11:10:09 +0530 Subject: [PATCH 2/3] remove whitespace diff --- config/config.go | 1 - 1 file changed, 1 deletion(-) diff --git a/config/config.go b/config/config.go index 48f2421b6f..cea8ef1314 100644 --- a/config/config.go +++ b/config/config.go @@ -163,7 +163,6 @@ func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swa } opts := cfg.SwarmOpts - if cfg.Reporter != nil { opts = append(opts, swarm.WithMetrics(cfg.Reporter)) } From 2b39a0dd84b8db3f1effe7e1c672e10855e0ea08 Mon Sep 17 00:00:00 2001 From: Sukun Date: Fri, 14 Jul 2023 11:16:29 +0530 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Marten Seemann --- options.go | 2 +- p2p/net/swarm/swarm.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/options.go b/options.go index a96e147c80..beb4930f7c 100644 --- a/options.go +++ b/options.go @@ -579,7 +579,7 @@ func PrometheusRegisterer(reg prometheus.Registerer) 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. -// Deprecated: use SwarmOpts(swarm.WithDialRanker(d)) +// Deprecated: use SwarmOpts(swarm.WithDialRanker(d)) instead func DialRanker(d network.DialRanker) Option { return func(cfg *Config) error { if cfg.DialRanker != nil { diff --git a/p2p/net/swarm/swarm.go b/p2p/net/swarm/swarm.go index ca3bc6824b..4bc16a7d31 100644 --- a/p2p/net/swarm/swarm.go +++ b/p2p/net/swarm/swarm.go @@ -104,7 +104,7 @@ func WithResourceManager(m network.ResourceManager) Option { func WithDialRanker(d network.DialRanker) Option { return func(s *Swarm) error { if d == nil { - return errors.New("dial ranker cannot be nil") + return errors.New("swarm: dial ranker cannot be nil") } s.dialRanker = d return nil