forked from Brodic/goots
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathretry.go
258 lines (209 loc) · 6.99 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
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
// Copyright 2016 The GiterLab Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// retry for ots2
package goots
import (
"math"
"math/rand"
"net"
"time"
)
// RetryPolicy 是重试策略的接口,包含2个未实现的方法和它们的参数列表。要实现一个重试策略,
// 继承这个类并实现它的2个方法。
type RetryPolicyInterface interface {
GetRetryDelay(retry_times int, exception *OTSServiceError, api_name string) float64
ShouldRetry(retry_times int, exception *OTSServiceError, api_name string) bool
}
// 默认重试策略
// 最大重试次数为3,最大重试间隔为2000毫秒,对流控类错误以及读操作相关的服务端内部错误进行了重试。
//
// Methods defined here:
//
// GetRetryDelay(retry_times int, exception *OTSServiceError, api_name string) float64
//
// ShouldRetry(retry_times int, exception *OTSServiceError, api_name string) bool
//
// ----------------------------------------------------------------------
// Data and other attributes defined here:
//
// MaxRetryDelay = 2
//
// MaxRetryTimes = 3
//
// ScaleFactor = 2
//
// ServerThrottlingExceptionDelayFactor = 0.5
//
// StabilityExceptionDelayFactor = 0.2
var OTSDefaultRetryPolicy DefaultRetryPolicy
// 不进行任何重试的重试策略
var OTSNoRetryPolicy NoRetryPolicy
// 没有延时的重试策略
// 最大重试次数为3
var OTSNoDelayRetryPolicy NoDelayRetryPolicy
func init() {
OTSDefaultRetryPolicy.MaxRetryTimes = 6
OTSDefaultRetryPolicy.MaxRetryDelay = 2
OTSDefaultRetryPolicy.ScaleFactor = 2
OTSDefaultRetryPolicy.ServerThrottlingExceptionDelayFactor = 0.5
OTSDefaultRetryPolicy.StabilityExceptionDelayFactor = 0.2
OTSNoDelayRetryPolicy.MaxRetryTimes = 3
}
func should_retry_no_matter_which_api(exception *OTSServiceError) bool {
if exception != nil {
error_code := exception.Code
error_message := exception.Message
if error_code == "OTSRowOperationConflict" ||
error_code == "OTSNotEnoughCapacityUnit" ||
error_code == "OTSTableNotReady" ||
error_code == "OTSPartitionUnavailable" ||
error_code == "OTSServerBusy" ||
error_code == "OTSOperationThrottled" {
return true
}
if error_code == "OTSQuotaExhausted" && error_message == "Too frequent table operations." {
return true
}
}
return false
}
func is_repeatable_api(api_name string) bool {
if api_name == "ListTable" ||
api_name == "DescribeTable" ||
api_name == "GetRow" ||
api_name == "BatchGetRow" ||
api_name == "GetRange" ||
api_name == "DescrieStream" ||
api_name == "GetShardIterator" ||
api_name == "GetStreamRecord" ||
api_name == "ListStream" {
return true
}
return false
}
func should_retry_when_api_repeatable(retry_times int, exception *OTSServiceError, api_name string) bool {
if exception != nil {
if exception.Err != nil {
if _, ok := exception.Err.(net.Error); ok {
return true
} else if exception.Err == ErrNonResponseBody || exception.Err == ErrReadResponse {
return true
}
}
error_code := exception.Code
//error_message := exception.Message
http_status := exception.HttpStatus
if error_code == "OTSTimeout" ||
error_code == "OTSInternalServerError" ||
error_code == "OTSServerUnavailable" {
return true
}
if http_status == 500 || http_status == 502 || http_status == 503 {
return true
}
}
// TODO handle network error & timeout
return false
}
func is_server_throttling_exception(exception *OTSServiceError) bool {
if exception != nil {
error_code := exception.Code
error_message := exception.Message
if error_code == "OTSServerBusy" ||
error_code == "OTSNotEnoughCapacityUnit" ||
error_code == "OTSOperationThrottled" {
return true
}
if error_code == "OTSQuotaExhausted" && error_message == "Too frequent table operations." {
return true
}
}
return false
}
// 默认重试策略
// 最大重试次数为3,最大重试间隔为2000毫秒,对流控类错误以及读操作相关的服务端内部错误进行了重试。
type DefaultRetryPolicy struct {
// 最大重试次数
MaxRetryTimes int
// 最大重试间隔,单位为秒
MaxRetryDelay float64
// 每次重试间隔的递增倍数
ScaleFactor float64
// 两种错误的起始重试间隔,单位为秒
ServerThrottlingExceptionDelayFactor float64
StabilityExceptionDelayFactor float64
}
func (self DefaultRetryPolicy) _max_retry_time_reached(retry_times int, exception *OTSServiceError, api_name string) bool {
return retry_times >= self.MaxRetryTimes
}
func (self DefaultRetryPolicy) _can_retry(retry_times int, exception *OTSServiceError, api_name string) bool {
if should_retry_no_matter_which_api(exception) {
return true
}
if is_repeatable_api(api_name) && should_retry_when_api_repeatable(retry_times, exception, api_name) {
return true
}
return false
}
func (self DefaultRetryPolicy) GetRetryDelay(retry_times int, exception *OTSServiceError, api_name string) float64 {
var delay_factor float64
if is_server_throttling_exception(exception) {
delay_factor = self.ServerThrottlingExceptionDelayFactor
} else {
delay_factor = self.StabilityExceptionDelayFactor
}
delay_limit := delay_factor * math.Pow(self.ScaleFactor, float64(retry_times))
if delay_limit >= self.MaxRetryDelay {
delay_limit = self.MaxRetryDelay
}
real_delay := delay_limit*0.5 + delay_limit*0.5*rand.New(rand.NewSource(time.Now().UnixNano())).Float64()
return real_delay
}
func (self DefaultRetryPolicy) ShouldRetry(retry_times int, exception *OTSServiceError, api_name string) bool {
if self._max_retry_time_reached(retry_times, exception, api_name) {
return false
}
if self._can_retry(retry_times, exception, api_name) {
return true
}
return false
}
// 不进行任何重试的重试策略
type NoRetryPolicy struct {
}
func (self NoRetryPolicy) GetRetryDelay(retry_times int, exception *OTSServiceError, api_name string) float64 {
return 0
}
func (self NoRetryPolicy) ShouldRetry(retry_times int, exception *OTSServiceError, api_name string) bool {
return false
}
// 没有延时的重试策略
type NoDelayRetryPolicy struct {
// 最大重试次数
MaxRetryTimes int
}
func (self NoDelayRetryPolicy) _max_retry_time_reached(retry_times int, exception *OTSServiceError, api_name string) bool {
return retry_times >= self.MaxRetryTimes
}
func (self NoDelayRetryPolicy) _can_retry(retry_times int, exception *OTSServiceError, api_name string) bool {
if should_retry_no_matter_which_api(exception) {
return true
}
if is_repeatable_api(api_name) && should_retry_when_api_repeatable(retry_times, exception, api_name) {
return true
}
return false
}
func (sels NoDelayRetryPolicy) GetRetryDelay(retry_times int, exception *OTSServiceError, api_name string) float64 {
return 0
}
func (self NoDelayRetryPolicy) ShouldRetry(retry_times int, exception *OTSServiceError, api_name string) bool {
if self._max_retry_time_reached(retry_times, exception, api_name) {
return false
}
if self._can_retry(retry_times, exception, api_name) {
return true
}
return false
}