-
Notifications
You must be signed in to change notification settings - Fork 9
/
retry.go
195 lines (173 loc) · 4.37 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package blobstore
// Copyright 2019 Vivino. All rights reserved
//
// See LICENSE file for license details
import (
"context"
"time"
"github.com/cenkalti/backoff"
)
// RetryStore will apply retry functionality with customizable backoff.
type RetryStore struct {
retryStoreOptions
store Store
}
type RetryStoreOption func(*retryStoreOptions) error
type retryStoreOptions struct {
// Back off for each operation.
get, set, del func() backoff.BackOff
opTimeout time.Duration
// No retry on these errors
permanent map[error]struct{}
}
var defaultBackoff = func() backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.InitialInterval = 20 * time.Millisecond
return b
}
var defaultRetryStoreOptions = retryStoreOptions{
get: defaultBackoff,
set: defaultBackoff,
del: defaultBackoff,
opTimeout: time.Second,
permanent: map[error]struct{}{
context.Canceled: {},
ErrBlobNotFound: {},
ErrBlobTooBig: {},
},
}
// LazySaveOption provides access to LazySave Options that are methods on this struct.
type RetryOpt struct{}
// WithLazySaveOption provides an element to create lazySave parameters.
var WithRetryOpt = RetryOpt{}
// Backoff supplies individual backoff strategies for get, set and delete operations.
// If nil is supplied for any option, that strategy will not be updated.
func (l RetryOpt) Backoff(get, set, del func() backoff.BackOff) RetryStoreOption {
return func(o *retryStoreOptions) error {
if get != nil {
o.get = get
}
if set != nil {
o.set = set
}
if del != nil {
o.del = del
}
return nil
}
}
// PermanentErr will add errors to permanent error table.
func (l RetryOpt) PermanentErr(errs ...error) RetryStoreOption {
return func(o *retryStoreOptions) error {
if len(errs) == 0 {
return nil
}
// Copy so we don't modify defaults.
perm := map[error]struct{}{}
for k := range o.permanent {
perm[k] = struct{}{}
}
for _, err := range errs {
if err != nil {
o.permanent[err] = struct{}{}
}
}
o.permanent = perm
return nil
}
}
// OpTimeout will set the context timeout for each operation attempt.
// Setting this to 0 will use parent context deadlines.
// Default is 1 second.
func (l RetryOpt) OpTimeout(duration time.Duration) RetryStoreOption {
return func(o *retryStoreOptions) error {
o.opTimeout = duration
return nil
}
}
// NewLazySaver will create a new lazy saver backed by the provided store.
func NewRetryStore(store Store, opts ...RetryStoreOption) (*RetryStore, error) {
r := RetryStore{
retryStoreOptions: defaultRetryStoreOptions,
store: store,
}
for _, opt := range opts {
err := opt(&r.retryStoreOptions)
if err != nil {
return nil, err
}
}
return &r, nil
}
// Get a blob.
func (r *RetryStore) Get(ctx context.Context, set, key string) ([]byte, error) {
bo := r.get()
bo = backoff.WithContext(bo, ctx)
var res []byte
op := func() error {
lctx := ctx
if r.opTimeout != 0 {
var cancel context.CancelFunc
lctx, cancel = context.WithTimeout(ctx, r.opTimeout)
defer cancel()
}
b, err := r.store.Get(lctx, set, key)
if err != nil {
return r.opErr(ctx, err)
}
res = b
return nil
}
return res, backoff.Retry(op, bo)
}
// Set a blob.
func (r *RetryStore) Set(ctx context.Context, set, key string, b []byte) error {
bo := r.set()
bo = backoff.WithContext(bo, ctx)
op := func() error {
lctx := ctx
if r.opTimeout != 0 {
var cancel context.CancelFunc
lctx, cancel = context.WithTimeout(ctx, r.opTimeout)
defer cancel()
}
err := r.store.Set(lctx, set, key, b)
if err != nil {
return r.opErr(ctx, err)
}
return nil
}
return backoff.Retry(op, bo)
}
// Delete a blob.
func (r *RetryStore) Delete(ctx context.Context, set, key string) error {
bo := r.del()
bo = backoff.WithContext(bo, ctx)
op := func() error {
lctx := ctx
if r.opTimeout != 0 {
var cancel context.CancelFunc
lctx, cancel = context.WithTimeout(ctx, r.opTimeout)
defer cancel()
}
err := r.store.Delete(lctx, set, key)
if err != nil {
return r.opErr(ctx, err)
}
return nil
}
return backoff.Retry(op, bo)
}
// opErr checks if an error is permanent or the context is cancelled
// and returns a permanent error if so.
func (r *RetryStore) opErr(ctx context.Context, err error) error {
if _, ok := r.permanent[err]; ok {
return backoff.Permanent(err)
}
select {
case <-ctx.Done():
return backoff.Permanent(err)
default:
}
return err
}