Skip to content

Commit

Permalink
Add MaxConnectRetryInterval client option for reconnect exponential b…
Browse files Browse the repository at this point in the history
…ackoff related to eclipse#589

Signed-off-by: Daichi Tomaru <banaoa7543@gmail.com>
  • Loading branch information
tomatod committed Dec 26, 2022
1 parent 4b066a0 commit f458ea1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions client.go
Expand Up @@ -255,17 +255,26 @@ func (c *client) Connect() Token {
return
}

connectRetryInterval := c.options.ConnectRetryInterval
RETRYCONN:
var conn net.Conn
var rc byte
var err error
conn, rc, t.sessionPresent, err = c.attemptConnection()
if err != nil {
if c.options.ConnectRetry {
DEBUG.Println(CLI, "Connect failed, sleeping for", int(c.options.ConnectRetryInterval.Seconds()), "seconds and will then retry, error:", err.Error())
time.Sleep(c.options.ConnectRetryInterval)
DEBUG.Println(CLI, "Connect failed, sleeping for", int(connectRetryInterval.Seconds()), "seconds and will then retry, error:", err.Error())
time.Sleep(connectRetryInterval)

if c.status.ConnectionStatus() == connecting { // Possible connection aborted elsewhere
if c.options.ConnectRetryInterval >= c.options.MaxConnectRetryInterval {
goto RETRYCONN
}
if cri := connectRetryInterval * 2; cri <= c.options.MaxConnectRetryInterval {
connectRetryInterval = cri
} else {
connectRetryInterval = c.options.MaxConnectRetryInterval
}
goto RETRYCONN
}
}
Expand Down
10 changes: 10 additions & 0 deletions options.go
Expand Up @@ -88,6 +88,7 @@ type ClientOptions struct {
ConnectTimeout time.Duration
MaxReconnectInterval time.Duration
AutoReconnect bool
MaxConnectRetryInterval time.Duration
ConnectRetryInterval time.Duration
ConnectRetry bool
Store Store
Expand Down Expand Up @@ -136,6 +137,7 @@ func NewClientOptions() *ClientOptions {
ConnectTimeout: 30 * time.Second,
MaxReconnectInterval: 10 * time.Minute,
AutoReconnect: true,
MaxConnectRetryInterval: 30 * time.Second,
ConnectRetryInterval: 30 * time.Second,
ConnectRetry: false,
Store: nil,
Expand Down Expand Up @@ -386,6 +388,14 @@ func (o *ClientOptions) SetAutoReconnect(a bool) *ClientOptions {
return o
}

// SetMaxConnectRetryInterval sets the maximum time that will be waited between connection attempts
// when initially connecting if ConnectRetry is TRUE
// If ConnectRetryInterval >= MaxConnectRetryInterval, ConnectRetryInterval is kept between the attempts
func (o *ClientOptions) SetMaxConnectRetryInterval(t time.Duration) *ClientOptions {
o.MaxConnectRetryInterval = t
return o
}

// SetConnectRetryInterval sets the time that will be waited between connection attempts
// when initially connecting if ConnectRetry is TRUE
func (o *ClientOptions) SetConnectRetryInterval(t time.Duration) *ClientOptions {
Expand Down
5 changes: 5 additions & 0 deletions options_reader.go
Expand Up @@ -137,6 +137,11 @@ func (r *ClientOptionsReader) AutoReconnect() bool {
return s
}

func (r *ClientOptionsReader) MaxConnectRetryInterval() time.Duration {
s := r.options.MaxConnectRetryInterval
return s
}

// ConnectRetryInterval returns the delay between retries on the initial connection (if ConnectRetry true)
func (r *ClientOptionsReader) ConnectRetryInterval() time.Duration {
s := r.options.ConnectRetryInterval
Expand Down

0 comments on commit f458ea1

Please sign in to comment.