Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions recws.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ type RecConn struct {
// CloseAndReconnect will try to reconnect.
func (rc *RecConn) CloseAndReconnect() {
rc.Close()
go rc.connect()
connected := make(chan<- struct{})
defer close(connected)
go rc.connect(connected)
}

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

// Connect
go rc.connect()

// wait on first attempt
time.Sleep(rc.getHandshakeTimeout())
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
}
}

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

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

Expand Down Expand Up @@ -421,7 +436,8 @@ func (rc *RecConn) connect() {
if rc.getKeepAliveTimeout() != 0 {
rc.keepAlive()
}


connected <- struct{}{}
return
}

Expand Down