mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
merge rc
This commit is contained in:
@@ -60,7 +60,7 @@ func (q *ArrayQueue[T]) Front() T {
|
|||||||
|
|
||||||
// Back return back value of queue
|
// Back return back value of queue
|
||||||
func (q *ArrayQueue[T]) Back() T {
|
func (q *ArrayQueue[T]) Back() T {
|
||||||
return q.data[q.size-1]
|
return q.data[q.tail-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnQueue put element into queue
|
// EnQueue put element into queue
|
||||||
|
|||||||
@@ -128,9 +128,10 @@ func Retry(retryFunc RetryFunc, opts ...Option) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var i uint
|
var i uint
|
||||||
|
var lastErr error
|
||||||
for i < config.retryTimes {
|
for i < config.retryTimes {
|
||||||
err := retryFunc()
|
lastErr = retryFunc()
|
||||||
if err == nil {
|
if lastErr == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +149,7 @@ func Retry(retryFunc RetryFunc, opts ...Option) error {
|
|||||||
lastSlash := strings.LastIndex(funcPath, "/")
|
lastSlash := strings.LastIndex(funcPath, "/")
|
||||||
funcName := funcPath[lastSlash+1:]
|
funcName := funcPath[lastSlash+1:]
|
||||||
|
|
||||||
return fmt.Errorf("function %s run failed after %d times retry", funcName, i)
|
return fmt.Errorf("function %s run failed after %d times retry, last error: %w", funcName, i, lastErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackoffStrategy is an interface that defines a method for calculating backoff intervals.
|
// BackoffStrategy is an interface that defines a method for calculating backoff intervals.
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ func ExampleRetryTimes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// function retry.ExampleRetryTimes.func1 run failed after 2 times retry
|
// function retry.ExampleRetryTimes.func1 run failed after 2 times retry, last error: error occurs
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRetry() {
|
func ExampleRetry() {
|
||||||
|
|||||||
@@ -15,14 +15,16 @@ func TestRetryFailed(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestRetryFailed")
|
assert := internal.NewAssert(t, "TestRetryFailed")
|
||||||
|
|
||||||
var number int
|
var number int
|
||||||
|
customError := errors.New("error occurs")
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
return errors.New("error occurs")
|
return customError
|
||||||
}
|
}
|
||||||
|
|
||||||
err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50))
|
err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50))
|
||||||
|
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
assert.Equal(errors.Is(err, customError), true)
|
||||||
assert.Equal(DefaultRetryTimes, number)
|
assert.Equal(DefaultRetryTimes, number)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user