From 937f84605b0815b4902315a801bfcc0011045b3d Mon Sep 17 00:00:00 2001 From: peted Date: Sat, 14 Nov 2015 23:10:39 +1000 Subject: [PATCH 1/2] fix reconnection logic --- irc.go | 22 +++++++++++----------- irc_struct.go | 3 ++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/irc.go b/irc.go index 652f327..ffa2ecd 100644 --- a/irc.go +++ b/irc.go @@ -67,7 +67,7 @@ func (irc *Connection) readLoop() { if err != nil { errChan <- err - break + return } if irc.Debug { @@ -129,8 +129,7 @@ func (irc *Connection) writeLoop() { select { case <-irc.end: return - default: - b, ok := <-irc.pwrite + case b, ok := <-irc.pwrite: if !ok || b == "" || irc.socket == nil { return } @@ -189,16 +188,13 @@ func (irc *Connection) pingLoop() { // Main loop to control the connection. func (irc *Connection) Loop() { errChan := irc.ErrorChan() - for !irc.stopped { + for !irc.quit { err := <-errChan - if irc.stopped { - break - } irc.Log.Printf("Error, disconnected: %s\n", err) - for !irc.stopped { + for !irc.quit { if err = irc.Reconnect(); err != nil { irc.Log.Printf("Error while reconnecting: %s\n", err) - time.Sleep(1 * time.Second) + time.Sleep(60 * time.Second) } else { break } @@ -217,6 +213,7 @@ func (irc *Connection) Quit() { irc.SendRaw(quit) irc.stopped = true + irc.quit = true } // Use the connection to join a given channel. @@ -369,6 +366,9 @@ func (irc *Connection) Disconnect() { // Reconnect to a server using the current connection. func (irc *Connection) Reconnect() error { if irc.end != nil { + for i := 0; i < 5; i++ { + irc.end <- 1 + } close(irc.end) } @@ -376,7 +376,7 @@ func (irc *Connection) Reconnect() error { irc.Wait() //make sure that wait group is cleared ensuring that all spawned goroutines have completed - irc.end = make(chan struct{}) + irc.end = make(chan int, 5) return irc.Connect(irc.Server) } @@ -464,7 +464,7 @@ func IRC(nick, user string) *Connection { nickcurrent: nick, user: user, Log: log.New(os.Stdout, "", log.LstdFlags), - end: make(chan struct{}), + end: make(chan int, 5), Version: VERSION, KeepAlive: 4 * time.Minute, Timeout: 1 * time.Minute, diff --git a/irc_struct.go b/irc_struct.go index fbba0ce..46d9112 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -27,7 +27,7 @@ type Connection struct { socket net.Conn pwrite chan string - end chan struct{} + end chan int nick string //The nickname we want. nickcurrent string //The nickname we currently have. @@ -42,6 +42,7 @@ type Connection struct { Log *log.Logger stopped bool + quit bool } // A struct to represent an event. From 5a16c2581e17f58a549c2abbefe4e0f7ea23720c Mon Sep 17 00:00:00 2001 From: peted Date: Sat, 14 Nov 2015 23:51:15 +1000 Subject: [PATCH 2/2] revert some changes but also update err chan for new connection so further errors are received in Loop --- irc.go | 8 +++----- irc_struct.go | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/irc.go b/irc.go index ffa2ecd..06917d5 100644 --- a/irc.go +++ b/irc.go @@ -196,6 +196,7 @@ func (irc *Connection) Loop() { irc.Log.Printf("Error while reconnecting: %s\n", err) time.Sleep(60 * time.Second) } else { + errChan = irc.ErrorChan() break } } @@ -366,9 +367,6 @@ func (irc *Connection) Disconnect() { // Reconnect to a server using the current connection. func (irc *Connection) Reconnect() error { if irc.end != nil { - for i := 0; i < 5; i++ { - irc.end <- 1 - } close(irc.end) } @@ -376,7 +374,7 @@ func (irc *Connection) Reconnect() error { irc.Wait() //make sure that wait group is cleared ensuring that all spawned goroutines have completed - irc.end = make(chan int, 5) + irc.end = make(chan struct{}) return irc.Connect(irc.Server) } @@ -464,7 +462,7 @@ func IRC(nick, user string) *Connection { nickcurrent: nick, user: user, Log: log.New(os.Stdout, "", log.LstdFlags), - end: make(chan int, 5), + end: make(chan struct{}), Version: VERSION, KeepAlive: 4 * time.Minute, Timeout: 1 * time.Minute, diff --git a/irc_struct.go b/irc_struct.go index 46d9112..fb301a1 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -27,7 +27,7 @@ type Connection struct { socket net.Conn pwrite chan string - end chan int + end chan struct{} nick string //The nickname we want. nickcurrent string //The nickname we currently have.