-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry.go
125 lines (104 loc) · 4.45 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
package tripwire
import (
com "github.com/shengyanli1982/tripwire/common"
)
// emptyRetryResult 结构体用于存储重试操作的结果
// The emptyRetryResult struct is used to store the results of retry operations
type emptyRetryResult struct {
// attemptCount 是已经尝试的次数
// attemptCount is the number of attempts that have been made
attemptCount uint64
// data 是重试操作的返回数据
// data is the return data of the retry operation
data interface{}
// tryError 是重试操作的错误
// tryError is the error of the retry operation
tryError error
// execErrors 是执行过程中遇到的所有错误
// execErrors are all the errors encountered during execution
execErrors []error
}
// Data 方法返回重试操作的返回数据
// The Data method returns the return data of the retry operation
func (r *emptyRetryResult) Data() interface{} {
return r.data
}
// TryError 方法返回重试操作的错误
// The TryError method returns the error of the retry operation
func (r *emptyRetryResult) TryError() error {
return r.tryError
}
// IsSuccess 方法检查重试操作是否成功,如果没有错误,则认为操作成功
// The IsSuccess method checks whether the retry operation is successful. If there is no error, the operation is considered successful
func (r *emptyRetryResult) IsSuccess() bool {
return r.tryError == nil
}
// Count 方法返回已经尝试的次数
// The Count method returns the number of attempts that have been made
func (r *emptyRetryResult) Count() int64 {
return int64(r.attemptCount)
}
// ExecErrors 方法返回执行过程中遇到的所有错误
// The ExecErrors method returns all the errors encountered during execution
func (r *emptyRetryResult) ExecErrors() []error {
return r.execErrors
}
// LastExecError 方法返回最后一个执行错误
// The LastExecError method returns the last execution error
func (r *emptyRetryResult) LastExecError() error {
// 检查是否有执行错误,如果有,则返回最后一个错误
// Check if there are execution errors, if there are, return the last error
if len(r.execErrors) > 0 {
return r.execErrors[len(r.execErrors)-1]
}
// 如果没有执行错误,则返回 nil
// If there are no execution errors, return nil
return nil
}
// FirstExecError 方法返回第一个执行错误
// The FirstExecError method returns the first execution error
func (r *emptyRetryResult) FirstExecError() error {
// 检查是否有执行错误,如果有,则返回第一个错误
// Check if there are execution errors, if there are, return the first error
if len(r.execErrors) > 0 {
return r.execErrors[0]
}
// 如果没有执行错误,则返回 nil
// If there are no execution errors, return nil
return nil
}
// ExecErrorByIndex 方法返回指定索引位置的执行错误
// The ExecErrorByIndex method returns the execution error at the specified index position
func (r *emptyRetryResult) ExecErrorByIndex(idx int) error {
// 检查索引是否在有效范围内,如果在有效范围内,则返回对应的错误
// Check if the index is within the valid range, if it is within the valid range, return the corresponding error
if idx >= 0 && idx < len(r.execErrors) {
return r.execErrors[idx]
}
// 如果索引不在有效范围内,则返回 nil
// If the index is not within the valid range, return nil
return nil
}
// EmptyRetry 是一个空的重试结构体
// EmptyRetry is an empty retry struct
type EmptyRetry struct{}
// TryOnConflictVal 方法尝试执行 fn 函数,并返回执行结果
// The TryOnConflictVal method attempts to execute the fn function and returns the result
func (r *EmptyRetry) TryOnConflictVal(fn com.RetryableFunc) com.RetryResult {
// 创建一个 retryResult 结构体,设置尝试次数为 1
// Create a retryResult struct and set the attempt count to 1
re := emptyRetryResult{attemptCount: 1}
// 执行 fn 函数,将返回的数据和错误设置到 retryResult 结构体中
// Execute the fn function and set the returned data and error to the retryResult struct
re.data, re.tryError = fn()
// 返回 retryResult 结构体的指针
// Return the pointer of the retryResult struct
return &re
}
// NewEmptyRetry 函数返回一个新的 EmptyRetry 结构体的指针
// The NewEmptyRetry function returns a new pointer to the EmptyRetry struct
func NewEmptyRetry() com.Retry {
// 返回一个新的 EmptyRetry 结构体的指针
// Return a new pointer to the EmptyRetry struct
return &EmptyRetry{}
}