This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
retry.go
64 lines (55 loc) · 1.59 KB
/
retry.go
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
package retry
import (
"time"
"github.com/juju/errgo"
)
// Do performs the given operation. Based on the options, it can retry the operation,
// if it failed.
//
// The following options are supported:
// * RetryChecker(func(err error) bool) - If this func returns true for the returned error, the operation is tried again
// * MaxTries(int) - Maximum number of calls to op() before aborting with MaxRetriesReached
// * Timeout(time.Duration) - Maximum number of time to try to perform this op before aborting with TimeoutReached
// * Sleep(time.Duration) - time to sleep after error failed op()
//
// Defaults:
// Timeout = 15 seconds
// MaxRetries = 5
// Retryer = errgo.Any
// Sleep = No sleep
//
func Do(op func() error, retryOptions ...RetryOption) error {
options := newRetryOptions(retryOptions...)
var timeout <-chan time.Time
if options.Timeout > 0 {
timeout = time.After(options.Timeout)
}
tryCounter := 0
for {
// Check if we reached the timeout
select {
case <-timeout:
return errgo.Mask(TimeoutError, errgo.Any)
default:
}
// Execute the op
tryCounter++
lastError := op()
options.AfterRetry(lastError)
if lastError != nil {
if options.Checker != nil && options.Checker(lastError) {
// Check max retries
if tryCounter >= options.MaxTries {
options.AfterRetryLimit(lastError)
return errgo.WithCausef(lastError, MaxRetriesReachedError, "retry limit reached (%d/%d)", tryCounter, options.MaxTries)
}
if options.Sleep > 0 {
time.Sleep(options.Sleep)
}
continue
}
return errgo.Mask(lastError, errgo.Any)
}
return nil
}
}