diff --git a/vendor/github.com/thoj/go-ircevent/irc.go b/vendor/github.com/thoj/go-ircevent/irc.go index d59dc6fe..eb9174d3 100644 --- a/vendor/github.com/thoj/go-ircevent/irc.go +++ b/vendor/github.com/thoj/go-ircevent/irc.go @@ -74,7 +74,9 @@ func (irc *Connection) readLoop() { irc.Log.Printf("<-- %s\n", strings.TrimSpace(msg)) } + irc.Lock() irc.lastMessage = time.Now() + irc.Unlock() event, err := parseToEvent(msg) event.Connection = irc if err == nil { @@ -171,10 +173,12 @@ func (irc *Connection) pingLoop() { //Ping at the ping frequency irc.SendRawf("PING %d", time.Now().UnixNano()) //Try to recapture nickname if it's not as configured. + irc.Lock() if irc.nick != irc.nickcurrent { irc.nickcurrent = irc.nick irc.SendRawf("NICK %s", irc.nick) } + irc.Unlock() case <-irc.end: ticker.Stop() ticker2.Stop() @@ -183,13 +187,21 @@ func (irc *Connection) pingLoop() { } } +func (irc *Connection) isQuitting() bool { + irc.Lock() + defer irc.Unlock() + return irc.quit +} + // Main loop to control the connection. func (irc *Connection) Loop() { errChan := irc.ErrorChan() - for !irc.quit { + for !irc.isQuitting() { err := <-errChan - irc.Log.Printf("Error, disconnected: %s\n", err) - for !irc.quit { + close(irc.end) + irc.Wait() + for !irc.isQuitting() { + irc.Log.Printf("Error, disconnected: %s\n", err) if err = irc.Reconnect(); err != nil { irc.Log.Printf("Error while reconnecting: %s\n", err) time.Sleep(60 * time.Second) @@ -211,8 +223,10 @@ func (irc *Connection) Quit() { } irc.SendRaw(quit) + irc.Lock() irc.stopped = true irc.quit = true + irc.Unlock() } // Use the connection to join a given channel. @@ -341,37 +355,14 @@ func (irc *Connection) Connected() bool { // A disconnect sends all buffered messages (if possible), // stops all goroutines and then closes the socket. func (irc *Connection) Disconnect() { - for event := range irc.events { - irc.ClearCallback(event) - } - if irc.end != nil { - close(irc.end) - } - - irc.end = nil - - if irc.pwrite != nil { - close(irc.pwrite) - } - - irc.Wait() if irc.socket != nil { irc.socket.Close() } - irc.socket = nil irc.ErrorChan() <- ErrDisconnected } // Reconnect to a server using the current connection. func (irc *Connection) Reconnect() error { - if irc.end != nil { - close(irc.end) - } - - irc.end = nil - - irc.Wait() //make sure that wait group is cleared ensuring that all spawned goroutines have completed - irc.end = make(chan struct{}) return irc.Connect(irc.Server) } @@ -427,7 +418,7 @@ func (irc *Connection) Connect(server string) error { } irc.stopped = false - //irc.Log.Printf("Connected to %s (%s)\n", irc.Server, irc.socket.RemoteAddr()) + irc.Log.Printf("Connected to %s (%s)\n", irc.Server, irc.socket.RemoteAddr()) irc.pwrite = make(chan string, 10) irc.Error = make(chan error, 2) diff --git a/vendor/github.com/thoj/go-ircevent/irc_callback.go b/vendor/github.com/thoj/go-ircevent/irc_callback.go index b5622367..d389f731 100644 --- a/vendor/github.com/thoj/go-ircevent/irc_callback.go +++ b/vendor/github.com/thoj/go-ircevent/irc_callback.go @@ -136,9 +136,8 @@ func (irc *Connection) RunCallbacks(event *Event) { func (irc *Connection) setupCallbacks() { irc.events = make(map[string]map[int]func(*Event)) - //Handle error events. This has to be called in a new thred to allow - //readLoop to exit - irc.AddCallback("ERROR", func(e *Event) { go irc.Disconnect() }) + //Handle error events. + irc.AddCallback("ERROR", func(e *Event) { irc.Disconnect() }) //Handle ping events irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message()) }) @@ -201,7 +200,7 @@ func (irc *Connection) setupCallbacks() { ns, _ := strconv.ParseInt(e.Message(), 10, 64) delta := time.Duration(time.Now().UnixNano() - ns) if irc.Debug { - irc.Log.Printf("Lag: %vs\n", delta) + irc.Log.Printf("Lag: %.3f s\n", delta.Seconds()) } }) @@ -216,6 +215,8 @@ func (irc *Connection) setupCallbacks() { // 1: RPL_WELCOME "Welcome to the Internet Relay Network !@" // Set irc.nickcurrent to the actually used nick in this connection. irc.AddCallback("001", func(e *Event) { + irc.Lock() irc.nickcurrent = e.Arguments[0] + irc.Unlock() }) } diff --git a/vendor/github.com/thoj/go-ircevent/irc_struct.go b/vendor/github.com/thoj/go-ircevent/irc_struct.go index 33db846e..a188d9d9 100644 --- a/vendor/github.com/thoj/go-ircevent/irc_struct.go +++ b/vendor/github.com/thoj/go-ircevent/irc_struct.go @@ -13,6 +13,7 @@ import ( ) type Connection struct { + sync.Mutex sync.WaitGroup Debug bool Error chan error @@ -46,7 +47,7 @@ type Connection struct { Log *log.Logger stopped bool - quit bool + quit bool //User called Quit, do not reconnect. } // A struct to represent an event. diff --git a/vendor/manifest b/vendor/manifest index 18543d64..bf30edbe 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -186,7 +186,7 @@ "importpath": "github.com/thoj/go-ircevent", "repository": "https://github.com/thoj/go-ircevent", "vcs": "git", - "revision": "98c1902dd2097f38142384167e60206ba26f1585", + "revision": "1b0acb5f2f1b615cfbd4b9f91abb14cb39a18769", "branch": "master", "notests": true },