From: Matt Joiner Date: Wed, 19 Nov 2014 03:53:00 +0000 (-0600) Subject: Set a minimum dial timeout X-Git-Tag: v1.0.0~1506 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=358f089515a9c3b68dd7581a12ee70edb363e180;p=btrtrc.git Set a minimum dial timeout --- diff --git a/client.go b/client.go index 5c1223c4..e51019c0 100644 --- 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 diff --git a/client_test.go b/client_test.go index 05d08e69..207790df 100644 --- a/client_test.go +++ b/client_test.go @@ -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 29541515..b96faf06 100644 --- 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 {