From 809b9be4e4d748189698a2852e9011ff1f2eca98 Mon Sep 17 00:00:00 2001 From: soda Date: Sat, 21 Jun 2014 16:30:15 +0200 Subject: [PATCH 1/4] fixing reconnect --- irc.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/irc.go b/irc.go index a8fd459..18590fe 100644 --- a/irc.go +++ b/irc.go @@ -102,13 +102,16 @@ func (irc *Connection) readLoop() { // Loop to write to a connection. To be used as a goroutine. func (irc *Connection) writeLoop() { + irc.Log.Printf("writeLoop() created") defer irc.Done() for { select { case <-irc.end: + irc.Log.Printf("got irc.end, returning") return default: b, ok := <-irc.pwrite + irc.Log.Printf("got pwrite in writeLoop()") if !ok || b == "" || irc.socket == nil { return } @@ -171,12 +174,14 @@ func (irc *Connection) Loop() { if irc.stopped { break } - irc.Log.Printf("Error: %s\n", err) - irc.Disconnect() + irc.Log.Printf("Error, disconnected: %s\n", err) + //irc.Disconnect() for !irc.stopped { - if err = irc.Connect(irc.server); err != nil { - irc.Log.Printf("Error: %s\n", err) - time.Sleep(1 * time.Second) + irc.Log.Printf("Reconnecting in 3 seconds") + time.Sleep(3 * time.Second) + if err = irc.Reconnect(); err != nil { + irc.Log.Printf("Error while reconnecting: %s\n", err) + //time.Sleep(1 * time.Second) } else { break } @@ -280,6 +285,7 @@ func (irc *Connection) Mode(target string, modestring ...string) { // A disconnect sends all buffered messages (if possible), // stops all goroutines and then closes the socket. func (irc *Connection) Disconnect() { + fmt.Println("closing channels") close(irc.end) close(irc.pwrite) close(irc.pread) @@ -353,16 +359,17 @@ func (irc *Connection) Connect(server string) error { irc.pread = make(chan string, 10) irc.pwrite = make(chan string, 10) irc.Error = make(chan error, 2) - + irc.Log.Printf("Created channels") irc.Add(3) go irc.readLoop() go irc.writeLoop() go irc.pingLoop() - + irc.Log.Printf("created go routines") if len(irc.Password) > 0 { irc.pwrite <- fmt.Sprintf("PASS %s\r\n", irc.Password) } irc.pwrite <- fmt.Sprintf("NICK %s\r\n", irc.nick) + irc.Log.Printf("sent NICK") irc.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, irc.user) return nil } From 32cee7464ff18525773ce4299f56be70e14824e3 Mon Sep 17 00:00:00 2001 From: soda Date: Sat, 21 Jun 2014 16:46:47 +0200 Subject: [PATCH 2/4] fix reconnect, make irc.end --- irc.go | 1 + 1 file changed, 1 insertion(+) diff --git a/irc.go b/irc.go index 18590fe..2cc039e 100644 --- a/irc.go +++ b/irc.go @@ -302,6 +302,7 @@ func (irc *Connection) Disconnect() { // Reconnect to a server using the current connection. func (irc *Connection) Reconnect() error { + irc.end = make(chan struct{}) return irc.Connect(irc.server) } From 2a60a33ae9716b89a69cbf02684b42430056cb50 Mon Sep 17 00:00:00 2001 From: soda Date: Sat, 21 Jun 2014 16:53:24 +0200 Subject: [PATCH 3/4] removed debug-printfs --- irc.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/irc.go b/irc.go index 2cc039e..0af7186 100644 --- a/irc.go +++ b/irc.go @@ -102,16 +102,13 @@ func (irc *Connection) readLoop() { // Loop to write to a connection. To be used as a goroutine. func (irc *Connection) writeLoop() { - irc.Log.Printf("writeLoop() created") defer irc.Done() for { select { case <-irc.end: - irc.Log.Printf("got irc.end, returning") return default: b, ok := <-irc.pwrite - irc.Log.Printf("got pwrite in writeLoop()") if !ok || b == "" || irc.socket == nil { return } @@ -177,11 +174,9 @@ func (irc *Connection) Loop() { irc.Log.Printf("Error, disconnected: %s\n", err) //irc.Disconnect() for !irc.stopped { - irc.Log.Printf("Reconnecting in 3 seconds") - time.Sleep(3 * time.Second) if err = irc.Reconnect(); err != nil { irc.Log.Printf("Error while reconnecting: %s\n", err) - //time.Sleep(1 * time.Second) + time.Sleep(1 * time.Second) } else { break } @@ -285,7 +280,6 @@ func (irc *Connection) Mode(target string, modestring ...string) { // A disconnect sends all buffered messages (if possible), // stops all goroutines and then closes the socket. func (irc *Connection) Disconnect() { - fmt.Println("closing channels") close(irc.end) close(irc.pwrite) close(irc.pread) @@ -360,17 +354,14 @@ func (irc *Connection) Connect(server string) error { irc.pread = make(chan string, 10) irc.pwrite = make(chan string, 10) irc.Error = make(chan error, 2) - irc.Log.Printf("Created channels") irc.Add(3) go irc.readLoop() go irc.writeLoop() go irc.pingLoop() - irc.Log.Printf("created go routines") if len(irc.Password) > 0 { irc.pwrite <- fmt.Sprintf("PASS %s\r\n", irc.Password) } irc.pwrite <- fmt.Sprintf("NICK %s\r\n", irc.nick) - irc.Log.Printf("sent NICK") irc.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, irc.user) return nil } From 9328e327717d0a9d631c93864c5e734d815ee3fd Mon Sep 17 00:00:00 2001 From: soda Date: Sat, 21 Jun 2014 17:21:15 +0200 Subject: [PATCH 4/4] removed irc.Disconnect() comment --- irc.go | 1 - 1 file changed, 1 deletion(-) diff --git a/irc.go b/irc.go index 0af7186..1fc06fa 100644 --- a/irc.go +++ b/irc.go @@ -172,7 +172,6 @@ func (irc *Connection) Loop() { break } irc.Log.Printf("Error, disconnected: %s\n", err) - //irc.Disconnect() for !irc.stopped { if err = irc.Reconnect(); err != nil { irc.Log.Printf("Error while reconnecting: %s\n", err)