mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
doc for retry
This commit is contained in:
parent
d45b867f98
commit
e8e6cf1429
@ -6,10 +6,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
RetryFailed = errors.New("All retry attempts failed.")
|
errorRetryFailed = errors.New("All retry attempts failed.")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Strategy is a way to retry on a specific function.
|
||||||
type Strategy interface {
|
type Strategy interface {
|
||||||
|
// On performs a retry on a specific function, until it doesn't return any error.
|
||||||
On(func() error) error
|
On(func() error) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,6 +19,7 @@ type retryer struct {
|
|||||||
NextDelay func(int) int
|
NextDelay func(int) int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// On implements Strategy.On.
|
||||||
func (r *retryer) On(method func() error) error {
|
func (r *retryer) On(method func() error) error {
|
||||||
attempt := 0
|
attempt := 0
|
||||||
for {
|
for {
|
||||||
@ -26,13 +29,14 @@ func (r *retryer) On(method func() error) error {
|
|||||||
}
|
}
|
||||||
delay := r.NextDelay(attempt)
|
delay := r.NextDelay(attempt)
|
||||||
if delay < 0 {
|
if delay < 0 {
|
||||||
return RetryFailed
|
return errorRetryFailed
|
||||||
}
|
}
|
||||||
<-time.After(time.Duration(delay) * time.Millisecond)
|
<-time.After(time.Duration(delay) * time.Millisecond)
|
||||||
attempt++
|
attempt++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Timed returns a retry strategy with fixed interval.
|
||||||
func Timed(attempts int, delay int) Strategy {
|
func Timed(attempts int, delay int) Strategy {
|
||||||
return &retryer{
|
return &retryer{
|
||||||
NextDelay: func(attempt int) int {
|
NextDelay: func(attempt int) int {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
TestError = errors.New("This is a fake error.")
|
errorTestOnly = errors.New("This is a fake error.")
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNoRetry(t *testing.T) {
|
func TestNoRetry(t *testing.T) {
|
||||||
@ -33,7 +33,7 @@ func TestRetryOnce(t *testing.T) {
|
|||||||
err := Timed(10, 1000).On(func() error {
|
err := Timed(10, 1000).On(func() error {
|
||||||
if called == 0 {
|
if called == 0 {
|
||||||
called++
|
called++
|
||||||
return TestError
|
return errorTestOnly
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -51,7 +51,7 @@ func TestRetryMultiple(t *testing.T) {
|
|||||||
err := Timed(10, 1000).On(func() error {
|
err := Timed(10, 1000).On(func() error {
|
||||||
if called < 5 {
|
if called < 5 {
|
||||||
called++
|
called++
|
||||||
return TestError
|
return errorTestOnly
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -69,12 +69,12 @@ func TestRetryExhausted(t *testing.T) {
|
|||||||
err := Timed(2, 1000).On(func() error {
|
err := Timed(2, 1000).On(func() error {
|
||||||
if called < 5 {
|
if called < 5 {
|
||||||
called++
|
called++
|
||||||
return TestError
|
return errorTestOnly
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
duration := time.Since(startTime)
|
duration := time.Since(startTime)
|
||||||
|
|
||||||
assert.Error(err).Equals(RetryFailed)
|
assert.Error(err).Equals(errorRetryFailed)
|
||||||
assert.Int64(int64(duration / time.Millisecond)).AtLeast(1900)
|
assert.Int64(int64(duration / time.Millisecond)).AtLeast(1900)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user