]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Set a minimum dial timeout
authorMatt Joiner <anacrolix@gmail.com>
Wed, 19 Nov 2014 03:53:00 +0000 (21:53 -0600)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 19 Nov 2014 03:53:00 +0000 (21:53 -0600)
client.go
client_test.go
misc.go

index 5c1223c4146076fc1aca6232e70da3ece8ec9666..e51019c007caf241cf9a75879e1a720101fb6058 100644 (file)
--- a/client.go
+++ b/client.go
@@ -394,8 +394,12 @@ func doDial(dial func() (net.Conn, error), ch chan dialResult, utp bool) {
        }
 }
 
-func reducedDialTimeout(max time.Duration, halfOpenLimit int, pendingPeers int) time.Duration {
-       return max / time.Duration((pendingPeers+halfOpenLimit)/halfOpenLimit)
+func reducedDialTimeout(max time.Duration, halfOpenLimit int, pendingPeers int) (ret time.Duration) {
+       ret = max / time.Duration((pendingPeers+halfOpenLimit)/halfOpenLimit)
+       if ret < minDialTimeout {
+               ret = minDialTimeout
+       }
+       return
 }
 
 // Start the process of connecting to the given peer for the given torrent if
@@ -409,7 +413,7 @@ func (me *Client) initiateConn(peer Peer, t *torrent) {
                duplicateConnsAvoided.Add(1)
                return
        }
-       dialTimeout := reducedDialTimeout(dialTimeout, me.halfOpenLimit, len(t.Peers))
+       dialTimeout := reducedDialTimeout(nominalDialTimeout, me.halfOpenLimit, len(t.Peers))
        t.HalfOpen[addr] = struct{}{}
        go func() {
                // Binding to the listen address and dialing via net.Dialer gives
index 05d08e694dfaea7f42bb0f59ffc4d7e91ae0ca60..207790df72b39f372408fadbd26bfa96e0185889 100644 (file)
@@ -84,15 +84,19 @@ func TestReducedDialTimeout(t *testing.T) {
                PendingPeers    int
                ExpectedReduced time.Duration
        }{
-               {dialTimeout, 40, 0, dialTimeout},
-               {dialTimeout, 40, 1, dialTimeout},
-               {dialTimeout, 40, 39, dialTimeout},
-               {dialTimeout, 40, 40, dialTimeout / 2},
-               {dialTimeout, 40, 80, dialTimeout / 3},
-               {dialTimeout, 40, 4000, dialTimeout / 101},
+               {nominalDialTimeout, 40, 0, nominalDialTimeout},
+               {nominalDialTimeout, 40, 1, nominalDialTimeout},
+               {nominalDialTimeout, 40, 39, nominalDialTimeout},
+               {nominalDialTimeout, 40, 40, nominalDialTimeout / 2},
+               {nominalDialTimeout, 40, 80, nominalDialTimeout / 3},
+               {nominalDialTimeout, 40, 4000, nominalDialTimeout / 101},
        } {
                reduced := reducedDialTimeout(_case.Max, _case.HalfOpenLimit, _case.PendingPeers)
-               if reduced != _case.ExpectedReduced {
+               expected := _case.ExpectedReduced
+               if expected < minDialTimeout {
+                       expected = minDialTimeout
+               }
+               if reduced != expected {
                        t.Fatalf("expected %s, got %s", _case.ExpectedReduced, reduced)
                }
        }
diff --git a/misc.go b/misc.go
index 295415151fd8bd63517ef58dd76342de6cc634ca..b96faf06573ea3d19c3e275df9ba7e2f567a5077 100644 (file)
--- a/misc.go
+++ b/misc.go
@@ -15,11 +15,12 @@ import (
 )
 
 const (
-       pieceHash   = crypto.SHA1
-       maxRequests = 250        // Maximum pending requests we allow peers to send us.
-       chunkSize   = 0x4000     // 16KiB
-       BEP20       = "-GT0000-" // Peer ID client identifier prefix
-       dialTimeout = time.Second * 30
+       pieceHash          = crypto.SHA1
+       maxRequests        = 250        // Maximum pending requests we allow peers to send us.
+       chunkSize          = 0x4000     // 16KiB
+       BEP20              = "-GT0000-" // Peer ID client identifier prefix
+       nominalDialTimeout = time.Second * 30
+       minDialTimeout     = 5 * time.Second
 )
 
 type (
@@ -156,6 +157,7 @@ func mmapTorrentData(md *metainfo.Info, location string) (mms mmap_span.MMapSpan
        return
 }
 
+// The size in bytes of a metadata extension piece.
 func metadataPieceSize(totalSize int, piece int) int {
        ret := totalSize - piece*(1<<14)
        if ret > 1<<14 {