}
}
+func reducedDialTimeout(max time.Duration, halfOpenLimit int, pendingPeers int) time.Duration {
+ return max / time.Duration((pendingPeers+halfOpenLimit)/halfOpenLimit)
+}
+
// Start the process of connecting to the given peer for the given torrent if
// appropriate.
func (me *Client) initiateConn(peer Peer, t *torrent) {
// this address so that peers associate our local address with our
// listen address.
+ dialTimeout := reducedDialTimeout(dialTimeout, me.halfOpenLimit, len(t.Peers))
// Initiate connections via TCP and UTP simultaneously. Use the first
// one that succeeds.
left := 2
import (
"os"
"testing"
+ "time"
"bitbucket.org/anacrolix/go.torrent/testutil"
"bitbucket.org/anacrolix/go.torrent/util"
t.FailNow()
}
}
+
+func TestReducedDialTimeout(t *testing.T) {
+ for _, _case := range []struct {
+ Max time.Duration
+ HalfOpenLimit int
+ 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},
+ } {
+ reduced := reducedDialTimeout(_case.Max, _case.HalfOpenLimit, _case.PendingPeers)
+ if reduced != _case.ExpectedReduced {
+ t.Fatalf("expected %s, got %s", _case.ExpectedReduced, reduced)
+ }
+ }
+}