simplify dial timeout context

From the documentation:
WithDeadline returns a copy of the parent context with the deadline
adjusted to be no later than d. If the parent's deadline is already
earlier than d, WithDeadline(parent, d) is semantically equivalent to
parent.
This commit is contained in:
Marten Seemann 2021-09-18 16:36:27 +02:00
parent 3a5db3b2a3
commit a738e019b5
1 changed files with 3 additions and 6 deletions

View File

@ -121,12 +121,9 @@ func (t *TcpTransport) CanDial(addr ma.Multiaddr) bool {
func (t *TcpTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Conn, error) {
// Apply the deadline iff applicable
if t.ConnectTimeout > 0 {
deadline := time.Now().Add(t.ConnectTimeout)
if d, ok := ctx.Deadline(); !ok || deadline.Before(d) {
var cancel func()
ctx, cancel = context.WithDeadline(ctx, deadline)
defer cancel()
}
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, t.ConnectTimeout)
defer cancel()
}
if t.UseReuseport() {