Reinstate the reduce dial timeout and update some values

Fixes #263
This commit is contained in:
Matt Joiner 2018-07-10 11:19:14 +10:00
parent f149d0b82a
commit e3cb94006c
3 changed files with 30 additions and 20 deletions

View File

@ -597,7 +597,11 @@ func (cl *Client) establishOutgoingConnEx(t *Torrent, addr string, ctx context.C
// Returns nil connection and nil error if no connection could be established // Returns nil connection and nil error if no connection could be established
// for valid reasons. // for valid reasons.
func (cl *Client) establishOutgoingConn(t *Torrent, addr string) (c *connection, err error) { func (cl *Client) establishOutgoingConn(t *Torrent, addr string) (c *connection, err error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) ctx, cancel := context.WithTimeout(context.Background(), func() time.Duration {
cl.mu.RLock()
defer cl.mu.RUnlock()
return t.dialTimeout()
}())
defer cancel() defer cancel()
obfuscatedHeaderFirst := !cl.config.DisableEncryption && !cl.config.PreferNoEncryption obfuscatedHeaderFirst := !cl.config.DisableEncryption && !cl.config.PreferNoEncryption
c, err = cl.establishOutgoingConnEx(t, addr, ctx, obfuscatedHeaderFirst) c, err = cl.establishOutgoingConnEx(t, addr, ctx, obfuscatedHeaderFirst)

View File

@ -93,17 +93,19 @@ type ClientConfig struct {
// Also see `extendedHandshakeClientVersion`. // Also see `extendedHandshakeClientVersion`.
Bep20 string // default "-GT0001-" Bep20 string // default "-GT0001-"
NominalDialTimeout time.Duration // default time.Second * 30 // Peer dial timeout to use when there are limited peers.
MinDialTimeout time.Duration // default 5 * time.Second NominalDialTimeout time.Duration
EstablishedConnsPerTorrent int // default 80 // Minimum peer dial timeout to use (even if we have lots of peers).
HalfOpenConnsPerTorrent int // default 80 MinDialTimeout time.Duration
TorrentPeersHighWater int // default 200 EstablishedConnsPerTorrent int
TorrentPeersLowWater int // default 50 HalfOpenConnsPerTorrent int
TorrentPeersHighWater int
TorrentPeersLowWater int
// Limit how long handshake can take. This is to reduce the lingering // Limit how long handshake can take. This is to reduce the lingering
// impact of a few bad apples. 4s loses 1% of successful handshakes that // impact of a few bad apples. 4s loses 1% of successful handshakes that
// are obtained with 60s timeout, and 5% of unsuccessful handshakes. // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
HandshakesTimeout time.Duration // default 20 * time.Second HandshakesTimeout time.Duration
PublicIp4 net.IP PublicIp4 net.IP
PublicIp6 net.IP PublicIp6 net.IP
@ -134,13 +136,13 @@ func NewDefaultClientConfig() *ClientConfig {
HTTPUserAgent: DefaultHTTPUserAgent, HTTPUserAgent: DefaultHTTPUserAgent,
ExtendedHandshakeClientVersion: "go.torrent dev 20150624", ExtendedHandshakeClientVersion: "go.torrent dev 20150624",
Bep20: "-GT0001-", Bep20: "-GT0001-",
NominalDialTimeout: 30 * time.Second, NominalDialTimeout: 20 * time.Second,
MinDialTimeout: 5 * time.Second, MinDialTimeout: 5 * time.Second,
EstablishedConnsPerTorrent: 50, EstablishedConnsPerTorrent: 50,
HalfOpenConnsPerTorrent: 25, HalfOpenConnsPerTorrent: 25,
TorrentPeersHighWater: 500, TorrentPeersHighWater: 500,
TorrentPeersLowWater: 50, TorrentPeersLowWater: 50,
HandshakesTimeout: 20 * time.Second, HandshakesTimeout: 4 * time.Second,
DhtStartingNodes: dht.GlobalBootstrapAddrs, DhtStartingNodes: dht.GlobalBootstrapAddrs,
ListenHost: func(string) string { return "" }, ListenHost: func(string) string { return "" },
UploadRateLimiter: unlimited, UploadRateLimiter: unlimited,

View File

@ -1793,3 +1793,7 @@ func (t *Torrent) hashingPiece(i pieceIndex) bool {
func (t *Torrent) pieceQueuedForHash(i pieceIndex) bool { func (t *Torrent) pieceQueuedForHash(i pieceIndex) bool {
return t.piecesQueuedForHash.Get(bitmap.BitIndex(i)) return t.piecesQueuedForHash.Get(bitmap.BitIndex(i))
} }
func (t *Torrent) dialTimeout() time.Duration {
return reducedDialTimeout(t.cl.config.MinDialTimeout, t.cl.config.NominalDialTimeout, t.cl.halfOpenLimit, t.peers.Len())
}