1package anaconda
2
3import (
4	"time"
5
6	"github.com/azr/backoff"
7)
8
9/*
10Reconnecting(from https://developer.twitter.com/en/docs/tutorials/consuming-streaming-data) :
11
12Once an established connection drops, attempt to reconnect immediately.
13If the reconnect fails, slow down your reconnect attempts according to the type of error experienced:
14*/
15
16//Back off linearly for TCP/IP level network errors.
17//	These problems are generally temporary and tend to clear quickly.
18//	Increase the delay in reconnects by 250ms each attempt, up to 16 seconds.
19func NewTCPIPErrBackoff() backoff.Interface {
20	return backoff.NewLinear(0, time.Second*16, time.Millisecond*250, 1)
21}
22
23//Back off exponentially for HTTP errors for which reconnecting would be appropriate.
24//	Start with a 5 second wait, doubling each attempt, up to 320 seconds.
25func NewHTTPErrBackoff() backoff.Interface {
26	eb := backoff.NewExponential()
27	eb.InitialInterval = time.Second * 5
28	eb.MaxInterval = time.Second * 320
29	eb.Multiplier = 2
30	eb.Reset()
31	return eb
32}
33
34// Back off exponentially for HTTP 420 errors.
35// 	Start with a 1 minute wait and double each attempt.
36// 	Note that every HTTP 420 received increases the time you must
37// 	wait until rate limiting will no longer will be in effect for your account.
38func NewHTTP420ErrBackoff() backoff.Interface {
39	eb := backoff.NewExponential()
40	eb.InitialInterval = time.Minute * 1
41	eb.Multiplier = 2
42	eb.MaxInterval = time.Minute * 20
43	eb.Reset()
44	return eb
45}
46