Skip to content

Commit fc9bf51

Browse files
authored
Allow configuring min/max delays for backoff strategies (#117)
Added MinReconnectDelay and MaxReconnectDelay fields to Config struct for controlling client reconnection timing Added MinResubscribeDelay and MaxResubscribeDelay fields to SubscriptionConfig for controlling subscription retry timing
1 parent e0ccf72 commit fc9bf51

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func newClient(endpoint string, isProtobuf bool, config Config) *Client {
188188
subs: make(map[string]*Subscription),
189189
serverSubs: make(map[string]*serverSub),
190190
requests: make(map[uint32]request),
191-
reconnectStrategy: defaultBackoffReconnect,
191+
reconnectStrategy: newBackoffReconnect(config.MinReconnectDelay, config.MaxReconnectDelay),
192192
delayPing: make(chan struct{}, 32),
193193
events: newEventHub(),
194194
connectFutures: make(map[uint64]connectFuture),

config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ type Config struct {
5151
// MaxServerPingDelay used to set maximum delay of ping from server.
5252
// Zero value means 10 * time.Second.
5353
MaxServerPingDelay time.Duration
54+
// MinReconnectDelay is the minimum delay between reconnection attempts.
55+
// This delay is jittered.
56+
// Zero value means 200 * time.Millisecond.
57+
MinReconnectDelay time.Duration
58+
// MaxReconnectDelay is the maximum delay between reconnection attempts.
59+
// Zero value means 20 * time.Second.
60+
MaxReconnectDelay time.Duration
5461
// TLSConfig specifies the TLS configuration to use with tls.Client.
5562
// If nil, the default configuration is used.
5663
TLSConfig *tls.Config

reconnect.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ type backoffReconnect struct {
2121
MaxDelay time.Duration
2222
}
2323

24-
var defaultBackoffReconnect = &backoffReconnect{
25-
MinDelay: 200 * time.Millisecond,
26-
MaxDelay: 20 * time.Second,
27-
Factor: 2,
28-
Jitter: true,
29-
}
30-
3124
func (r *backoffReconnect) timeBeforeNextAttempt(attempt int) time.Duration {
3225
b := &backoff.Backoff{
3326
Min: r.MinDelay,
@@ -37,3 +30,20 @@ func (r *backoffReconnect) timeBeforeNextAttempt(attempt int) time.Duration {
3730
}
3831
return b.ForAttempt(float64(attempt))
3932
}
33+
34+
// newBackoffReconnect creates a new backoff reconnect strategy with custom min and max delays.
35+
// If minDelay or maxDelay is zero, it uses the default values.
36+
func newBackoffReconnect(minDelay, maxDelay time.Duration) reconnectStrategy {
37+
if minDelay == 0 {
38+
minDelay = 200 * time.Millisecond
39+
}
40+
if maxDelay == 0 {
41+
maxDelay = 20 * time.Second
42+
}
43+
return &backoffReconnect{
44+
MinDelay: minDelay,
45+
MaxDelay: maxDelay,
46+
Factor: 2,
47+
Jitter: true,
48+
}
49+
}

subscription.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,30 @@ type SubscriptionConfig struct {
5050
JoinLeave bool
5151
// Delta allows to specify delta type for the subscription. By default, no delta is used.
5252
Delta DeltaType
53+
// MinResubscribeDelay is the minimum delay between resubscription attempts.
54+
// This delay is jittered.
55+
// Zero value means 200 * time.Millisecond.
56+
MinResubscribeDelay time.Duration
57+
// MaxResubscribeDelay is the maximum delay between resubscription attempts.
58+
// Zero value means 20 * time.Second.
59+
MaxResubscribeDelay time.Duration
5360
}
5461

5562
func newSubscription(c *Client, channel string, config ...SubscriptionConfig) *Subscription {
63+
var resubscribeStrategy reconnectStrategy
64+
var minResubscribeDelay, maxResubscribeDelay time.Duration
65+
if len(config) == 1 {
66+
minResubscribeDelay = config[0].MinResubscribeDelay
67+
maxResubscribeDelay = config[0].MaxResubscribeDelay
68+
}
69+
resubscribeStrategy = newBackoffReconnect(minResubscribeDelay, maxResubscribeDelay)
5670
s := &Subscription{
5771
Channel: channel,
5872
centrifuge: c,
5973
state: SubStateUnsubscribed,
6074
events: newSubscriptionEventHub(),
6175
subFutures: make(map[uint64]subFuture),
62-
resubscribeStrategy: defaultBackoffReconnect,
76+
resubscribeStrategy: resubscribeStrategy,
6377
}
6478
if len(config) == 1 {
6579
cfg := config[0]

0 commit comments

Comments
 (0)