Store the backoff in the client struct

This commit is contained in:
Ken-Håvard Lieng 2016-01-13 01:00:57 +01:00
parent c22b7d2a1d
commit 83aef5df7b
2 changed files with 7 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/jpillora/backoff"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/matryer/resync" "github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/matryer/resync"
) )
@ -26,6 +27,7 @@ type Client struct {
connected bool connected bool
dialer *net.Dialer dialer *net.Dialer
reader *bufio.Reader reader *bufio.Reader
backoff *backoff.Backoff
out chan string out chan string
quit chan struct{} quit chan struct{}
@ -45,6 +47,9 @@ func NewClient(nick, username string) *Client {
out: make(chan string, 32), out: make(chan string, 32),
quit: make(chan struct{}), quit: make(chan struct{}),
reconnect: make(chan struct{}), reconnect: make(chan struct{}),
backoff: &backoff.Backoff{
Jitter: true,
},
} }
} }

View File

@ -7,8 +7,6 @@ import (
"net" "net"
"strings" "strings"
"time" "time"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/jpillora/backoff"
) )
func (c *Client) Connect(address string) { func (c *Client) Connect(address string) {
@ -84,10 +82,6 @@ func (c *Client) connect() error {
} }
func (c *Client) tryConnect() { func (c *Client) tryConnect() {
b := &backoff.Backoff{
Jitter: true,
}
for { for {
select { select {
case <-c.quit: case <-c.quit:
@ -98,10 +92,11 @@ func (c *Client) tryConnect() {
err := c.connect() err := c.connect()
if err == nil { if err == nil {
c.backoff.Reset()
return return
} }
time.Sleep(b.Duration()) time.Sleep(c.backoff.Duration())
} }
} }