forked from seiffert/ctxaws
/
retryer.go
49 lines (41 loc) · 1.53 KB
/
retryer.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
package ctxaws
import (
"errors"
"time"
"github.com/aws/aws-sdk-go/aws/request"
"golang.org/x/net/context"
)
// ErrDeadlineWouldExceedBeforeRetry is set as a request's error when a retry is cancelled because
// the backoff interval would exceed the context's deadline.
var ErrDeadlineWouldExceedBeforeRetry = errors.New("deadline would exceed before next retry")
// NewContextAwareRetryer creates a Retryer that honors the given context's deadline.
func NewContextAwareRetryer(ctx context.Context, awsRetryer request.Retryer) *Retryer {
return &Retryer{
Retryer: awsRetryer,
ctx: ctx,
}
}
// Retryer is an implementation of `request.Retryer` that honors its context's deadline. While the
// delay mechanism is exactly the same as in the `client.DefaultRetryer`, it instructs its clients
// not to retry requests when the context would expire while waiting for the next try. Also requests
// won't be retried when the context already has expired.
type Retryer struct {
request.Retryer
ctx context.Context
}
// ShouldRetry compares the next retry time with the context's deadline and returns false if the
// deadline would occur prior to the next try. If not, the call is passed through to the default
// retryer.
func (r *Retryer) ShouldRetry(req *request.Request) bool {
if r.ctx.Err() != nil {
req.Error = r.ctx.Err()
return false
}
if deadline, ok := r.ctx.Deadline(); ok {
if deadline.Sub(time.Now()) < r.RetryRules(req) {
req.Error = ErrDeadlineWouldExceedBeforeRetry
return false
}
}
return r.Retryer.ShouldRetry(req)
}