Skip to content

Commit

Permalink
Revert "dial: return early on successful initial dial instead of bloc…
Browse files Browse the repository at this point in the history
…king via sleep unconditionally"

This reverts commit a42a134.
  • Loading branch information
Brandon Berhent committed Aug 11, 2022
1 parent 56edc4a commit f1a90d2
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions apps/client/websocket/recws.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ type RecConn struct {
// CloseAndReconnect will try to reconnect.
func (rc *RecConn) CloseAndReconnect() {
rc.Close()
connected := make(chan<- struct{})
defer close(connected)
go rc.connect(connected)
go rc.connect()
}

// setIsConnected sets state for isConnected
Expand Down Expand Up @@ -350,23 +348,10 @@ func (rc *RecConn) Dial(urlStr string, reqHeader http.Header) {
rc.setDefaultDialer(rc.getTLSClientConfig(), rc.getHandshakeTimeout())

// Connect
connected := make(chan struct{})
go rc.connect(connected)
defer close(connected)

// wait for first attempt, but only up to a point
timer := time.NewTimer(rc.getHandshakeTimeout())
defer timer.Stop()

// no default case means this select will block until one of these conditions is met.
// this is still guaranteed to complete, since the fallback here is the timer
select {
// in this case, the dial error is deferred until rc.GetDialError()
case <-timer.C:
return
case <-connected:
return
}
go rc.connect()

// wait on first attempt
time.Sleep(rc.getHandshakeTimeout())
}

// GetURL returns current connection url
Expand Down Expand Up @@ -452,7 +437,7 @@ func (rc *RecConn) keepAlive() {
}()
}

func (rc *RecConn) connect(connected chan<- struct{}) {
func (rc *RecConn) connect() {
b := rc.getBackoff()
rand.Seed(time.Now().UTC().UnixNano())

Expand Down Expand Up @@ -485,8 +470,6 @@ func (rc *RecConn) connect(connected chan<- struct{}) {
rc.keepAlive()
}

connected <- struct{}{}

return
}

Expand Down

0 comments on commit f1a90d2

Please sign in to comment.