2021-02-16 15:31:50 -05:00
|
|
|
package retry
|
2015-10-13 06:27:50 -04:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
|
2017-04-08 19:43:25 -04:00
|
|
|
|
2015-10-13 06:27:50 -04:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-05-19 17:28:52 -04:00
|
|
|
var ErrRetryFailed = newError("all retry attempts failed")
|
2015-10-13 06:27:50 -04:00
|
|
|
|
2015-12-02 03:58:00 -05:00
|
|
|
// Strategy is a way to retry on a specific function.
|
2015-12-02 03:46:19 -05:00
|
|
|
type Strategy interface {
|
2015-12-02 06:47:54 -05:00
|
|
|
// On performs a retry on a specific function, until it doesn't return any error.
|
2015-10-13 06:27:50 -04:00
|
|
|
On(func() error) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type retryer struct {
|
2016-11-20 15:47:51 -05:00
|
|
|
totalAttempt int
|
|
|
|
nextDelay func() uint32
|
2015-10-13 06:27:50 -04:00
|
|
|
}
|
|
|
|
|
2015-12-02 03:58:00 -05:00
|
|
|
// On implements Strategy.On.
|
2015-10-13 06:27:50 -04:00
|
|
|
func (r *retryer) On(method func() error) error {
|
|
|
|
attempt := 0
|
2017-02-10 05:41:50 -05:00
|
|
|
accumulatedError := make([]error, 0, r.totalAttempt)
|
2016-11-20 15:47:51 -05:00
|
|
|
for attempt < r.totalAttempt {
|
2015-10-13 06:27:50 -04:00
|
|
|
err := method()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-10 05:41:50 -05:00
|
|
|
numErrors := len(accumulatedError)
|
|
|
|
if numErrors == 0 || err.Error() != accumulatedError[numErrors-1].Error() {
|
|
|
|
accumulatedError = append(accumulatedError, err)
|
|
|
|
}
|
2016-11-20 15:47:51 -05:00
|
|
|
delay := r.nextDelay()
|
2017-05-08 18:01:15 -04:00
|
|
|
time.Sleep(time.Duration(delay) * time.Millisecond)
|
2015-10-13 18:57:00 -04:00
|
|
|
attempt++
|
2015-10-13 06:27:50 -04:00
|
|
|
}
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError(accumulatedError).Base(ErrRetryFailed)
|
2015-10-13 06:27:50 -04:00
|
|
|
}
|
|
|
|
|
2015-12-02 03:58:00 -05:00
|
|
|
// Timed returns a retry strategy with fixed interval.
|
2016-11-20 15:47:51 -05:00
|
|
|
func Timed(attempts int, delay uint32) Strategy {
|
2015-10-13 06:27:50 -04:00
|
|
|
return &retryer{
|
2016-11-20 15:47:51 -05:00
|
|
|
totalAttempt: attempts,
|
|
|
|
nextDelay: func() uint32 {
|
2015-10-13 06:27:50 -04:00
|
|
|
return delay
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 15:47:51 -05:00
|
|
|
|
|
|
|
func ExponentialBackoff(attempts int, delay uint32) Strategy {
|
|
|
|
nextDelay := uint32(0)
|
|
|
|
return &retryer{
|
|
|
|
totalAttempt: attempts,
|
|
|
|
nextDelay: func() uint32 {
|
|
|
|
r := nextDelay
|
|
|
|
nextDelay += delay
|
|
|
|
return r
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|