]> Sergey Matveev's repositories - btrtrc.git/blobdiff - client.go
Ignore dial rate limits for holepunch connects
[btrtrc.git] / client.go
index 21f213b325b7163ab9d1597fbbff825a1566340c..70b98482820e1bd488c161383c176c02dd3f14d4 100644 (file)
--- 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