File: README.md

package info (click to toggle)
golang-github-cloudflare-backoff 0.0~git20161212.647f3cd-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 100 kB
  • sloc: makefile: 2
file content (83 lines) | stat: -rw-r--r-- 2,381 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# backoff
## Go implementation of "Exponential Backoff And Jitter"

This package implements the backoff strategy described in the AWS
Architecture Blog article
["Exponential Backoff And Jitter"](http://www.awsarchitectureblog.com/2015/03/backoff.html). Essentially,
the backoff has an interval `time.Duration`; the *n<sup>th</sup>* call
to backoff will return an a `time.Duration` that is *2 <sup>n</sup> *
interval*. If jitter is enabled (which is the default behaviour), the
duration is a random value between 0 and *2 <sup>n</sup> * interval*.
The backoff is configured with a maximum duration that will not be
exceeded; e.g., by default, the longest duration returned is
`backoff.DefaultMaxDuration`.

## Usage

A `Backoff` is initialised with a call to `New`. Using zero values
causes it to use `DefaultMaxDuration` and `DefaultInterval` as the
maximum duration and interval.

```
package something

import "github.com/cloudflare/backoff"

func retryable() {
        b := backoff.New(0, 0)
        for {
                err := someOperation()
                if err == nil {
                    break
                }

                log.Printf("error in someOperation: %v", err)
                <-time.After(b.Duration())
        }

        log.Printf("succeeded after %d tries", b.Tries()+1)
        b.Reset()
}
```

It can also be used to rate limit code that should retry infinitely, but which does not
use `Backoff` itself.

```
package something

import (
    "time"

    "github.com/cloudflare/backoff"
)

func retryable() {
        b := backoff.New(0, 0)
        b.SetDecay(30 * time.Second)

        for {
                // b will reset if someOperation returns later than
                // the last call to b.Duration() + 30s.
                err := someOperation()
                if err == nil {
                    break
                }

                log.Printf("error in someOperation: %v", err)
                <-time.After(b.Duration())
        }
}
```

## Tunables

* `NewWithoutJitter` creates a Backoff that doesn't use jitter.

The default behaviour is controlled by two variables:

* `DefaultInterval` sets the base interval for backoffs created with
  the zero `time.Duration` value in the `Interval` field.
* `DefaultMaxDuration` sets the maximum duration for backoffs created
  with the zero `time.Duration` value in the `MaxDuration` field.