Created
September 19, 2017 06:52
-
-
Save igungor/0c843269e7665cac6532172c74ea765c to your computer and use it in GitHub Desktop.
Exponential backoff without jitter (also without bloat)
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
// you dont need some fancy exponential backoff packages with unnecessary | |
// bloats. this is fine. | |
package main | |
import "time" | |
func main() { | |
backoff = time.Duration(0) | |
for { | |
backoff = exponential(backoff) | |
time.Sleep(backoff) | |
} | |
} | |
func exponential(d time.Duration) time.Duration { | |
const ( | |
min = 16 * time.Millisecond | |
max = 1024 * time.Millisecond | |
) | |
d *= 2 | |
if d < min { | |
d = min | |
} | |
if d > max { | |
d = max | |
} | |
return d | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment