Skip to content

Instantly share code, notes, and snippets.

@karrick
Created August 3, 2018 16:08
Show Gist options
  • Save karrick/8b95e12dd14a0a47ededaf304cda5466 to your computer and use it in GitHub Desktop.
Save karrick/8b95e12dd14a0a47ededaf304cda5466 to your computer and use it in GitHub Desktop.
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