-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_test.go
435 lines (415 loc) · 10.4 KB
/
retry_test.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package fast_retry
import (
"context"
"errors"
"fmt"
"io"
"log"
"math/rand"
"os"
"runtime"
"strings"
"sync"
"testing"
"time"
"net/http"
_ "net/http/pprof"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"golang.org/x/sync/errgroup"
)
func TestRetryNormal(t *testing.T) {
// 如果正确,应该只发起一次请求
ctx := context.Background()
var n atomic.Int32
r := New(Config{})
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
n.Inc()
return "hello", nil
})
require.Nil(t, err)
require.Equal(t, int32(1), n.Load())
}
func TestRetryAllFailed(t *testing.T) {
// 如果请求可以全部失败,那么也能快速返回
ctx := context.Background()
var n atomic.Int32
r := New(Config{})
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
n.Inc()
return nil, errors.New("xx")
})
require.NotNil(t, err)
require.Equal(t, int32(3), n.Load())
}
func TestRetryCancelBug(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
var n atomic.Int32
r := New(Config{})
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
n.Inc()
return "hello", nil
})
require.Equal(t, context.Canceled, err)
require.Equal(t, int32(0), n.Load())
}
func TestRetryCancelBug2(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
now := time.Now()
go func() {
time.Sleep(time.Second / 100)
cancel()
}()
r := New(Config{})
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
time.Sleep(time.Second * 10)
return "hello", nil
})
require.Equal(t, context.Canceled, err)
require.True(t, time.Since(now).Seconds() < 0.1)
}
func TestRetryBench(t *testing.T) {
t.Skip("用于人工看优化效果")
l := sync.Mutex{}
lines := make([]Line, 0)
ctx := context.Background()
g, ctx := errgroup.WithContext(ctx)
r := New(Config{})
for i := 0; i < 100000; i++ {
fn := func() (resp interface{}, err error) {
if rand.Float64() < 0.99 {
if rand.Float64() < 0.99 {
time.Sleep(time.Second * 1)
return "ok", nil
} else {
time.Sleep(time.Second * 1)
return nil, errors.New("bad req")
}
}
if rand.Float64() < 0.5 {
time.Sleep(time.Second * 11)
return "ok", nil
}
time.Sleep(time.Second * 11)
return nil, errors.New("timeout")
}
g.Go(func() error {
start := time.Now()
_, err := r.BackupRetry(ctx, fn)
useTime := time.Since(start)
l.Lock()
lines = append(lines, Line{Type: "retry", UseTime: useTime.Seconds(), Err: err != nil})
l.Unlock()
return nil
})
g.Go(func() error {
start := time.Now()
var err error
for i := 0; i < 3; i++ {
_, err = fn()
if err == nil {
break
}
}
useTime := time.Since(start)
l.Lock()
lines = append(lines, Line{Type: "normal", UseTime: useTime.Seconds(), Err: err != nil})
l.Unlock()
return nil
})
}
_ = g.Wait()
//buf, err := json.Marshal(lines)
//require.Nil(t, err)
//err = ioutil.WriteFile("out.json", buf, 0644)
//require.Nil(t, err)
normalErr := 0
retryErr := 0
normalSlow := 0
retrySlow := 0
for _, line := range lines {
if line.Err && line.Type == "normal" {
normalErr++
}
if line.Err && line.Type == "retry" {
retryErr++
}
if line.UseTime > 5 && line.Type == "normal" {
normalSlow++
}
if line.UseTime > 5 && line.Type == "retry" {
retrySlow++
}
}
t.Log("normalErr", normalErr, "retryErr", retryErr, "normalSlow", normalSlow, "retrySlow", retrySlow)
}
func TestMaxRetryRate(t *testing.T) {
if testing.Short() {
t.Skip()
}
ctx := context.Background()
g, ctx := errgroup.WithContext(ctx)
var callbackCnt atomic.Int64
var errorCnt atomic.Int64
var successCnt atomic.Int64
// 模拟 FastRetryTime 设置不当,看请求是否放大两次
r := New(Config{MaxRetryRate: 0.1, FastRetryTime: time.Second / 15})
fn := func() (resp interface{}, err error) {
callbackCnt.Inc()
time.Sleep(time.Second / 10)
return "ok", nil
}
for i := 0; i < 100; i++ {
g.Go(func() error {
for j := 0; j < 100; j++ {
_, err := r.BackupRetry(ctx, fn)
if err != nil {
errorCnt.Inc()
} else {
successCnt.Inc()
}
}
return nil
})
}
_ = g.Wait()
t.Logf("%v %v %v", callbackCnt.Load(), errorCnt.Load(), successCnt.Load())
require.Equal(t, errorCnt.Load(), int64(0))
require.True(t, callbackCnt.Load() < int64(float64(successCnt.Load())*1.2))
}
func TestMaxRetryCapacity(t *testing.T) {
if testing.Short() {
t.Skip()
}
ctx := context.Background()
var callbackCnt atomic.Int64
var errorCnt atomic.Int64
var successCnt atomic.Int64
// 模拟 FastRetryTime 设置不当,看请求是否放大两次
maxRetryCapacity := 100
r := New(Config{MaxRetryRate: 0.1, FastRetryTime: time.Second / 15, MaxRetryCapacity: maxRetryCapacity})
slow := false
fn := func() (resp interface{}, err error) {
callbackCnt.Inc()
if slow {
time.Sleep(time.Second / 10)
}
return "ok", nil
}
{
g, ctx := errgroup.WithContext(ctx)
for i := 0; i < 100; i++ {
g.Go(func() error {
for j := 0; j < 100; j++ {
_, err := r.BackupRetry(ctx, fn)
if err != nil {
fmt.Println(err)
errorCnt.Inc()
} else {
successCnt.Inc()
}
}
return nil
})
}
_ = g.Wait()
}
slow = true
{
g, ctx := errgroup.WithContext(ctx)
for i := 0; i < 50; i++ {
g.Go(func() error {
for j := 0; j < 100; j++ {
_, err := r.BackupRetry(ctx, fn)
if err != nil {
fmt.Println(err)
errorCnt.Inc()
} else {
successCnt.Inc()
}
}
return nil
})
}
_ = g.Wait()
}
// 先 10000 个快速请求,然后 5000 个慢请求
// 5000 里面会有 10% 被重试,之前 10000 会积累一部分 quota
t.Logf("%v %v %v", callbackCnt.Load(), errorCnt.Load(), successCnt.Load())
require.Equal(t, errorCnt.Load(), int64(0))
require.True(t, callbackCnt.Load() < int64(float64(successCnt.Load())*1.2))
require.True(t, callbackCnt.Load() <= int64(10000+5000+5000*0.1+maxRetryCapacity*2))
}
func BenchmarkRetry(b *testing.B) {
ctx := context.Background()
r := New(Config{MaxRetryRate: 0.1, FastRetryTime: time.Second / 100})
for i := 0; i < b.N; i++ {
_, _ = r.BackupRetry(ctx, func() (resp interface{}, err error) {
return "ok", nil
})
}
}
func TestMemLeakSimple(t *testing.T) {
if testing.Short() {
t.Skip()
}
str := strings.Repeat("1234567890", 1024) // 10kb
ctx := context.Background()
r := New(Config{MaxRetryRate: 0.1, FastRetryTime: time.Second / 100})
g := sync.WaitGroup{}
for i := 0; i < 100000; i++ {
g.Add(1)
go func() {
_, _ = r.BackupRetry(ctx, func() (resp interface{}, err error) {
time.Sleep(time.Second / 10)
if rand.Float64() < 0.1 {
return nil, errors.New("ok")
}
return str + "a", nil
})
g.Done()
}()
if i%100 == 0 {
t.Logf("i:%v", i)
}
}
g.Wait()
time.Sleep(time.Second)
t.Logf("go:%v", runtime.NumGoroutine())
require.True(t, runtime.NumGoroutine() < 5)
}
func TestMemLeakFull(t *testing.T) {
if os.Getenv("TEST_LEAK") == "" {
t.Skip()
}
// TEST_LEAK=1 GODEBUG=gctrace=1 go test -run TestMemLeak -v
// 内存泄露测试,如果允许内存一直不变,则代表无内存泄露
// 如果把 m[i] = "ok" 注释回来,则可以模拟泄露的情况
go func() {
for {
time.Sleep(time.Second * 3)
t.Logf("num:%v", runtime.NumGoroutine())
}
}()
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
str := strings.Repeat("1234567890", 1024) // 10kb
ctx := context.Background()
r := New(Config{MaxRetryRate: 0.1, FastRetryTime: time.Second / 100})
// m := map[int]string{}
for i := 0; i < 100000; i++ {
// m[i] = "ok"
go func() {
_, _ = r.BackupRetry(ctx, func() (resp interface{}, err error) {
time.Sleep(time.Second)
if rand.Float64() < 0.1 {
return nil, errors.New("ok")
}
return str + "a", nil
})
}()
if i%100 == 0 {
t.Logf("i:%v", i)
}
}
time.Sleep(time.Hour)
}
type Line struct {
Type string
UseTime float64
Err bool
}
func TestRetryIf(t *testing.T) {
{
// 设置了 RetryIf, 重试一次
ctx := context.Background()
var n atomic.Int32
r := New(Config{RetryIf: func(err error) bool {
if err == io.EOF {
return false
}
return err != nil
}})
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
n.Inc()
return "ok", io.EOF
})
require.Error(t, err, io.EOF)
require.Equal(t, int32(1), n.Load())
}
{
// 设置了 RetryIf, 但是非预期错误,重试 3 次
ctx := context.Background()
var n atomic.Int32
r := New(Config{RetryIf: func(err error) bool {
if err == io.EOF {
return false
}
return err != nil
}})
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
n.Inc()
return "ok", errors.New("ok")
})
require.Error(t, err, errors.New("ok"))
require.Equal(t, int32(3), n.Load())
}
{
// 没有设置了 RetryIf, 重试 3 次
ctx := context.Background()
var n atomic.Int32
r := New(Config{RetryIf: func(err error) bool {
return err != nil
}})
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
n.Inc()
return "ok", io.EOF
})
require.Error(t, err, io.EOF)
require.Equal(t, int32(3), n.Load())
}
}
func TestRetryQuota(t *testing.T) {
{
ctx := context.Background()
var n atomic.Int32
r := New(Config{FastRetryTime: time.Millisecond * 20, MaxRetryRate: 0.05, RetryIf: func(err error) bool {
return err == io.EOF
}})
{
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
n.Inc()
time.Sleep(time.Millisecond * 30)
return "ok", nil
})
require.Nil(t, err)
require.Equal(t, r.score.Load(), int64(-19))
require.Equal(t, int32(2), n.Load())
}
// 此时 r.score 处于小于0的状态
// 如果 接下来的请求本身成功了,那么还是返回成功,只是 fast retry 没有 quota 自动失效
{
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
n.Inc()
time.Sleep(time.Millisecond * 50)
return "ok", nil
})
require.Nil(t, err)
require.Equal(t, r.score.Load(), int64(-18))
require.Equal(t, int32(3), n.Load())
}
// 如果 接下来的请求本身失败了,那么返回真实的错误
{
_, err := r.BackupRetry(ctx, func() (resp interface{}, err error) {
n.Inc()
time.Sleep(time.Millisecond * 50)
return "ok", io.EOF
})
require.Equal(t, r.score.Load(), int64(-17))
require.Equal(t, int32(5), n.Load())
require.Equal(t, err, io.EOF)
}
}
}