backoff.go 608 B

1234567891011121314151617181920212223
  1. // Copyright 2016 Michal Witkowski. All Rights Reserved.
  2. // See LICENSE for licensing terms.
  3. /*
  4. Backoff Helper Utilities
  5. Implements common backoff features.
  6. */
  7. package backoffutils
  8. import (
  9. "math/rand"
  10. "time"
  11. )
  12. // JitterUp adds random jitter to the duration.
  13. //
  14. // This adds or substracts time from the duration within a given jitter fraction.
  15. // For example for 10s and jitter 0.1, it will returna time within [9s, 11s])
  16. func JitterUp(duration time.Duration, jitter float64) time.Duration {
  17. multiplier := jitter * (rand.Float64()*2 - 1)
  18. return time.Duration(float64(duration) * (1 + multiplier))
  19. }