client.go | 12 ++++++------ config.go | 3 +++ diff --git a/client.go b/client.go index bc9bafbbd8fd95b065ebd03bc154426729fc37e9..21f213b325b7163ab9d1597fbbff825a1566340c 100644 --- a/client.go +++ b/client.go @@ -35,7 +35,6 @@ "github.com/davecgh/go-spew/spew" "github.com/dustin/go-humanize" gbtree "github.com/google/btree" "github.com/pion/datachannel" - "golang.org/x/time/rate" "github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/internal/check" @@ -82,9 +81,8 @@ badPeerIPs map[netip.Addr]struct{} torrents map[InfoHash]*Torrent pieceRequestOrder map[interface{}]*request_strategy.PieceRequestOrder - acceptLimiter map[ipStr]int - dialRateLimiter *rate.Limiter - numHalfOpen int + acceptLimiter map[ipStr]int + numHalfOpen int websocketTrackers websocketTrackers @@ -201,7 +199,6 @@ func (cl *Client) init(cfg *ClientConfig) { cl.config = cfg g.MakeMap(&cl.dopplegangerAddrs) cl.torrents = make(map[metainfo.Hash]*Torrent) - cl.dialRateLimiter = rate.NewLimiter(10, 10) cl.activeAnnounceLimiter.SlotsPerKey = 2 cl.event.L = cl.locker() cl.ipBlockList = cfg.IPBlocklist @@ -734,6 +731,10 @@ } // Returns nil connection and nil error if no connection could be established for valid reasons. func (cl *Client) dialAndCompleteHandshake(opts outgoingConnOpts) (c *PeerConn, err error) { + err = cl.config.DialRateLimiter.Wait(context.Background()) + if err != nil { + return + } torrent.Add("establish outgoing connection", 1) addr := opts.peerInfo.Addr dialPool := dialPool{ @@ -843,7 +844,6 @@ func (cl *Client) outgoingConnection( opts outgoingConnOpts, attemptKey outgoingConnAttemptKey, ) { - cl.dialRateLimiter.Wait(context.Background()) c, err := cl.dialAndCompleteHandshake(opts) if err == nil { c.conn.SetWriteDeadline(time.Time{}) diff --git a/config.go b/config.go index 1764168972e59fb4e6adad67d33cf23d2566245e..fca37e637a5d5c8a148d96fe2a1895a88133a345 100644 --- a/config.go +++ b/config.go @@ -180,6 +180,8 @@ DisableWebtorrent bool DisableWebseeds bool Callbacks Callbacks + + DialRateLimiter *rate.Limiter } func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig { @@ -223,6 +225,7 @@ ListenPort: 42069, Extensions: defaultPeerExtensionBytes(), AcceptPeerConnections: true, MaxUnverifiedBytes: 64 << 20, + DialRateLimiter: rate.NewLimiter(10, 10), } cc.DhtStartingNodes = func(network string) dht.StartingNodesGetter { return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) }