client.go | 18 +++++++++--------- diff --git a/client.go b/client.go index 7b2c3c550d58ca86b509371c256890c720102c78..9941598d9c555e6b6da3970817f4b08f451f50e1 100644 --- a/client.go +++ b/client.go @@ -626,7 +626,13 @@ } // Returns nil connection and nil error if no connection could be established // for valid reasons. -func (cl *Client) establishOutgoingConnEx(t *Torrent, addr IpPort, ctx context.Context, obfuscatedHeader bool) (c *connection, err error) { +func (cl *Client) establishOutgoingConnEx(t *Torrent, addr IpPort, obfuscatedHeader bool) (c *connection, err error) { + ctx, cancel := context.WithTimeout(context.Background(), func() time.Duration { + cl.rLock() + defer cl.rUnlock() + return t.dialTimeout() + }()) + defer cancel() dr := cl.dialFirst(ctx, addr.String()) nc := dr.Conn if nc == nil { @@ -644,14 +650,8 @@ // Returns nil connection and nil error if no connection could be established // for valid reasons. func (cl *Client) establishOutgoingConn(t *Torrent, addr IpPort) (c *connection, err error) { torrent.Add("establish outgoing connection", 1) - ctx, cancel := context.WithTimeout(context.Background(), func() time.Duration { - cl.rLock() - defer cl.rUnlock() - return t.dialTimeout() - }()) - defer cancel() obfuscatedHeaderFirst := cl.config.HeaderObfuscationPolicy.Preferred - c, err = cl.establishOutgoingConnEx(t, addr, ctx, obfuscatedHeaderFirst) + c, err = cl.establishOutgoingConnEx(t, addr, obfuscatedHeaderFirst) if err != nil { //cl.logger.Printf("error establish connection to %s (obfuscatedHeader=%t): %v", addr, obfuscatedHeaderFirst, err) } @@ -665,7 +665,7 @@ // there's nothing else to try. return } // Try again with encryption if we didn't earlier, or without if we did. - c, err = cl.establishOutgoingConnEx(t, addr, ctx, !obfuscatedHeaderFirst) + c, err = cl.establishOutgoingConnEx(t, addr, !obfuscatedHeaderFirst) if c != nil { torrent.Add("initiated conn with fallback header obfuscation", 1) }