-
-
Save g1ibby/390f0e0f3ddae8e700a53d6fe7017419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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