2017-09-22 12:55:03 -07:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package realis
|
|
|
|
|
|
|
|
import (
|
2017-11-04 15:06:26 -07:00
|
|
|
"time"
|
|
|
|
|
2018-01-21 19:30:01 -08:00
|
|
|
"math/rand"
|
2018-02-06 12:44:27 -08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-09-22 12:55:03 -07:00
|
|
|
)
|
|
|
|
|
2018-01-21 19:30:01 -08:00
|
|
|
// Jitter returns a time.Duration between duration and duration + maxFactor *
|
|
|
|
// duration.
|
|
|
|
//
|
|
|
|
// This allows clients to avoid converging on periodic behavior. If maxFactor
|
|
|
|
// is 0.0, a suggested default value will be chosen.
|
|
|
|
func Jitter(duration time.Duration, maxFactor float64) time.Duration {
|
|
|
|
if maxFactor <= 0.0 {
|
|
|
|
maxFactor = 1.0
|
|
|
|
}
|
|
|
|
wait := duration + time.Duration(rand.Float64()*maxFactor*float64(duration))
|
|
|
|
return wait
|
|
|
|
}
|
2017-11-04 15:06:26 -07:00
|
|
|
|
2017-09-22 12:55:03 -07:00
|
|
|
// ConditionFunc returns true if the condition is satisfied, or an error
|
|
|
|
// if the loop should be aborted.
|
|
|
|
type ConditionFunc func() (done bool, err error)
|
|
|
|
|
2018-01-16 14:35:01 -08:00
|
|
|
// Modified version of the Kubernetes exponential-backoff code.
|
2017-09-22 12:55:03 -07:00
|
|
|
// ExponentialBackoff repeats a condition check with exponential backoff.
|
|
|
|
//
|
|
|
|
// It checks the condition up to Steps times, increasing the wait by multiplying
|
|
|
|
// the previous duration by Factor.
|
|
|
|
//
|
|
|
|
// If Jitter is greater than zero, a random amount of each duration is added
|
|
|
|
// (between duration and duration*(1+jitter)).
|
|
|
|
//
|
|
|
|
// If the condition never returns true, ErrWaitTimeout is returned. All other
|
|
|
|
// errors terminate immediately.
|
|
|
|
func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error {
|
2018-02-06 12:44:27 -08:00
|
|
|
var err error
|
|
|
|
var ok bool
|
2017-09-22 12:55:03 -07:00
|
|
|
duration := backoff.Duration
|
|
|
|
for i := 0; i < backoff.Steps; i++ {
|
|
|
|
if i != 0 {
|
|
|
|
adjusted := duration
|
|
|
|
if backoff.Jitter > 0.0 {
|
|
|
|
adjusted = Jitter(duration, backoff.Jitter)
|
|
|
|
}
|
|
|
|
time.Sleep(adjusted)
|
|
|
|
duration = time.Duration(float64(duration) * backoff.Factor)
|
|
|
|
}
|
2018-01-16 14:35:01 -08:00
|
|
|
|
2018-02-06 12:44:27 -08:00
|
|
|
ok, err = condition()
|
2018-01-16 14:35:01 -08:00
|
|
|
|
|
|
|
// If the function executed says it succeeded, stop retrying
|
|
|
|
if ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop retrying if the error is NOT temporary.
|
|
|
|
if err != nil {
|
|
|
|
if !IsTemporary(err) {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-22 12:55:03 -07:00
|
|
|
}
|
2018-01-16 14:35:01 -08:00
|
|
|
|
2017-09-22 12:55:03 -07:00
|
|
|
}
|
2018-02-06 12:44:27 -08:00
|
|
|
|
|
|
|
// Provide more information to the user wherever possible
|
|
|
|
if err != nil {
|
|
|
|
return NewTimeoutError(errors.Wrap(err, "Timed out while retrying"))
|
|
|
|
} else {
|
|
|
|
return NewTimeoutError(errors.New("Timed out while retrying"))
|
|
|
|
}
|
2017-09-22 12:55:03 -07:00
|
|
|
}
|