add a close channel in order to finish the dial loop
before, connect() would keep iterating fater connection close Signed-off-by: p4u <pau@dabax.net>
This commit is contained in:
@@ -54,6 +54,8 @@ type RecConn struct {
|
||||
httpResp *http.Response
|
||||
dialErr error
|
||||
dialer *websocket.Dialer
|
||||
// if set to true, close stops dial reconnection
|
||||
close chan (bool)
|
||||
|
||||
*websocket.Conn
|
||||
}
|
||||
@@ -87,7 +89,7 @@ func (rc *RecConn) Close() {
|
||||
rc.Conn.Close()
|
||||
rc.mu.Unlock()
|
||||
}
|
||||
|
||||
rc.close <- true
|
||||
rc.setIsConnected(false)
|
||||
}
|
||||
|
||||
@@ -290,6 +292,7 @@ func (rc *RecConn) Dial(urlStr string, reqHeader http.Header) {
|
||||
log.Fatalf("Dial: %v", err)
|
||||
}
|
||||
|
||||
rc.close = make(chan bool, 1)
|
||||
// Config
|
||||
rc.setURL(urlStr)
|
||||
rc.setReqHeader(reqHeader)
|
||||
@@ -394,6 +397,10 @@ func (rc *RecConn) connect() {
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-rc.close:
|
||||
return
|
||||
default:
|
||||
nextItvl := b.Duration()
|
||||
wsConn, httpResp, err := rc.dialer.Dial(rc.url, rc.reqHeader)
|
||||
|
||||
@@ -433,6 +440,7 @@ func (rc *RecConn) connect() {
|
||||
time.Sleep(nextItvl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetHTTPResponse returns the http response from the handshake.
|
||||
// Useful when WebSocket handshake fails,
|
||||
|
||||
Reference in New Issue
Block a user