From 83aef5df7baf6910ee27fe427de0e1f59f7b9a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Wed, 13 Jan 2016 01:00:57 +0100 Subject: [PATCH] Store the backoff in the client struct --- irc/client.go | 5 +++++ irc/conn.go | 9 ++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/irc/client.go b/irc/client.go index 2a96704e..24111bc4 100644 --- a/irc/client.go +++ b/irc/client.go @@ -7,6 +7,7 @@ import ( "strings" "sync" + "github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/jpillora/backoff" "github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/matryer/resync" ) @@ -26,6 +27,7 @@ type Client struct { connected bool dialer *net.Dialer reader *bufio.Reader + backoff *backoff.Backoff out chan string quit chan struct{} @@ -45,6 +47,9 @@ func NewClient(nick, username string) *Client { out: make(chan string, 32), quit: make(chan struct{}), reconnect: make(chan struct{}), + backoff: &backoff.Backoff{ + Jitter: true, + }, } } diff --git a/irc/conn.go b/irc/conn.go index 73f1a2c0..b599c7b1 100644 --- a/irc/conn.go +++ b/irc/conn.go @@ -7,8 +7,6 @@ import ( "net" "strings" "time" - - "github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/jpillora/backoff" ) func (c *Client) Connect(address string) { @@ -84,10 +82,6 @@ func (c *Client) connect() error { } func (c *Client) tryConnect() { - b := &backoff.Backoff{ - Jitter: true, - } - for { select { case <-c.quit: @@ -98,10 +92,11 @@ func (c *Client) tryConnect() { err := c.connect() if err == nil { + c.backoff.Reset() return } - time.Sleep(b.Duration()) + time.Sleep(c.backoff.Duration()) } }