Add ClientConfig.DialRateLimiter, handle dial rate limiting errors

This commit is contained in:
Matt Joiner 2023-05-18 10:41:51 +10:00
parent 9352f6cf8e
commit 6b18583a06
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
2 changed files with 9 additions and 6 deletions

View File

@ -35,7 +35,6 @@ import (
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
gbtree "github.com/google/btree" gbtree "github.com/google/btree"
"github.com/pion/datachannel" "github.com/pion/datachannel"
"golang.org/x/time/rate"
"github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/internal/check" "github.com/anacrolix/torrent/internal/check"
@ -82,9 +81,8 @@ type Client struct {
torrents map[InfoHash]*Torrent torrents map[InfoHash]*Torrent
pieceRequestOrder map[interface{}]*request_strategy.PieceRequestOrder pieceRequestOrder map[interface{}]*request_strategy.PieceRequestOrder
acceptLimiter map[ipStr]int acceptLimiter map[ipStr]int
dialRateLimiter *rate.Limiter numHalfOpen int
numHalfOpen int
websocketTrackers websocketTrackers websocketTrackers websocketTrackers
@ -201,7 +199,6 @@ func (cl *Client) init(cfg *ClientConfig) {
cl.config = cfg cl.config = cfg
g.MakeMap(&cl.dopplegangerAddrs) g.MakeMap(&cl.dopplegangerAddrs)
cl.torrents = make(map[metainfo.Hash]*Torrent) cl.torrents = make(map[metainfo.Hash]*Torrent)
cl.dialRateLimiter = rate.NewLimiter(10, 10)
cl.activeAnnounceLimiter.SlotsPerKey = 2 cl.activeAnnounceLimiter.SlotsPerKey = 2
cl.event.L = cl.locker() cl.event.L = cl.locker()
cl.ipBlockList = cfg.IPBlocklist 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. // 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) { 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) torrent.Add("establish outgoing connection", 1)
addr := opts.peerInfo.Addr addr := opts.peerInfo.Addr
dialPool := dialPool{ dialPool := dialPool{
@ -843,7 +844,6 @@ func (cl *Client) outgoingConnection(
opts outgoingConnOpts, opts outgoingConnOpts,
attemptKey outgoingConnAttemptKey, attemptKey outgoingConnAttemptKey,
) { ) {
cl.dialRateLimiter.Wait(context.Background())
c, err := cl.dialAndCompleteHandshake(opts) c, err := cl.dialAndCompleteHandshake(opts)
if err == nil { if err == nil {
c.conn.SetWriteDeadline(time.Time{}) c.conn.SetWriteDeadline(time.Time{})

View File

@ -180,6 +180,8 @@ type ClientConfig struct {
DisableWebseeds bool DisableWebseeds bool
Callbacks Callbacks Callbacks Callbacks
DialRateLimiter *rate.Limiter
} }
func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig { func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
@ -223,6 +225,7 @@ func NewDefaultClientConfig() *ClientConfig {
Extensions: defaultPeerExtensionBytes(), Extensions: defaultPeerExtensionBytes(),
AcceptPeerConnections: true, AcceptPeerConnections: true,
MaxUnverifiedBytes: 64 << 20, MaxUnverifiedBytes: 64 << 20,
DialRateLimiter: rate.NewLimiter(10, 10),
} }
cc.DhtStartingNodes = func(network string) dht.StartingNodesGetter { cc.DhtStartingNodes = func(network string) dht.StartingNodesGetter {
return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) } return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) }