Created
August 3, 2018 16:08
-
-
Save karrick/8b95e12dd14a0a47ededaf304cda5466 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 main | |
import "time" | |
func backoff(retries int, callback func() error) error { | |
var err error | |
delay := time.Millisecond | |
for { | |
err = callback() | |
if err == nil { | |
break | |
} | |
if retries == 0 { | |
break | |
} | |
retries-- | |
delay <<= 1 // multiply previous delay by 2 | |
time.Sleep(delay) | |
} | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment