X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=client.go;h=70b98482820e1bd488c161383c176c02dd3f14d4;hb=f45365fd986e2957843f70ff2c98c7145e82f17e;hp=21f213b325b7163ab9d1597fbbff825a1566340c;hpb=6b18583a0642929aa0c6e844e36c85d689657b4b;p=btrtrc.git diff --git a/client.go b/client.go index 21f213b3..70b98482 100644 --- a/client.go +++ b/client.go @@ -731,9 +731,18 @@ 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 + // 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