client.go | 13 ++++++++----- diff --git a/client.go b/client.go index 14fff4f64da6fd8b66895f1cb7f0967b936951a0..f1cb3635da4bb19bd92481f3e8647d257e6543be 100644 --- a/client.go +++ b/client.go @@ -26,16 +26,16 @@ "github.com/anacrolix/missinggo/pproffd" "github.com/anacrolix/missinggo/pubsub" "github.com/anacrolix/missinggo/slices" "github.com/anacrolix/sync" - "github.com/davecgh/go-spew/spew" - "github.com/dustin/go-humanize" - "github.com/google/btree" - "github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/iplist" "github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/mse" pp "github.com/anacrolix/torrent/peer_protocol" "github.com/anacrolix/torrent/storage" + "github.com/davecgh/go-spew/spew" + "github.com/dustin/go-humanize" + "github.com/google/btree" + "golang.org/x/time/rate" ) // Clients contain zero or more Torrents. A Client manages a blocklist, the @@ -68,7 +68,8 @@ dopplegangerAddrs map[string]struct{} badPeerIPs map[string]struct{} torrents map[InfoHash]*Torrent - acceptLimiter map[ipStr]int + acceptLimiter map[ipStr]int + dialRateLimiter *rate.Limiter } type ipStr string @@ -186,6 +187,7 @@ cl = &Client{ config: cfg, dopplegangerAddrs: make(map[string]struct{}), torrents: make(map[metainfo.Hash]*Torrent), + dialRateLimiter: rate.NewLimiter(10, 10), } go cl.acceptLimitClearer() cl.initLogger() @@ -656,6 +658,7 @@ // Called to dial out and run a connection. The addr we're given is already // considered half-open. func (cl *Client) outgoingConnection(t *Torrent, addr string, ps peerSource) { + cl.dialRateLimiter.Wait(context.Background()) c, err := cl.establishOutgoingConn(t, addr) cl.lock() defer cl.unlock()