Wait 15 seconds before reconnecting if the connection closed before registration finished

This commit is contained in:
Ken-Håvard Lieng 2018-06-17 22:53:22 +02:00
parent 9669092148
commit fde6a9e630
2 changed files with 28 additions and 6 deletions

View File

@ -28,6 +28,7 @@ type Client struct {
conn net.Conn conn net.Conn
connected bool connected bool
registered bool
dialer *net.Dialer dialer *net.Dialer
reader *bufio.Reader reader *bufio.Reader
backoff *backoff.Backoff backoff *backoff.Backoff
@ -76,6 +77,19 @@ func (c *Client) Connected() bool {
return connected return connected
} }
func (c *Client) Registered() bool {
c.lock.Lock()
reg := c.registered
c.lock.Unlock()
return reg
}
func (c *Client) setRegistered(reg bool) {
c.lock.Lock()
c.registered = reg
c.lock.Unlock()
}
func (c *Client) Nick(nick string) { func (c *Client) Nick(nick string) {
c.Write("NICK " + nick) c.Write("NICK " + nick)
} }

View File

@ -60,6 +60,7 @@ func (c *Client) run() {
for { for {
select { select {
case <-c.quit: case <-c.quit:
c.setRegistered(false)
if c.Connected() { if c.Connected() {
c.disconnect() c.disconnect()
} }
@ -69,6 +70,7 @@ func (c *Client) run() {
return return
case <-c.reconnect: case <-c.reconnect:
c.setRegistered(false)
if c.Connected() { if c.Connected() {
c.disconnect() c.disconnect()
} }
@ -191,6 +193,11 @@ func (c *Client) recv() {
default: default:
c.connChange(false, nil) c.connChange(false, nil)
if !c.Registered() {
time.Sleep(15 * time.Second)
}
c.Reconnect() c.Reconnect()
return return
} }
@ -219,6 +226,7 @@ func (c *Client) recv() {
case ReplyWelcome: case ReplyWelcome:
c.setNick(msg.Params[0]) c.setNick(msg.Params[0])
c.setRegistered(true)
c.sendRecv.Add(1) c.sendRecv.Add(1)
go c.send() go c.send()