forked from algolia/algoliasearch-client-go
/
retry_strategy.go
266 lines (227 loc) · 6.49 KB
/
retry_strategy.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package algoliasearch
import (
"context"
"fmt"
"math/rand"
"net"
"strings"
"sync"
"time"
"github.com/algolia/algoliasearch-client-go/algoliasearch/call"
)
func init() {
rand.Seed(int64(time.Now().Nanosecond()))
}
type Outcome int
const (
DefaultReadTimeout = 5 * time.Second
DefaultWriteTimeout = 30 * time.Second
DefaultAnalyticsTimeout = 30 * time.Second
Success Outcome = iota
Failure
Retry
)
type TryableHost interface {
Host() string
Timeout() time.Duration
}
type tryableHost struct {
host string
timeout time.Duration
}
func (h *tryableHost) Host() string { return h.host }
func (h *tryableHost) Timeout() time.Duration { return h.timeout }
func (h *tryableHost) String() string { return fmt.Sprintf("tryableHost{%s,%s}", h.host, h.timeout) }
type RetryStrategy interface {
// GetTryableHosts returns the slice of host to try to send the request to.
GetTryableHosts(k call.Kind) []TryableHost
// Decide returns an Outcome defining if the call have succeded, or failed
// or to be retried.
Decide(h TryableHost, code int, err error) Outcome
// SetTimeouts updates the internal timeouts for read, write (i.e.
// indexing) and analytics calls. Negative values are simply ignored,
// leaving the original timeouts unchanged.
SetTimeouts(read, write, analytics time.Duration)
}
type retryStrategy struct {
sync.RWMutex
hosts []*statefulHost
readTimeout time.Duration
writeTimeout time.Duration
analyticsTimeout time.Duration
}
type statefulHost struct {
host string
isDown bool
retryCount int
lastUpdate time.Time
accept func(k call.Kind) bool
}
func (h *statefulHost) String() string {
return fmt.Sprintf(
"statefulHost{host:%s, isDown: %t, retryCount:%d}",
h.host,
h.isDown,
h.retryCount,
)
}
func (h *statefulHost) reset() {
h.isDown = false
h.lastUpdate = time.Now()
h.retryCount = 0
}
func NewRetryStrategy(appID string, providedHosts []string) *retryStrategy {
var allHosts []*statefulHost
now := time.Now()
if providedHosts != nil && len(providedHosts) > 0 {
for _, h := range providedHosts {
allHosts = append(allHosts, &statefulHost{host: h, lastUpdate: now, accept: call.IsReadWrite})
}
} else {
allHosts = append(allHosts, &statefulHost{host: appID + "-dsn.algolia.net", lastUpdate: now, accept: call.IsRead})
allHosts = append(allHosts, &statefulHost{host: appID + ".algolia.net", lastUpdate: now, accept: call.IsWrite})
allHosts = append(allHosts, shuffle(
[]*statefulHost{
&statefulHost{host: appID + "-1.algolianet.com", lastUpdate: now, accept: call.IsReadWrite},
&statefulHost{host: appID + "-2.algolianet.com", lastUpdate: now, accept: call.IsReadWrite},
&statefulHost{host: appID + "-3.algolianet.com", lastUpdate: now, accept: call.IsReadWrite},
},
)...)
}
allHosts = append(allHosts, &statefulHost{host: "analytics.algolia.com", lastUpdate: now, accept: call.IsAnalytics})
return &retryStrategy{
hosts: allHosts,
readTimeout: DefaultReadTimeout,
writeTimeout: DefaultWriteTimeout,
analyticsTimeout: DefaultAnalyticsTimeout,
}
}
func (s *retryStrategy) GetTryableHosts(k call.Kind) []TryableHost {
s.resetExpiredHosts()
s.displayState()
s.Lock()
defer s.Unlock()
var baseTimeout time.Duration
switch k {
case call.Read:
baseTimeout = s.readTimeout
case call.Write:
baseTimeout = s.writeTimeout
case call.Analytics:
baseTimeout = s.analyticsTimeout
default:
return nil
}
var hosts []TryableHost
for _, h := range s.hosts {
if !h.isDown && h.accept(k) {
hosts = append(hosts, &tryableHost{h.host, baseTimeout * time.Duration(h.retryCount+1)})
}
}
if len(hosts) > 0 {
return hosts
}
for _, h := range s.hosts {
if h.accept(k) {
h.reset()
hosts = append(hosts, &tryableHost{h.host, baseTimeout})
}
}
return hosts
}
func (s *retryStrategy) Decide(h TryableHost, code int, err error) Outcome {
if err == nil && is2xx(code) {
debug("* RETRY STRATEGY DECISION host=%s code=%d err=%v -> SUCCESS", h.Host(), code, err)
s.markUp(h.Host())
return Success
}
if isTimeoutError(err) {
debug("* RETRY STRATEGY DECISION host=%s code=%d err=%v -> RETRY (TIMEOUT)", h.Host(), code, err)
s.markTimeouted(h.Host())
return Retry
}
if !(isZero(code) || is4xx(code) || is2xx(code)) || isNetworkError(err) {
debug("* RETRY STRATEGY DECISION host=%s code=%d err=%v -> RETRY (DOWN)", h.Host(), code, err)
s.markDown(h.Host())
return Retry
}
debug("* RETRY STRATEGY DECISION host=%s code=%d err=%v -> FAILURE", h.Host(), code, err)
return Failure
}
func (s *retryStrategy) SetTimeouts(read, write, analytics time.Duration) {
s.Lock()
defer s.Unlock()
if read > 0 {
s.readTimeout = read
}
if write > 0 {
s.writeTimeout = write
}
if analytics > 0 {
s.analyticsTimeout = analytics
}
}
func (s *retryStrategy) markUp(host string) { s.update(host, false, false) }
func (s *retryStrategy) markDown(host string) { s.update(host, true, false) }
func (s *retryStrategy) markTimeouted(host string) { s.update(host, false, true) }
func (s *retryStrategy) update(host string, isDown, isTimeout bool) {
s.Lock()
defer s.Unlock()
for _, h := range s.hosts {
if h.host == host {
h.isDown = isDown
h.lastUpdate = time.Now()
if isTimeout {
h.retryCount++
} else {
h.retryCount = 0
}
return
}
}
}
func (s *retryStrategy) resetExpiredHosts() {
s.Lock()
defer s.Unlock()
for _, h := range s.hosts {
if h.isDown && time.Since(h.lastUpdate) > 5*time.Minute {
h.reset()
}
}
}
func (s *retryStrategy) displayState() {
s.RLock()
defer s.RUnlock()
for _, h := range s.hosts {
debug("* RETRY STRATEGY STATE %s", h)
}
}
func shuffle(hosts []*statefulHost) []*statefulHost {
if hosts == nil {
return nil
}
shuffled := make([]*statefulHost, len(hosts))
for i, v := range rand.Perm(len(hosts)) {
shuffled[i] = hosts[v]
}
return shuffled
}
func isNetworkError(err error) bool {
if err == nil {
return false
}
_, ok := err.(net.Error)
// We need to ensure that the error is a net.Error but not a
// context.DeadlineExceeded error (which is actually a net.Error), because
// we do not want to consider context.DeadlineExceeded as an error.
return ok && !isTimeoutError(err)
}
func isTimeoutError(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), context.DeadlineExceeded.Error())
}
func isZero(code int) bool { return code == 0 }
func is2xx(code int) bool { return 200 <= code && code < 300 }
func is4xx(code int) bool { return 400 <= code && code < 500 }