From 56197b8e14cfbfbf8797986aba0a20b7c6490408 Mon Sep 17 00:00:00 2001 From: millken Date: Tue, 31 May 2022 21:26:11 +0800 Subject: [PATCH 1/9] Added perPeerRateLimit env config Fix #1579 --- p2p/net/swarm/limiter.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/p2p/net/swarm/limiter.go b/p2p/net/swarm/limiter.go index 6b49d8ec0b..cc791f277b 100644 --- a/p2p/net/swarm/limiter.go +++ b/p2p/net/swarm/limiter.go @@ -54,7 +54,13 @@ func newDialLimiter(df dialfunc) *dialLimiter { fd = int(n) } } - return newDialLimiterWithParams(df, fd, DefaultPerPeerRateLimit) + perPeerRateLimit := DefaultPerPeerRateLimit + if env := os.Getenv("LIBP2P_SWARM_PEER_RATE_LIMIT"); env != "" { + if n, err := strconv.ParseInt(env, 10, 32); err == nil { + perPeerRateLimit = int(n) + } + } + return newDialLimiterWithParams(df, fd, perPeerRateLimit) } func newDialLimiterWithParams(df dialfunc, fdLimit, perPeerLimit int) *dialLimiter { From 80851661857e916b4f8df6ef61b1c06fe938e10f Mon Sep 17 00:00:00 2001 From: millken Date: Mon, 6 Jun 2022 15:46:00 +0800 Subject: [PATCH 2/9] Add global options swarm.WithPerPeerLimit, swarm.WithFDLimit to control dialLimiter --- p2p/net/swarm/limiter.go | 18 ------------------ p2p/net/swarm/swarm.go | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/p2p/net/swarm/limiter.go b/p2p/net/swarm/limiter.go index cc791f277b..ea01f4923e 100644 --- a/p2p/net/swarm/limiter.go +++ b/p2p/net/swarm/limiter.go @@ -2,8 +2,6 @@ package swarm import ( "context" - "os" - "strconv" "sync" "time" @@ -47,22 +45,6 @@ type dialLimiter struct { type dialfunc func(context.Context, peer.ID, ma.Multiaddr) (transport.CapableConn, error) -func newDialLimiter(df dialfunc) *dialLimiter { - fd := ConcurrentFdDials - if env := os.Getenv("LIBP2P_SWARM_FD_LIMIT"); env != "" { - if n, err := strconv.ParseInt(env, 10, 32); err == nil { - fd = int(n) - } - } - perPeerRateLimit := DefaultPerPeerRateLimit - if env := os.Getenv("LIBP2P_SWARM_PEER_RATE_LIMIT"); env != "" { - if n, err := strconv.ParseInt(env, 10, 32); err == nil { - perPeerRateLimit = int(n) - } - } - return newDialLimiterWithParams(df, fd, perPeerRateLimit) -} - func newDialLimiterWithParams(df dialfunc, fdLimit, perPeerLimit int) *dialLimiter { return &dialLimiter{ fdLimit: fdLimit, diff --git a/p2p/net/swarm/swarm.go b/p2p/net/swarm/swarm.go index e14a068c77..9cba9443ae 100644 --- a/p2p/net/swarm/swarm.go +++ b/p2p/net/swarm/swarm.go @@ -83,6 +83,20 @@ func WithResourceManager(m network.ResourceManager) Option { } } +func WithPerPeerLimit(perPeerLimit int) Option { + return func(s *Swarm) error { + s.perPeerLimit = perPeerLimit + return nil + } +} + +func WithFDLimit(fdLimit int) Option { + return func(s *Swarm) error { + s.fdLimit = fdLimit + return nil + } +} + // Swarm is a connection muxer, allowing connections to other peers to // be opened and closed, while still using the same Chan for all // communication. The Chan sends/receives Messages, which note the @@ -141,6 +155,10 @@ type Swarm struct { ctxCancel context.CancelFunc bwc metrics.Reporter + + // dial limiter + perPeerLimit int + fdLimit int } // NewSwarm constructs a Swarm. @@ -153,6 +171,8 @@ func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) (*Swarm, ctxCancel: cancel, dialTimeout: defaultDialTimeout, dialTimeoutLocal: defaultDialTimeoutLocal, + perPeerLimit: DefaultPerPeerRateLimit, + fdLimit: ConcurrentFdDials, } s.conns.m = make(map[peer.ID][]*Conn) @@ -170,7 +190,7 @@ func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) (*Swarm, } s.dsync = newDialSync(s.dialWorkerLoop) - s.limiter = newDialLimiter(s.dialAddr) + s.limiter = newDialLimiterWithParams(s.dialAddr, s.fdLimit, s.perPeerLimit) s.backf.init(s.ctx) return s, nil } From 547aff6a7993071618d6cbc5df97ab1ad3efb3d5 Mon Sep 17 00:00:00 2001 From: millken Date: Mon, 6 Jun 2022 16:05:17 +0800 Subject: [PATCH 3/9] Update config --- config/config.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/config/config.go b/config/config.go index e1ce654562..99a5c3e13a 100644 --- a/config/config.go +++ b/config/config.go @@ -104,6 +104,9 @@ type Config struct { EnableHolePunching bool HolePunchingOptions []holepunch.Option + + FDLimit int + PerPeerLimit int } func (cfg *Config) makeSwarm() (*swarm.Swarm, error) { @@ -151,6 +154,12 @@ func (cfg *Config) makeSwarm() (*swarm.Swarm, error) { if cfg.ResourceManager != nil { opts = append(opts, swarm.WithResourceManager(cfg.ResourceManager)) } + if cfg.FDLimit != 0 { + opts = append(opts, swarm.WithFDLimit(cfg.FDLimit)) + } + if cfg.PerPeerLimit != 0 { + opts = append(opts, swarm.WithPerPeerLimit(cfg.PerPeerLimit)) + } // TODO: Make the swarm implementation configurable. return swarm.NewSwarm(pid, cfg.Peerstore, opts...) } From 83baea420cc7eb4a987f90f3d414ee2d8d6baae6 Mon Sep 17 00:00:00 2001 From: millken Date: Mon, 6 Jun 2022 21:22:53 +0800 Subject: [PATCH 4/9] Update options.go --- options.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/options.go b/options.go index ce40fa5d34..7c9645224b 100644 --- a/options.go +++ b/options.go @@ -477,3 +477,17 @@ func WithDialTimeout(t time.Duration) Option { return nil } } + +func WithPerPeerLimit(perPeerLimit int) Option { + return func(cfg *Config) error { + cfg.PerPeerLimit = perPeerLimit + return nil + } +} + +func WithFDLimit(fdLimit int) Option { + return func(cfg *Config) error { + cfg.FDLimit = fdLimit + return nil + } +} From 9f3ab2ed649e8bc07979649a967d5355c754f1ab Mon Sep 17 00:00:00 2001 From: millken Date: Tue, 14 Jun 2022 10:40:05 +0800 Subject: [PATCH 5/9] make DefaultPerPeerRateLimit to changeable --- p2p/net/swarm/swarm_dial.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/net/swarm/swarm_dial.go b/p2p/net/swarm/swarm_dial.go index ca11d7326f..f1e5276754 100644 --- a/p2p/net/swarm/swarm_dial.go +++ b/p2p/net/swarm/swarm_dial.go @@ -65,7 +65,7 @@ const ConcurrentFdDials = 160 // DefaultPerPeerRateLimit is the number of concurrent outbound dials to make // per peer -const DefaultPerPeerRateLimit = 8 +var DefaultPerPeerRateLimit = 8 // dialbackoff is a struct used to avoid over-dialing the same, dead peers. // Whenever we totally time out on a peer (all three attempts), we add them From bcec64e09acf570e107aa9d9138466a3e028489d Mon Sep 17 00:00:00 2001 From: millken Date: Tue, 14 Jun 2022 10:44:09 +0800 Subject: [PATCH 6/9] Revert "Update options.go" This reverts commit 83baea420cc7eb4a987f90f3d414ee2d8d6baae6. --- options.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/options.go b/options.go index 7c9645224b..ce40fa5d34 100644 --- a/options.go +++ b/options.go @@ -477,17 +477,3 @@ func WithDialTimeout(t time.Duration) Option { return nil } } - -func WithPerPeerLimit(perPeerLimit int) Option { - return func(cfg *Config) error { - cfg.PerPeerLimit = perPeerLimit - return nil - } -} - -func WithFDLimit(fdLimit int) Option { - return func(cfg *Config) error { - cfg.FDLimit = fdLimit - return nil - } -} From 1744b708a1990aced43207978742904ff4476e77 Mon Sep 17 00:00:00 2001 From: millken Date: Tue, 14 Jun 2022 10:45:46 +0800 Subject: [PATCH 7/9] Revert "Update config" This reverts commit 547aff6a7993071618d6cbc5df97ab1ad3efb3d5. --- config/config.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/config/config.go b/config/config.go index 99a5c3e13a..e1ce654562 100644 --- a/config/config.go +++ b/config/config.go @@ -104,9 +104,6 @@ type Config struct { EnableHolePunching bool HolePunchingOptions []holepunch.Option - - FDLimit int - PerPeerLimit int } func (cfg *Config) makeSwarm() (*swarm.Swarm, error) { @@ -154,12 +151,6 @@ func (cfg *Config) makeSwarm() (*swarm.Swarm, error) { if cfg.ResourceManager != nil { opts = append(opts, swarm.WithResourceManager(cfg.ResourceManager)) } - if cfg.FDLimit != 0 { - opts = append(opts, swarm.WithFDLimit(cfg.FDLimit)) - } - if cfg.PerPeerLimit != 0 { - opts = append(opts, swarm.WithPerPeerLimit(cfg.PerPeerLimit)) - } // TODO: Make the swarm implementation configurable. return swarm.NewSwarm(pid, cfg.Peerstore, opts...) } From 1379b333912613a6268dc515913a8d28b19241be Mon Sep 17 00:00:00 2001 From: millken Date: Tue, 14 Jun 2022 10:46:01 +0800 Subject: [PATCH 8/9] Revert "Add global options swarm.WithPerPeerLimit, swarm.WithFDLimit to control dialLimiter" This reverts commit 80851661857e916b4f8df6ef61b1c06fe938e10f. --- p2p/net/swarm/limiter.go | 18 ++++++++++++++++++ p2p/net/swarm/swarm.go | 22 +--------------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/p2p/net/swarm/limiter.go b/p2p/net/swarm/limiter.go index ea01f4923e..cc791f277b 100644 --- a/p2p/net/swarm/limiter.go +++ b/p2p/net/swarm/limiter.go @@ -2,6 +2,8 @@ package swarm import ( "context" + "os" + "strconv" "sync" "time" @@ -45,6 +47,22 @@ type dialLimiter struct { type dialfunc func(context.Context, peer.ID, ma.Multiaddr) (transport.CapableConn, error) +func newDialLimiter(df dialfunc) *dialLimiter { + fd := ConcurrentFdDials + if env := os.Getenv("LIBP2P_SWARM_FD_LIMIT"); env != "" { + if n, err := strconv.ParseInt(env, 10, 32); err == nil { + fd = int(n) + } + } + perPeerRateLimit := DefaultPerPeerRateLimit + if env := os.Getenv("LIBP2P_SWARM_PEER_RATE_LIMIT"); env != "" { + if n, err := strconv.ParseInt(env, 10, 32); err == nil { + perPeerRateLimit = int(n) + } + } + return newDialLimiterWithParams(df, fd, perPeerRateLimit) +} + func newDialLimiterWithParams(df dialfunc, fdLimit, perPeerLimit int) *dialLimiter { return &dialLimiter{ fdLimit: fdLimit, diff --git a/p2p/net/swarm/swarm.go b/p2p/net/swarm/swarm.go index 9cba9443ae..e14a068c77 100644 --- a/p2p/net/swarm/swarm.go +++ b/p2p/net/swarm/swarm.go @@ -83,20 +83,6 @@ func WithResourceManager(m network.ResourceManager) Option { } } -func WithPerPeerLimit(perPeerLimit int) Option { - return func(s *Swarm) error { - s.perPeerLimit = perPeerLimit - return nil - } -} - -func WithFDLimit(fdLimit int) Option { - return func(s *Swarm) error { - s.fdLimit = fdLimit - return nil - } -} - // Swarm is a connection muxer, allowing connections to other peers to // be opened and closed, while still using the same Chan for all // communication. The Chan sends/receives Messages, which note the @@ -155,10 +141,6 @@ type Swarm struct { ctxCancel context.CancelFunc bwc metrics.Reporter - - // dial limiter - perPeerLimit int - fdLimit int } // NewSwarm constructs a Swarm. @@ -171,8 +153,6 @@ func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) (*Swarm, ctxCancel: cancel, dialTimeout: defaultDialTimeout, dialTimeoutLocal: defaultDialTimeoutLocal, - perPeerLimit: DefaultPerPeerRateLimit, - fdLimit: ConcurrentFdDials, } s.conns.m = make(map[peer.ID][]*Conn) @@ -190,7 +170,7 @@ func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) (*Swarm, } s.dsync = newDialSync(s.dialWorkerLoop) - s.limiter = newDialLimiterWithParams(s.dialAddr, s.fdLimit, s.perPeerLimit) + s.limiter = newDialLimiter(s.dialAddr) s.backf.init(s.ctx) return s, nil } From 3d9342f33388ba2991106c965356652090c59ab2 Mon Sep 17 00:00:00 2001 From: millken Date: Tue, 14 Jun 2022 10:47:43 +0800 Subject: [PATCH 9/9] revert change --- p2p/net/swarm/limiter.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/p2p/net/swarm/limiter.go b/p2p/net/swarm/limiter.go index cc791f277b..6b49d8ec0b 100644 --- a/p2p/net/swarm/limiter.go +++ b/p2p/net/swarm/limiter.go @@ -54,13 +54,7 @@ func newDialLimiter(df dialfunc) *dialLimiter { fd = int(n) } } - perPeerRateLimit := DefaultPerPeerRateLimit - if env := os.Getenv("LIBP2P_SWARM_PEER_RATE_LIMIT"); env != "" { - if n, err := strconv.ParseInt(env, 10, 32); err == nil { - perPeerRateLimit = int(n) - } - } - return newDialLimiterWithParams(df, fd, perPeerRateLimit) + return newDialLimiterWithParams(df, fd, DefaultPerPeerRateLimit) } func newDialLimiterWithParams(df dialfunc, fdLimit, perPeerLimit int) *dialLimiter {