Skip to content

Instantly share code, notes, and snippets.

@igungor
Created September 19, 2017 06:52
Show Gist options
  • Save igungor/0c843269e7665cac6532172c74ea765c to your computer and use it in GitHub Desktop.
Save igungor/0c843269e7665cac6532172c74ea765c to your computer and use it in GitHub Desktop.
Exponential backoff without jitter (also without bloat)
// 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