Skip to content

add a close channel in order to finish the dial loop #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2020
Merged
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
68 changes: 38 additions & 30 deletions recws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -87,7 +89,7 @@ func (rc *RecConn) Close() {
rc.Conn.Close()
rc.mu.Unlock()
}

rc.close <- true
rc.setIsConnected(false)
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -394,43 +397,48 @@ func (rc *RecConn) connect() {
rand.Seed(time.Now().UTC().UnixNano())

for {
nextItvl := b.Duration()
wsConn, httpResp, err := rc.dialer.Dial(rc.url, rc.reqHeader)

rc.mu.Lock()
rc.Conn = wsConn
rc.dialErr = err
rc.isConnected = err == nil
rc.httpResp = httpResp
rc.mu.Unlock()

if err == nil {
if !rc.getNonVerbose() {
log.Printf("Dial: connection was successfully established with %s\n", rc.url)
}
select {
case <-rc.close:
return
default:
nextItvl := b.Duration()
wsConn, httpResp, err := rc.dialer.Dial(rc.url, rc.reqHeader)

rc.mu.Lock()
rc.Conn = wsConn
rc.dialErr = err
rc.isConnected = err == nil
rc.httpResp = httpResp
rc.mu.Unlock()

if err == nil {
if !rc.getNonVerbose() {
log.Printf("Dial: connection was successfully established with %s\n", rc.url)
}

if rc.hasSubscribeHandler() {
if err := rc.SubscribeHandler(); err != nil {
log.Fatalf("Dial: connect handler failed with %s", err.Error())
if rc.hasSubscribeHandler() {
if err := rc.SubscribeHandler(); err != nil {
log.Fatalf("Dial: connect handler failed with %s", err.Error())
}
if !rc.getNonVerbose() {
log.Printf("Dial: connect handler was successfully established with %s\n", rc.url)
}
}
if !rc.getNonVerbose() {
log.Printf("Dial: connect handler was successfully established with %s\n", rc.url)

if rc.getKeepAliveTimeout() != 0 {
rc.keepAlive()
}

return
}

if rc.getKeepAliveTimeout() != 0 {
rc.keepAlive()
if !rc.getNonVerbose() {
log.Println(err)
log.Println("Dial: will try again in", nextItvl, "seconds.")
}

return
}

if !rc.getNonVerbose() {
log.Println(err)
log.Println("Dial: will try again in", nextItvl, "seconds.")
time.Sleep(nextItvl)
}

time.Sleep(nextItvl)
}
}

Expand Down