Skip to content

Commit f458ea1

Browse files
committed
Add MaxConnectRetryInterval client option for reconnect exponential backoff related to eclipse-paho#589
Signed-off-by: Daichi Tomaru <[email protected]>
1 parent 4b066a0 commit f458ea1

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

client.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,26 @@ func (c *client) Connect() Token {
255255
return
256256
}
257257

258+
connectRetryInterval := c.options.ConnectRetryInterval
258259
RETRYCONN:
259260
var conn net.Conn
260261
var rc byte
261262
var err error
262263
conn, rc, t.sessionPresent, err = c.attemptConnection()
263264
if err != nil {
264265
if c.options.ConnectRetry {
265-
DEBUG.Println(CLI, "Connect failed, sleeping for", int(c.options.ConnectRetryInterval.Seconds()), "seconds and will then retry, error:", err.Error())
266-
time.Sleep(c.options.ConnectRetryInterval)
266+
DEBUG.Println(CLI, "Connect failed, sleeping for", int(connectRetryInterval.Seconds()), "seconds and will then retry, error:", err.Error())
267+
time.Sleep(connectRetryInterval)
267268

268269
if c.status.ConnectionStatus() == connecting { // Possible connection aborted elsewhere
270+
if c.options.ConnectRetryInterval >= c.options.MaxConnectRetryInterval {
271+
goto RETRYCONN
272+
}
273+
if cri := connectRetryInterval * 2; cri <= c.options.MaxConnectRetryInterval {
274+
connectRetryInterval = cri
275+
} else {
276+
connectRetryInterval = c.options.MaxConnectRetryInterval
277+
}
269278
goto RETRYCONN
270279
}
271280
}

options.go

+10
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type ClientOptions struct {
8888
ConnectTimeout time.Duration
8989
MaxReconnectInterval time.Duration
9090
AutoReconnect bool
91+
MaxConnectRetryInterval time.Duration
9192
ConnectRetryInterval time.Duration
9293
ConnectRetry bool
9394
Store Store
@@ -136,6 +137,7 @@ func NewClientOptions() *ClientOptions {
136137
ConnectTimeout: 30 * time.Second,
137138
MaxReconnectInterval: 10 * time.Minute,
138139
AutoReconnect: true,
140+
MaxConnectRetryInterval: 30 * time.Second,
139141
ConnectRetryInterval: 30 * time.Second,
140142
ConnectRetry: false,
141143
Store: nil,
@@ -386,6 +388,14 @@ func (o *ClientOptions) SetAutoReconnect(a bool) *ClientOptions {
386388
return o
387389
}
388390

391+
// SetMaxConnectRetryInterval sets the maximum time that will be waited between connection attempts
392+
// when initially connecting if ConnectRetry is TRUE
393+
// If ConnectRetryInterval >= MaxConnectRetryInterval, ConnectRetryInterval is kept between the attempts
394+
func (o *ClientOptions) SetMaxConnectRetryInterval(t time.Duration) *ClientOptions {
395+
o.MaxConnectRetryInterval = t
396+
return o
397+
}
398+
389399
// SetConnectRetryInterval sets the time that will be waited between connection attempts
390400
// when initially connecting if ConnectRetry is TRUE
391401
func (o *ClientOptions) SetConnectRetryInterval(t time.Duration) *ClientOptions {

options_reader.go

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ func (r *ClientOptionsReader) AutoReconnect() bool {
137137
return s
138138
}
139139

140+
func (r *ClientOptionsReader) MaxConnectRetryInterval() time.Duration {
141+
s := r.options.MaxConnectRetryInterval
142+
return s
143+
}
144+
140145
// ConnectRetryInterval returns the delay between retries on the initial connection (if ConnectRetry true)
141146
func (r *ClientOptionsReader) ConnectRetryInterval() time.Duration {
142147
s := r.options.ConnectRetryInterval

0 commit comments

Comments
 (0)