-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
182 lines (160 loc) · 4.58 KB
/
context.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
package pail
import (
"errors"
"fmt"
"sync/atomic"
"time"
"github.com/couchbase/gocb/v2"
)
type (
ClusterRetryFunc func(*gocb.Cluster) error
CollectionRetryFunc func(*gocb.Collection) error
QueryIndexManagerRetryFunc func(*gocb.QueryIndexManager) error
)
// this is a list of errors deemed to probably be related to a connection issue.
var cbConnectionErrors = []error{
gocb.ErrOverload,
gocb.ErrTimeout,
}
// Given an error, determine if it's a connection related error and return
// true if so.
func isConnectErr(err error) bool {
if err == nil {
return false
}
for _, connErr := range cbConnectionErrors {
if errors.Is(err, connErr) {
return true
}
}
return false
}
type ConnectionErrorRetryAction time.Duration
func (a ConnectionErrorRetryAction) Duration() time.Duration { return time.Duration(a) }
type baseRetryContext struct {
tries uint32
limit uint32
action ConnectionErrorRetryAction
baseStrategy gocb.RetryStrategy
}
func newBaseRetryContext(retries uint32, delay time.Duration, baseStrategy gocb.RetryStrategy) baseRetryContext {
return baseRetryContext{
limit: retries,
action: ConnectionErrorRetryAction(delay),
baseStrategy: baseStrategy,
}
}
func (bc baseRetryContext) RetryAfter(req gocb.RetryRequest, reason gocb.RetryReason) gocb.RetryAction {
// if base strategy provided, defer to it
if bc.baseStrategy != nil {
// increment counter only if this is not an "always retry" reason
if !reason.AlwaysRetry() {
atomic.AddUint32(&bc.tries, 1)
}
return bc.baseStrategy.RetryAfter(req, reason)
}
// if the source reason stems from an "always retry" error i.e., incorrect node queried for a particular vbucket,
// always attempt again
if reason.AlwaysRetry() {
return bc.action
}
// test for breach of retry limit
if t := atomic.AddUint32(&bc.tries, 1); t > bc.limit {
return ConnectionErrorRetryAction(0)
}
// try again, plz.
return bc.action
}
type ClusterRetryContext interface {
gocb.RetryStrategy
Try(*gocb.Cluster) error
}
type DefaultClusterRetryContext struct {
baseRetryContext
retryFunc ClusterRetryFunc
}
func NewSimpleClusterRetryContext(retries uint32, delay time.Duration, baseStrategy gocb.RetryStrategy, fn ClusterRetryFunc) DefaultClusterRetryContext {
rc := DefaultClusterRetryContext{
baseRetryContext: newBaseRetryContext(retries, delay, baseStrategy),
retryFunc: fn,
}
return rc
}
func (rc DefaultClusterRetryContext) Try(c *gocb.Cluster) error {
var (
t uint32
err error
)
for t = atomic.AddUint32(&rc.tries, 1); t <= rc.limit; {
if err = rc.retryFunc(c); err == nil {
return nil
} else if isConnectErr(err) {
time.Sleep(time.Duration(rc.action))
} else {
return err
}
}
return fmt.Errorf("retry limit breached (last error: %w)", err)
}
type CollectionRetryContext interface {
gocb.RetryStrategy
Try(*gocb.Collection) error
}
type SimpleCollectionRetryContext struct {
baseRetryContext
retryFunc CollectionRetryFunc
}
func NewSimpleCollectionRetryContext(retries uint32, delay time.Duration, baseStrategy gocb.RetryStrategy, fn CollectionRetryFunc) SimpleCollectionRetryContext {
rc := SimpleCollectionRetryContext{
baseRetryContext: newBaseRetryContext(retries, delay, baseStrategy),
retryFunc: fn,
}
return rc
}
func (rc SimpleCollectionRetryContext) Try(c *gocb.Collection) error {
var (
t uint32
err error
)
for t = atomic.AddUint32(&rc.tries, 1); t <= rc.limit; {
if err = rc.retryFunc(c); err == nil {
return nil
} else if isConnectErr(err) {
time.Sleep(time.Duration(rc.action))
} else {
return err
}
}
return fmt.Errorf("retry limit breached (last error: %w)", err)
}
type QueryIndexManagerRetryContext interface {
gocb.RetryStrategy
Try(*gocb.QueryIndexManager) error
}
type SimpleQueryIndexManagerRetryContext struct {
baseRetryContext
retryFunc QueryIndexManagerRetryFunc
}
func NewSimpleQueryIndexManagerRetryContext(retries uint32, delay time.Duration, baseStrategy gocb.RetryStrategy, fn QueryIndexManagerRetryFunc) SimpleQueryIndexManagerRetryContext {
rc := SimpleQueryIndexManagerRetryContext{
baseRetryContext: newBaseRetryContext(retries, delay, baseStrategy),
retryFunc: fn,
}
return rc
}
func (rc SimpleQueryIndexManagerRetryContext) Try(qm *gocb.QueryIndexManager) error {
var (
t uint32
err error
)
for t = atomic.AddUint32(&rc.tries, 1); t <= rc.limit; {
if err = rc.retryFunc(qm); err == nil {
return nil
} else if isConnectErr(err) {
time.Sleep(time.Duration(rc.action))
} else {
return err
}
}
return fmt.Errorf("retry limit breached (last error: %w)", err)
}