Reduce dial timeouts when there are lots of peers in the backlog
This commit is contained in:
parent
d7764a5c23
commit
8cae78cbf1
|
@ -387,6 +387,10 @@ 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)
|
||||
}
|
||||
|
||||
// Start the process of connecting to the given peer for the given torrent if
|
||||
// appropriate.
|
||||
func (me *Client) initiateConn(peer Peer, t *torrent) {
|
||||
|
@ -405,6 +409,7 @@ 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
|
||||
|
|
|
@ -3,6 +3,7 @@ package torrent
|
|||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"bitbucket.org/anacrolix/go.torrent/testutil"
|
||||
"bitbucket.org/anacrolix/go.torrent/util"
|
||||
|
@ -75,3 +76,24 @@ func TestUnmarshalPEXMsg(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue