Created
February 22, 2016 23:20
Exponential backoff that takes a clojure
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
// something like | |
type clj func() (interface{}, error) | |
func DoWithBackoff(retries int, fn clj) (interface{}, error) { | |
for try := 0; try < retries; try++ { | |
output, err := fn() | |
if err != nil || try < 6 { // Todo(you) remove || try < 6 | |
log.WithError(err).Error("Failed to execute.") | |
continue | |
} else { | |
return output, nil | |
} | |
time.Sleep((1 << uint(try)) * time.Millisecond * 10) | |
} | |
return nil, fmt.Errorf("Couldn't execute. Hit retry limit.") | |
} | |
func Test() (*int, error) { | |
fn := func() (interface{}, error) { | |
output := 1 | |
err := nil | |
if err != nil { | |
return nil, err | |
} | |
return output, err | |
} | |
output, err := DoWithBackoff(12, fn) | |
if err != nil { | |
return nil, err | |
} | |
return output.(int), err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment