Skip to content

Commit

Permalink
simplify dial timeout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Cooper committed May 8, 2018
1 parent 127dae0 commit 1ea1159
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions gumble/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,37 +133,37 @@ func DialWithDialer(dialer *net.Dialer, addr string, config *Config, tlsConfig *

go client.pingRoutine()

var deadline time.Time
if !dialer.Deadline.IsZero() {
deadline = dialer.Deadline
}
if dialer.Timeout > 0 {
diff := start.Add(dialer.Timeout)
if deadline.IsZero() || diff.Before(deadline) {
deadline = diff
var timeout <-chan time.Time
{
var deadline time.Time
if !dialer.Deadline.IsZero() {
deadline = dialer.Deadline
}
}

if !deadline.IsZero() {
timeout := deadline.Sub(start)
select {
case <-time.After(timeout):
client.Conn.Close()
return nil, errors.New("gumble: synchronization timeout")
case err := <-client.connect:
if err != nil {
client.Conn.Close()
return nil, err
if dialer.Timeout > 0 {
diff := start.Add(dialer.Timeout)
if deadline.IsZero() || diff.Before(deadline) {
deadline = diff
}
}
} else {
if err := <-client.connect; err != nil {
if !deadline.IsZero() {
timer := time.NewTimer(deadline.Sub(start))
defer timer.Stop()
timeout = timer.C
}
}

select {
case <-timeout:
client.Conn.Close()
return nil, errors.New("gumble: synchronization timeout")
case err := <-client.connect:
if err != nil {
client.Conn.Close()
return nil, err
}
}

return client, nil
return client, nil
}
}

// State returns the current state of the client.
Expand Down

0 comments on commit 1ea1159

Please sign in to comment.