1
- # backoff
1
+ # Exponential Backoff [ ![ GoDoc ] [ godoc image ]] [ godoc ] [ ![ Build Status ] [ travis image ]] [ travis ]
2
2
3
- [ ![ GoDoc] ( https://godoc.org/github.com/cenkalti/backoff?status.png )] ( https://godoc.org/github.com/cenkalti/backoff )
4
- [ ![ Build Status] ( https://travis-ci.org/cenkalti/backoff.png )] ( https://travis-ci.org/cenkalti/backoff )
3
+ This is a Go port of the exponential backoff algorithm from [ Google's HTTP Client Library for Java] [ google-http-java-client ] .
5
4
6
- This is a Go port of the exponential backoff algorithm from
7
- [ google-http-java-client] ( https://code.google.com/p/google-http-java-client/wiki/ExponentialBackoff ) .
8
-
9
- [ Exponential backoff] ( http://en.wikipedia.org/wiki/Exponential_backoff )
5
+ [ Exponential backoff] [ exponential backoff wiki ]
10
6
is an algorithm that uses feedback to multiplicatively decrease the rate of some process,
11
7
in order to gradually find an acceptable rate.
12
8
The retries exponentially increase and stop increasing when a certain threshold is met.
13
9
10
+ ## How To
14
11
12
+ We define two functions, ` Retry() ` and ` RetryNotify() ` .
13
+ They receive an ` Operation ` to execute, a ` BackOff ` algorithm,
14
+ and an optional ` Notify ` error handler.
15
15
16
+ The operation will be executed, and will be retried on failure with delay
17
+ as given by the backoff algorithm. The backoff algorithm can also decide when to stop
18
+ retrying.
19
+ In addition, the notify error handler will be called after each failed attempt,
20
+ except for the last time, whose error should be handled by the caller.
16
21
17
- ## Install
18
-
19
- ``` bash
20
- go get github.com/cenkalti/backoff
22
+ ``` go
23
+ // An Operation is executing by Retry() or RetryNotify().
24
+ // The operation will be retried using a backoff policy if it returns an error.
25
+ type Operation func () error
26
+
27
+ // Notify is a notify-on-error function. It receives an operation error and
28
+ // backoff delay if the operation failed (with an error).
29
+ //
30
+ // NOTE that if the backoff policy stated to stop retrying,
31
+ // the notify function isn't called.
32
+ type Notify func (error , time.Duration )
33
+
34
+ func Retry (Operation , BackOff ) error
35
+ func RetryNotify(Operation, BackOff, Notify)
21
36
```
22
37
23
- ## Example
38
+ ## Examples
24
39
25
- Simple retry helper that uses exponential back-off algorithm:
40
+ See more advanced examples in the [godoc][advanced example].
41
+
42
+ ### Retry
43
+
44
+ Simple retry helper that uses the default exponential backoff algorithm:
26
45
27
46
```go
28
47
operation := func() error {
29
- // An operation that might fail
48
+ // An operation that might fail.
49
+ return nil // or return errors.New("some error")
30
50
}
31
51
32
- err := backoff. Retry (operation, backoff. NewExponentialBackOff ())
52
+ err := Retry (operation, NewExponentialBackOff ())
33
53
if err != nil {
34
- // handle error
54
+ // Handle error.
55
+ return err
35
56
}
36
57
37
- // operation is successfull
58
+ // Operation is successful.
59
+ return nil
38
60
```
39
61
40
- Ticker example:
62
+ ### Ticker
41
63
42
64
``` go
43
65
operation := func () error {
44
- // An operation that may fail
66
+ // An operation that might fail
67
+ return nil // or return errors.New("some error")
45
68
}
46
69
47
- b := backoff. NewExponentialBackOff ()
48
- ticker := backoff. NewTicker (b)
70
+ b := NewExponentialBackOff ()
71
+ ticker := NewTicker (b)
49
72
50
73
var err error
51
74
52
75
// Ticks will continue to arrive when the previous operation is still running,
53
76
// so operations that take a while to fail could run in quick succession.
54
- for t = range ticker.C {
77
+ for range ticker.C {
55
78
if err = operation (); err != nil {
56
79
log.Println (err, " will retry..." )
57
80
continue
@@ -63,7 +86,31 @@ for t = range ticker.C {
63
86
64
87
if err != nil {
65
88
// Operation has failed.
89
+ return err
66
90
}
67
91
68
- // Operation is successfull.
92
+ // Operation is successful.
93
+ return nil
69
94
```
95
+
96
+ ## Getting Started
97
+
98
+ ``` bash
99
+ # install
100
+ $ go get github.com/cenkalti/backoff
101
+
102
+ # test
103
+ $ cd $GOPATH /src/github.com/cenkalti/backoff
104
+ $ go get -t ./...
105
+ $ go test -v -cover
106
+ ```
107
+
108
+ [ godoc ] : https://godoc.org/github.com/cenkalti/backoff
109
+ [ godoc image ] : https://godoc.org/github.com/cenkalti/backoff?status.png
110
+ [ travis ] : https://travis-ci.org/cenkalti/backoff
111
+ [ travis image ] : https://travis-ci.org/cenkalti/backoff.png
112
+
113
+ [ google-http-java-client ] : https://github.com/google/google-http-java-client
114
+ [ exponential backoff wiki ] : http://en.wikipedia.org/wiki/Exponential_backoff
115
+
116
+ [ advanced example ] : https://godoc.org/github.com/cenkalti/backoff#example_
0 commit comments