|
|
@@ -2,11 +2,12 @@ package retry
|
|
|
|
|
|
import (
|
|
|
"time"
|
|
|
+
|
|
|
"v2ray.com/core/common/errors"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- ErrRetryFailed = errors.New("All retry attempts failed.")
|
|
|
+ ErrRetryFailed = errors.New("Retry: All retry attempts failed.")
|
|
|
)
|
|
|
|
|
|
// Strategy is a way to retry on a specific function.
|
|
|
@@ -23,16 +24,21 @@ type retryer struct {
|
|
|
// On implements Strategy.On.
|
|
|
func (r *retryer) On(method func() error) error {
|
|
|
attempt := 0
|
|
|
+ accumulatedError := make([]error, 0, r.totalAttempt)
|
|
|
for attempt < r.totalAttempt {
|
|
|
err := method()
|
|
|
if err == nil {
|
|
|
return nil
|
|
|
}
|
|
|
+ numErrors := len(accumulatedError)
|
|
|
+ if numErrors == 0 || err.Error() != accumulatedError[numErrors-1].Error() {
|
|
|
+ accumulatedError = append(accumulatedError, err)
|
|
|
+ }
|
|
|
delay := r.nextDelay()
|
|
|
<-time.After(time.Duration(delay) * time.Millisecond)
|
|
|
attempt++
|
|
|
}
|
|
|
- return ErrRetryFailed
|
|
|
+ return errors.Base(ErrRetryFailed).Message(accumulatedError)
|
|
|
}
|
|
|
|
|
|
// Timed returns a retry strategy with fixed interval.
|