fix reconnection logic
This commit is contained in:
parent
db3338ebd4
commit
937f84605b
22
irc.go
22
irc.go
@ -67,7 +67,7 @@ func (irc *Connection) readLoop() {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- err
|
errChan <- err
|
||||||
break
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if irc.Debug {
|
if irc.Debug {
|
||||||
@ -129,8 +129,7 @@ func (irc *Connection) writeLoop() {
|
|||||||
select {
|
select {
|
||||||
case <-irc.end:
|
case <-irc.end:
|
||||||
return
|
return
|
||||||
default:
|
case b, ok := <-irc.pwrite:
|
||||||
b, ok := <-irc.pwrite
|
|
||||||
if !ok || b == "" || irc.socket == nil {
|
if !ok || b == "" || irc.socket == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -189,16 +188,13 @@ func (irc *Connection) pingLoop() {
|
|||||||
// Main loop to control the connection.
|
// Main loop to control the connection.
|
||||||
func (irc *Connection) Loop() {
|
func (irc *Connection) Loop() {
|
||||||
errChan := irc.ErrorChan()
|
errChan := irc.ErrorChan()
|
||||||
for !irc.stopped {
|
for !irc.quit {
|
||||||
err := <-errChan
|
err := <-errChan
|
||||||
if irc.stopped {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
irc.Log.Printf("Error, disconnected: %s\n", err)
|
irc.Log.Printf("Error, disconnected: %s\n", err)
|
||||||
for !irc.stopped {
|
for !irc.quit {
|
||||||
if err = irc.Reconnect(); err != nil {
|
if err = irc.Reconnect(); err != nil {
|
||||||
irc.Log.Printf("Error while reconnecting: %s\n", err)
|
irc.Log.Printf("Error while reconnecting: %s\n", err)
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(60 * time.Second)
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -217,6 +213,7 @@ func (irc *Connection) Quit() {
|
|||||||
|
|
||||||
irc.SendRaw(quit)
|
irc.SendRaw(quit)
|
||||||
irc.stopped = true
|
irc.stopped = true
|
||||||
|
irc.quit = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the connection to join a given channel.
|
// Use the connection to join a given channel.
|
||||||
@ -369,6 +366,9 @@ func (irc *Connection) Disconnect() {
|
|||||||
// Reconnect to a server using the current connection.
|
// Reconnect to a server using the current connection.
|
||||||
func (irc *Connection) Reconnect() error {
|
func (irc *Connection) Reconnect() error {
|
||||||
if irc.end != nil {
|
if irc.end != nil {
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
irc.end <- 1
|
||||||
|
}
|
||||||
close(irc.end)
|
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.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)
|
return irc.Connect(irc.Server)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -464,7 +464,7 @@ func IRC(nick, user string) *Connection {
|
|||||||
nickcurrent: nick,
|
nickcurrent: nick,
|
||||||
user: user,
|
user: user,
|
||||||
Log: log.New(os.Stdout, "", log.LstdFlags),
|
Log: log.New(os.Stdout, "", log.LstdFlags),
|
||||||
end: make(chan struct{}),
|
end: make(chan int, 5),
|
||||||
Version: VERSION,
|
Version: VERSION,
|
||||||
KeepAlive: 4 * time.Minute,
|
KeepAlive: 4 * time.Minute,
|
||||||
Timeout: 1 * time.Minute,
|
Timeout: 1 * time.Minute,
|
||||||
|
@ -27,7 +27,7 @@ type Connection struct {
|
|||||||
|
|
||||||
socket net.Conn
|
socket net.Conn
|
||||||
pwrite chan string
|
pwrite chan string
|
||||||
end chan struct{}
|
end chan int
|
||||||
|
|
||||||
nick string //The nickname we want.
|
nick string //The nickname we want.
|
||||||
nickcurrent string //The nickname we currently have.
|
nickcurrent string //The nickname we currently have.
|
||||||
@ -42,6 +42,7 @@ type Connection struct {
|
|||||||
Log *log.Logger
|
Log *log.Logger
|
||||||
|
|
||||||
stopped bool
|
stopped bool
|
||||||
|
quit bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// A struct to represent an event.
|
// A struct to represent an event.
|
||||||
|
Loading…
Reference in New Issue
Block a user