mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-29 04:15:24 -05:00
add exponential backoff as retry logic
This commit is contained in:
@@ -16,34 +16,43 @@ type Strategy interface {
|
||||
}
|
||||
|
||||
type retryer struct {
|
||||
NextDelay func(int) int
|
||||
totalAttempt int
|
||||
nextDelay func() uint32
|
||||
}
|
||||
|
||||
// On implements Strategy.On.
|
||||
func (r *retryer) On(method func() error) error {
|
||||
attempt := 0
|
||||
for {
|
||||
for attempt < r.totalAttempt {
|
||||
err := method()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
delay := r.NextDelay(attempt)
|
||||
if delay < 0 {
|
||||
return ErrRetryFailed
|
||||
}
|
||||
delay := r.nextDelay()
|
||||
<-time.After(time.Duration(delay) * time.Millisecond)
|
||||
attempt++
|
||||
}
|
||||
return ErrRetryFailed
|
||||
}
|
||||
|
||||
// Timed returns a retry strategy with fixed interval.
|
||||
func Timed(attempts int, delay int) Strategy {
|
||||
func Timed(attempts int, delay uint32) Strategy {
|
||||
return &retryer{
|
||||
NextDelay: func(attempt int) int {
|
||||
if attempt >= attempts {
|
||||
return -1
|
||||
}
|
||||
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
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user