Sleep before first reconnect attempt

This commit is contained in:
Ken-Håvard Lieng 2020-05-05 01:35:05 +02:00
parent 530e08b9ee
commit 010bb6a102
2 changed files with 4 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"net" "net"
"strings" "strings"
"sync" "sync"
"time"
"github.com/jpillora/backoff" "github.com/jpillora/backoff"
) )
@ -54,6 +55,8 @@ func NewClient(nick, username string) *Client {
reconnect: make(chan struct{}), reconnect: make(chan struct{}),
recvBuf: make([]byte, 0, 4096), recvBuf: make([]byte, 0, 4096),
backoff: &backoff.Backoff{ backoff: &backoff.Backoff{
Min: 500 * time.Millisecond,
Max: 30 * time.Second,
Jitter: true, Jitter: true,
}, },
} }

View File

@ -79,6 +79,7 @@ func (c *Client) run() {
c.sendRecv.Wait() c.sendRecv.Wait()
c.reconnect = make(chan struct{}) c.reconnect = make(chan struct{})
time.Sleep(c.backoff.Duration())
c.tryConnect() c.tryConnect()
} }
} }