-
Notifications
You must be signed in to change notification settings - Fork 269
/
retry_with_opt.go
109 lines (96 loc) · 2.7 KB
/
retry_with_opt.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
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package retry
import (
"context"
"math"
"math/rand"
"strconv"
"time"
"github.com/pingcap/errors"
cerror "github.com/pingcap/tiflow/pkg/errors"
)
// Operation is the action need to retry
type Operation func() error
// Do execute the specified function.
// By default, it retries infinitely until it succeeds or got canceled.
func Do(ctx context.Context, operation Operation, opts ...Option) error {
retryOption := setOptions(opts...)
return run(ctx, operation, retryOption)
}
func setOptions(opts ...Option) *retryOptions {
retryOption := newRetryOptions()
for _, opt := range opts {
opt(retryOption)
}
return retryOption
}
func run(ctx context.Context, op Operation, retryOption *retryOptions) error {
select {
case <-ctx.Done():
return errors.Trace(ctx.Err())
default:
}
var t *time.Timer
var start time.Time
try := uint64(0)
backOff := time.Duration(0)
for {
err := op()
if err == nil {
return nil
}
if !retryOption.isRetryable(err) {
return err
}
try++
if try >= retryOption.maxTries {
return cerror.ErrReachMaxTry.
Wrap(err).GenWithStackByArgs(strconv.Itoa(int(retryOption.maxTries)), err)
}
if retryOption.totalRetryDuration > 0 {
if start.IsZero() {
start = time.Now()
} else if time.Since(start) > retryOption.totalRetryDuration {
return cerror.ErrReachMaxTry.
Wrap(err).GenWithStackByArgs(retryOption.totalRetryDuration, err)
}
}
backOff = getBackoffInMs(retryOption.backoffBaseInMs, retryOption.backoffCapInMs, float64(try))
if t == nil {
t = time.NewTimer(backOff)
defer t.Stop()
} else {
t.Reset(backOff)
}
select {
case <-ctx.Done():
return errors.Trace(ctx.Err())
case <-t.C:
}
}
}
// getBackoffInMs returns the duration to wait before next try
// See https://www.awsarchitectureblog.com/2015/03/backoff.html
func getBackoffInMs(backoffBaseInMs, backoffCapInMs, try float64) time.Duration {
temp := int64(math.Min(backoffCapInMs, backoffBaseInMs*math.Exp2(try)) / 2)
if temp <= 0 {
temp = 1
}
sleep := (temp + rand.Int63n(temp)) * 3
if sleep <= 0 {
sleep = math.MaxInt64
}
backOff := math.Min(backoffCapInMs, float64(rand.Int63n(sleep))+backoffBaseInMs)
return time.Duration(backOff) * time.Millisecond
}