This commit is contained in:
RPRX 2020-11-25 19:01:53 +08:00
parent 47d23e9972
commit c7f7c08ead
711 changed files with 82154 additions and 2 deletions

View file

@ -0,0 +1,9 @@
package retry
import "github.com/xtls/xray-core/v1/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

64
common/retry/retry.go Normal file
View file

@ -0,0 +1,64 @@
package retry // import "github.com/xtls/xray-core/v1/common/retry"
//go:generate go run github.com/xtls/xray-core/v1/common/errors/errorgen
import (
"time"
)
var (
ErrRetryFailed = newError("all retry attempts failed")
)
// Strategy is a way to retry on a specific function.
type Strategy interface {
// On performs a retry on a specific function, until it doesn't return any error.
On(func() error) error
}
type retryer struct {
totalAttempt int
nextDelay func() uint32
}
// 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.Sleep(time.Duration(delay) * time.Millisecond)
attempt++
}
return newError(accumulatedError).Base(ErrRetryFailed)
}
// Timed returns a retry strategy with fixed interval.
func Timed(attempts int, delay uint32) Strategy {
return &retryer{
totalAttempt: attempts,
nextDelay: func() uint32 {
return delay
},
}
}
func ExponentialBackoff(attempts int, delay uint32) Strategy {
nextDelay := uint32(0)
return &retryer{
totalAttempt: attempts,
nextDelay: func() uint32 {
r := nextDelay
nextDelay += delay
return r
},
}
}

View file

@ -0,0 +1,98 @@
package retry_test
import (
"testing"
"time"
"github.com/xtls/xray-core/v1/common"
"github.com/xtls/xray-core/v1/common/errors"
. "github.com/xtls/xray-core/v1/common/retry"
)
var (
errorTestOnly = errors.New("this is a fake error")
)
func TestNoRetry(t *testing.T) {
startTime := time.Now().Unix()
err := Timed(10, 100000).On(func() error {
return nil
})
endTime := time.Now().Unix()
common.Must(err)
if endTime < startTime {
t.Error("endTime < startTime: ", startTime, " -> ", endTime)
}
}
func TestRetryOnce(t *testing.T) {
startTime := time.Now()
called := 0
err := Timed(10, 1000).On(func() error {
if called == 0 {
called++
return errorTestOnly
}
return nil
})
duration := time.Since(startTime)
common.Must(err)
if v := int64(duration / time.Millisecond); v < 900 {
t.Error("duration: ", v)
}
}
func TestRetryMultiple(t *testing.T) {
startTime := time.Now()
called := 0
err := Timed(10, 1000).On(func() error {
if called < 5 {
called++
return errorTestOnly
}
return nil
})
duration := time.Since(startTime)
common.Must(err)
if v := int64(duration / time.Millisecond); v < 4900 {
t.Error("duration: ", v)
}
}
func TestRetryExhausted(t *testing.T) {
startTime := time.Now()
called := 0
err := Timed(2, 1000).On(func() error {
called++
return errorTestOnly
})
duration := time.Since(startTime)
if errors.Cause(err) != ErrRetryFailed {
t.Error("cause: ", err)
}
if v := int64(duration / time.Millisecond); v < 1900 {
t.Error("duration: ", v)
}
}
func TestExponentialBackoff(t *testing.T) {
startTime := time.Now()
called := 0
err := ExponentialBackoff(10, 100).On(func() error {
called++
return errorTestOnly
})
duration := time.Since(startTime)
if errors.Cause(err) != ErrRetryFailed {
t.Error("cause: ", err)
}
if v := int64(duration / time.Millisecond); v < 4000 {
t.Error("duration: ", v)
}
}