-
Notifications
You must be signed in to change notification settings - Fork 246
/
backoff.go
45 lines (38 loc) · 1.43 KB
/
backoff.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
40
41
42
43
44
45
package anaconda
import (
"time"
"github.com/azr/backoff"
)
/*
Reconnecting(from https://developer.twitter.com/en/docs/tutorials/consuming-streaming-data) :
Once an established connection drops, attempt to reconnect immediately.
If the reconnect fails, slow down your reconnect attempts according to the type of error experienced:
*/
//Back off linearly for TCP/IP level network errors.
// These problems are generally temporary and tend to clear quickly.
// Increase the delay in reconnects by 250ms each attempt, up to 16 seconds.
func NewTCPIPErrBackoff() backoff.Interface {
return backoff.NewLinear(0, time.Second*16, time.Millisecond*250, 1)
}
//Back off exponentially for HTTP errors for which reconnecting would be appropriate.
// Start with a 5 second wait, doubling each attempt, up to 320 seconds.
func NewHTTPErrBackoff() backoff.Interface {
eb := backoff.NewExponential()
eb.InitialInterval = time.Second * 5
eb.MaxInterval = time.Second * 320
eb.Multiplier = 2
eb.Reset()
return eb
}
// Back off exponentially for HTTP 420 errors.
// Start with a 1 minute wait and double each attempt.
// Note that every HTTP 420 received increases the time you must
// wait until rate limiting will no longer will be in effect for your account.
func NewHTTP420ErrBackoff() backoff.Interface {
eb := backoff.NewExponential()
eb.InitialInterval = time.Minute * 1
eb.Multiplier = 2
eb.MaxInterval = time.Minute * 20
eb.Reset()
return eb
}