Skip to content

Instantly share code, notes, and snippets.

@powerman
Created May 22, 2019 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save powerman/7e0ef1b72a031bbdbf7da7ade74640a7 to your computer and use it in GitHub Desktop.
Save powerman/7e0ef1b72a031bbdbf7da7ade74640a7 to your computer and use it in GitHub Desktop.
package expdelay
import "time"
// ExpDelay implements exponential delay.
type ExpDelay struct{ cur, max time.Duration }
// New returns new exponential delay which start with min delay, increase
// each next delay in 2 times up to max delay.
//
// for delay := expdelay.New(minDelay, maxDelay); ; delay.Sleep() {
// err := op()
// if err == nil {
// break
// }
// }
func New(min, max time.Duration) *ExpDelay {
return &ExpDelay{
cur: min,
max: max,
}
}
// Sleep will call time.Sleep using current delay.
func (d *ExpDelay) Sleep() {
time.Sleep(d.cur)
d.cur *= 2
if d.cur > d.max {
d.cur = d.max
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment