Changed the way Quit/Disconnect works to avoid possible DATA races.
Rewrote tests to be less spammy. Created REconnection test.
This commit is contained in:
39
irc.go
39
irc.go
@@ -183,13 +183,20 @@ 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 {
|
||||
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 +218,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 +350,15 @@ 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
|
||||
close(irc.end)
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user