backoff.go 1.4 KB

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