-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
reconnect.go
39 lines (33 loc) · 892 Bytes
/
reconnect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package centrifuge
import (
"time"
"github.com/jpillora/backoff"
)
type reconnectStrategy interface {
timeBeforeNextAttempt(attempt int) time.Duration
}
type backoffReconnect struct {
// Factor is the multiplying factor for each increment step.
Factor float64
// Jitter eases contention by randomizing backoff steps.
Jitter bool
// MinMilliseconds is a minimum value of reconnect interval.
MinDelay time.Duration
// MaxMilliseconds is a maximum value of reconnect interval.
MaxDelay time.Duration
}
var defaultBackoffReconnect = &backoffReconnect{
MinDelay: 200 * time.Millisecond,
MaxDelay: 20 * time.Second,
Factor: 2,
Jitter: true,
}
func (r *backoffReconnect) timeBeforeNextAttempt(attempt int) time.Duration {
b := &backoff.Backoff{
Min: r.MinDelay,
Max: r.MaxDelay,
Factor: r.Factor,
Jitter: r.Jitter,
}
return b.ForAttempt(float64(attempt))
}