[ANN] Exponential Backoff Algorithm

592 views
Skip to first unread message

Cenk Altı

unread,
Feb 26, 2014, 12:50:42 AM2/26/14
to golan...@googlegroups.com

I have ported the exponential backoff algorithm from google-http-java-client with tests. Then replaced integer types with appropriate types from time package.

Also included a helper for retrying operation that may fail:

    operation := func() error {
        // An operation that migth fail
    }

    err := backoff.Retry(operation, NewExponentialBackoff())
    if err != nil {
        // handle error
    }

    // operation is successfull

1. The original code was under Apache 2 license but I have released it under MIT license. I don't know if it counts as a derivative work or completely new software. How should I license it?

2. Any suggestions on the API?

3. If you want to contribute you can implement a http.RoundTripper for retrying HTTP requests and send a pull request. I will be happy to accept. Or, you can add different back-off algorithms (LinearBackoff, vs.).

Dmitry Vyukov

unread,
Feb 26, 2014, 1:05:02 AM2/26/14
to Cenk Altı, golang-nuts
I don't like inversion of control, especially for simple/helper
things. For backoff I usually do something along the lines of:

for b := backoff.Exponential{}; b.Backoff(); {
whatever you want goes here
}

However, in the context of reusable Go API I would also consider
channels, because they can be involved in selects, eg:

var b backoff.Exponential
...
select {
case <-b.Backoff():
continue
case <-foo:
...
case <-bar:
...
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Ingo Oeser

unread,
Feb 26, 2014, 4:10:16 PM2/26/14
to golan...@googlegroups.com, Cenk Altı
Great patterns.

I was playing around with naive exponential backoff while prototyping retry helpers at http://godoc.org/github.com/nightlyone/reconnect
around an Reconnectable interface like http://play.golang.org/p/Sx87ug9BrM 

The idea is to use this reconnecting in requests or reconnect in the background to replenish a pool of connections.
Reply all
Reply to author
Forward
0 new messages