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 }