]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add ClientConfig.DialRateLimiter, handle dial rate limiting errors
authorMatt Joiner <anacrolix@gmail.com>
Thu, 18 May 2023 00:41:51 +0000 (10:41 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 18 May 2023 00:41:51 +0000 (10:41 +1000)
client.go
config.go

index bc9bafbbd8fd95b065ebd03bc154426729fc37e9..21f213b325b7163ab9d1597fbbff825a1566340c 100644 (file)
--- a/client.go
+++ b/client.go
@@ -35,7 +35,6 @@ import (
        "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 @@ type Client 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 @@ func doProtocolHandshakeOnDialResult(
 
 // 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{})
index 1764168972e59fb4e6adad67d33cf23d2566245e..fca37e637a5d5c8a148d96fe2a1895a88133a345 100644 (file)
--- a/config.go
+++ b/config.go
@@ -180,6 +180,8 @@ type ClientConfig struct {
        DisableWebseeds   bool
 
        Callbacks Callbacks
+
+       DialRateLimiter *rate.Limiter
 }
 
 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
@@ -223,6 +225,7 @@ func NewDefaultClientConfig() *ClientConfig {
                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) }