Skip to content

Commit 1fa753b

Browse files
committed
remove the Recovery limits
Signed-off-by: Gabriele Santomaggio <G.santomaggio@gmail.com>
1 parent d84c3d2 commit 1fa753b

3 files changed

Lines changed: 15 additions & 19 deletions

File tree

pkg/rabbitmqamqp/amqp_connection.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ func validateOptions(connOptions *AmqpConnOptions) (*AmqpConnOptions, error) {
227227
if connOptions.RecoveryConfiguration.MaxReconnectAttempts <= 0 && connOptions.RecoveryConfiguration.ActiveRecovery {
228228
return nil, fmt.Errorf("MaxReconnectAttempts should be greater than 0")
229229
}
230-
if connOptions.RecoveryConfiguration.BackOffReconnectInterval <= 1*time.Second && connOptions.RecoveryConfiguration.ActiveRecovery {
231-
return nil, fmt.Errorf("BackOffReconnectInterval should be greater than 1 second")
232-
}
233230

234231
return connOptions, nil
235232
}
@@ -325,9 +322,11 @@ func (a *AmqpConnection) maybeReconnect() {
325322

326323
///wait for before reconnecting
327324
// add some random milliseconds to the wait time to avoid thundering herd
328-
// the random time is between 0 and 500 milliseconds
329-
// Calculate delay with exponential backoff and jitter
330-
jitter := time.Duration(rand.Intn(500)) * time.Millisecond
325+
var jitter = time.Duration(0)
326+
if a.amqpConnOptions.RecoveryConfiguration.Jitter > 0 {
327+
jitter = time.Duration(rand.Int63n(int64(a.amqpConnOptions.RecoveryConfiguration.Jitter)))
328+
}
329+
331330
delay := baseDelay + jitter
332331
if delay > maxDelay {
333332
delay = maxDelay

pkg/rabbitmqamqp/amqp_connection_recovery.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ type RecoveryConfiguration struct {
2121
*/
2222
BackOffReconnectInterval time.Duration
2323

24+
/*
25+
Jitter used to add randomness to the reconnection interval.
26+
Default is 500 milliseconds.
27+
*/
28+
Jitter time.Duration
29+
2430
/*
2531
MaxReconnectAttempts The maximum number of reconnection attempts.
2632
Default is 5.
@@ -34,6 +40,7 @@ func (c *RecoveryConfiguration) Clone() *RecoveryConfiguration {
3440
ActiveRecovery: c.ActiveRecovery,
3541
BackOffReconnectInterval: c.BackOffReconnectInterval,
3642
MaxReconnectAttempts: c.MaxReconnectAttempts,
43+
Jitter: c.Jitter,
3744
}
3845

3946
return cloned
@@ -45,6 +52,7 @@ func NewRecoveryConfiguration() *RecoveryConfiguration {
4552
ActiveRecovery: true,
4653
BackOffReconnectInterval: 5 * time.Second,
4754
MaxReconnectAttempts: 5,
55+
Jitter: 500 * time.Millisecond,
4856
}
4957
}
5058

pkg/rabbitmqamqp/amqp_connection_recovery_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ var _ = Describe("Recovery connection test", func() {
2929
// reduced the reconnect interval to speed up the test
3030
RecoveryConfiguration: &RecoveryConfiguration{
3131
ActiveRecovery: true,
32-
BackOffReconnectInterval: 2 * time.Second,
32+
BackOffReconnectInterval: 1 * time.Second,
3333
MaxReconnectAttempts: 5,
34+
Jitter: 200 * time.Millisecond,
3435
},
3536
Id: "reconnect producers and consumers",
3637
})
@@ -178,18 +179,6 @@ var _ = Describe("Recovery connection test", func() {
178179
It("validate the Recovery connection parameters", func() {
179180

180181
_, err := Dial(context.Background(), "amqp://", &AmqpConnOptions{
181-
SASLType: amqp.SASLTypeAnonymous(),
182-
// reduced the reconnect interval to speed up the test
183-
RecoveryConfiguration: &RecoveryConfiguration{
184-
ActiveRecovery: true,
185-
BackOffReconnectInterval: 500 * time.Millisecond,
186-
MaxReconnectAttempts: 5,
187-
},
188-
})
189-
Expect(err).NotTo(BeNil())
190-
Expect(err.Error()).To(ContainSubstring("BackOffReconnectInterval should be greater than"))
191-
192-
_, err = Dial(context.Background(), "amqp://", &AmqpConnOptions{
193182
SASLType: amqp.SASLTypeAnonymous(),
194183
RecoveryConfiguration: &RecoveryConfiguration{
195184
ActiveRecovery: true,

0 commit comments

Comments
 (0)