From fde6a9e630d1a5fc8659d36fd44d47fb7d139518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Sun, 17 Jun 2018 22:53:22 +0200 Subject: [PATCH] Wait 15 seconds before reconnecting if the connection closed before registration finished --- pkg/irc/client.go | 26 ++++++++++++++++++++------ pkg/irc/conn.go | 8 ++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/pkg/irc/client.go b/pkg/irc/client.go index 07938df7..d7a68ea0 100644 --- a/pkg/irc/client.go +++ b/pkg/irc/client.go @@ -26,12 +26,13 @@ type Client struct { channels []string Support *iSupport - conn net.Conn - connected bool - dialer *net.Dialer - reader *bufio.Reader - backoff *backoff.Backoff - out chan string + conn net.Conn + connected bool + registered bool + dialer *net.Dialer + reader *bufio.Reader + backoff *backoff.Backoff + out chan string quit chan struct{} reconnect chan struct{} @@ -76,6 +77,19 @@ func (c *Client) Connected() bool { 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) { c.Write("NICK " + nick) } diff --git a/pkg/irc/conn.go b/pkg/irc/conn.go index 4319deb5..3257fed7 100644 --- a/pkg/irc/conn.go +++ b/pkg/irc/conn.go @@ -60,6 +60,7 @@ func (c *Client) run() { for { select { case <-c.quit: + c.setRegistered(false) if c.Connected() { c.disconnect() } @@ -69,6 +70,7 @@ func (c *Client) run() { return case <-c.reconnect: + c.setRegistered(false) if c.Connected() { c.disconnect() } @@ -191,6 +193,11 @@ func (c *Client) recv() { default: c.connChange(false, nil) + + if !c.Registered() { + time.Sleep(15 * time.Second) + } + c.Reconnect() return } @@ -219,6 +226,7 @@ func (c *Client) recv() { case ReplyWelcome: c.setNick(msg.Params[0]) + c.setRegistered(true) c.sendRecv.Add(1) go c.send()