"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"
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
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
// 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) {
+ // It would be better if dial rate limiting could be tested when considering to open connections
+ // instead. Doing it here means if the limit is low, and the half-open limit is high, we could
+ // end up with lots of outgoing connection attempts pending that were initiated on stale data.
+ {
+ dialReservation := cl.config.DialRateLimiter.Reserve()
+ if !opts.receivedHolepunchConnect {
+ if !dialReservation.OK() {
+ err = errors.New("can't make dial limit reservation")
+ return
+ }
+ time.Sleep(dialReservation.Delay())
+ }
+ }
torrent.Add("establish outgoing connection", 1)
addr := opts.peerInfo.Addr
dialPool := dialPool{
opts outgoingConnOpts,
attemptKey outgoingConnAttemptKey,
) {
- cl.dialRateLimiter.Wait(context.Background())
c, err := cl.dialAndCompleteHandshake(opts)
if err == nil {
c.conn.SetWriteDeadline(time.Time{})