Skip to content

Commit 33a2857

Browse files
authored
Merge pull request #28 from p4u/master
add a close channel in order to finish the dial loop
2 parents c08788c + 1726c45 commit 33a2857

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

recws.go

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type RecConn struct {
5454
httpResp *http.Response
5555
dialErr error
5656
dialer *websocket.Dialer
57+
// if set to true, close stops dial reconnection
58+
close chan (bool)
5759

5860
*websocket.Conn
5961
}
@@ -87,7 +89,7 @@ func (rc *RecConn) Close() {
8789
rc.Conn.Close()
8890
rc.mu.Unlock()
8991
}
90-
92+
rc.close <- true
9193
rc.setIsConnected(false)
9294
}
9395

@@ -290,6 +292,7 @@ func (rc *RecConn) Dial(urlStr string, reqHeader http.Header) {
290292
log.Fatalf("Dial: %v", err)
291293
}
292294

295+
rc.close = make(chan bool, 1)
293296
// Config
294297
rc.setURL(urlStr)
295298
rc.setReqHeader(reqHeader)
@@ -394,43 +397,48 @@ func (rc *RecConn) connect() {
394397
rand.Seed(time.Now().UTC().UnixNano())
395398

396399
for {
397-
nextItvl := b.Duration()
398-
wsConn, httpResp, err := rc.dialer.Dial(rc.url, rc.reqHeader)
399-
400-
rc.mu.Lock()
401-
rc.Conn = wsConn
402-
rc.dialErr = err
403-
rc.isConnected = err == nil
404-
rc.httpResp = httpResp
405-
rc.mu.Unlock()
406-
407-
if err == nil {
408-
if !rc.getNonVerbose() {
409-
log.Printf("Dial: connection was successfully established with %s\n", rc.url)
410-
}
400+
select {
401+
case <-rc.close:
402+
return
403+
default:
404+
nextItvl := b.Duration()
405+
wsConn, httpResp, err := rc.dialer.Dial(rc.url, rc.reqHeader)
406+
407+
rc.mu.Lock()
408+
rc.Conn = wsConn
409+
rc.dialErr = err
410+
rc.isConnected = err == nil
411+
rc.httpResp = httpResp
412+
rc.mu.Unlock()
413+
414+
if err == nil {
415+
if !rc.getNonVerbose() {
416+
log.Printf("Dial: connection was successfully established with %s\n", rc.url)
417+
}
411418

412-
if rc.hasSubscribeHandler() {
413-
if err := rc.SubscribeHandler(); err != nil {
414-
log.Fatalf("Dial: connect handler failed with %s", err.Error())
419+
if rc.hasSubscribeHandler() {
420+
if err := rc.SubscribeHandler(); err != nil {
421+
log.Fatalf("Dial: connect handler failed with %s", err.Error())
422+
}
423+
if !rc.getNonVerbose() {
424+
log.Printf("Dial: connect handler was successfully established with %s\n", rc.url)
425+
}
415426
}
416-
if !rc.getNonVerbose() {
417-
log.Printf("Dial: connect handler was successfully established with %s\n", rc.url)
427+
428+
if rc.getKeepAliveTimeout() != 0 {
429+
rc.keepAlive()
418430
}
431+
432+
return
419433
}
420434

421-
if rc.getKeepAliveTimeout() != 0 {
422-
rc.keepAlive()
435+
if !rc.getNonVerbose() {
436+
log.Println(err)
437+
log.Println("Dial: will try again in", nextItvl, "seconds.")
423438
}
424-
425-
return
426-
}
427439

428-
if !rc.getNonVerbose() {
429-
log.Println(err)
430-
log.Println("Dial: will try again in", nextItvl, "seconds.")
440+
time.Sleep(nextItvl)
431441
}
432-
433-
time.Sleep(nextItvl)
434442
}
435443
}
436444

0 commit comments

Comments
 (0)