Created
May 22, 2019 21:43
-
-
Save powerman/7e0ef1b72a031bbdbf7da7ade74640a7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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