You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
3.5 KiB
134 lines
3.5 KiB
8 years ago
|
/*
|
||
5 years ago
|
* Minio Cloud Storage, (C) 2020 MinIO, Inc.
|
||
8 years ago
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
5 years ago
|
package retry
|
||
8 years ago
|
|
||
|
import (
|
||
5 years ago
|
"context"
|
||
5 years ago
|
"math"
|
||
8 years ago
|
"math/rand"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// MaxJitter will randomize over the full exponential backoff time
|
||
|
const MaxJitter = 1.0
|
||
|
|
||
8 years ago
|
// NoJitter disables the use of jitter for randomizing the
|
||
|
// exponential backoff time
|
||
8 years ago
|
const NoJitter = 0.0
|
||
|
|
||
5 years ago
|
// defaultTimer implements Timer interface using time.Timer
|
||
|
type defaultTimer struct {
|
||
|
timer *time.Timer
|
||
|
}
|
||
|
|
||
|
// C returns the timers channel which receives the current time when the timer fires.
|
||
|
func (t *defaultTimer) C() <-chan time.Time {
|
||
|
return t.timer.C
|
||
|
}
|
||
|
|
||
|
// Start starts the timer to fire after the given duration
|
||
|
// don't use this code concurrently.
|
||
|
func (t *defaultTimer) Start(duration time.Duration) {
|
||
|
if t.timer == nil {
|
||
|
t.timer = time.NewTimer(duration)
|
||
|
} else {
|
||
|
t.timer.Reset(duration)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Stop is called when the timer is not used anymore and resources may be freed.
|
||
|
func (t *defaultTimer) Stop() {
|
||
|
if t.timer != nil {
|
||
|
t.timer.Stop()
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
5 years ago
|
// NewTimerWithJitter creates a timer with exponentially increasing delays
|
||
8 years ago
|
// until the maximum retry attempts are reached. - this function is a fully
|
||
|
// configurable version, meant for only advanced use cases. For the most part
|
||
|
// one should use newRetryTimerSimple and newRetryTimer.
|
||
5 years ago
|
func NewTimerWithJitter(ctx context.Context, unit time.Duration, cap time.Duration, jitter float64) <-chan int {
|
||
8 years ago
|
attemptCh := make(chan int)
|
||
8 years ago
|
|
||
8 years ago
|
// normalize jitter to the range [0, 1.0]
|
||
5 years ago
|
jitter = math.Max(NoJitter, math.Min(MaxJitter, jitter))
|
||
8 years ago
|
|
||
8 years ago
|
// computes the exponential backoff duration according to
|
||
|
// https://www.awsarchitectureblog.com/2015/03/backoff.html
|
||
|
exponentialBackoffWait := func(attempt int) time.Duration {
|
||
8 years ago
|
// 1<<uint(attempt) below could overflow, so limit the value of attempt
|
||
5 years ago
|
const maxAttempt = 30
|
||
8 years ago
|
if attempt > maxAttempt {
|
||
|
attempt = maxAttempt
|
||
8 years ago
|
}
|
||
|
//sleep = random_between(0, min(cap, base * 2 ** attempt))
|
||
|
sleep := unit * time.Duration(1<<uint(attempt))
|
||
|
if sleep > cap {
|
||
|
sleep = cap
|
||
|
}
|
||
|
if jitter != NoJitter {
|
||
5 years ago
|
sleep -= time.Duration(rand.Float64() * float64(sleep) * jitter)
|
||
8 years ago
|
}
|
||
|
return sleep
|
||
|
}
|
||
|
|
||
|
go func() {
|
||
8 years ago
|
nextBackoff := 0
|
||
5 years ago
|
t := &defaultTimer{}
|
||
|
|
||
|
defer func() {
|
||
|
t.Stop()
|
||
|
}()
|
||
|
|
||
|
defer close(attemptCh)
|
||
|
|
||
8 years ago
|
// Channel used to signal after the expiry of backoff wait seconds.
|
||
8 years ago
|
for {
|
||
5 years ago
|
select {
|
||
8 years ago
|
case attemptCh <- nextBackoff:
|
||
8 years ago
|
nextBackoff++
|
||
5 years ago
|
case <-ctx.Done():
|
||
8 years ago
|
return
|
||
|
}
|
||
5 years ago
|
|
||
|
t.Start(exponentialBackoffWait(nextBackoff))
|
||
|
|
||
8 years ago
|
select {
|
||
5 years ago
|
case <-ctx.Done():
|
||
8 years ago
|
return
|
||
5 years ago
|
case <-t.C():
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
}()
|
||
8 years ago
|
|
||
|
// Start reading..
|
||
8 years ago
|
return attemptCh
|
||
|
}
|
||
8 years ago
|
|
||
|
// Default retry constants.
|
||
8 years ago
|
const (
|
||
5 years ago
|
defaultRetryUnit = 50 * time.Millisecond // 50 millisecond.
|
||
|
defaultRetryCap = 500 * time.Millisecond // 500 millisecond.
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
// NewTimer creates a timer with exponentially increasing delays
|
||
8 years ago
|
// until the maximum retry attempts are reached. - this function is a
|
||
|
// simpler version with all default values.
|
||
5 years ago
|
func NewTimer(ctx context.Context) <-chan int {
|
||
|
return NewTimerWithJitter(ctx, defaultRetryUnit, defaultRetryCap, MaxJitter)
|
||
8 years ago
|
}
|