forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.go
36 lines (31 loc) · 1.23 KB
/
wait.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package retry
import (
"context"
"k8s.io/apimachinery/pkg/util/wait"
)
// TODO: This should be added to k8s.io/client-go/util/retry
// ConditionWithContextFunc returns true if the condition is satisfied, or an error
// if the loop should be aborted. The context passed to condition function allow function body
// to return faster than context.Done().
type ConditionWithContextFunc func(ctx context.Context) (done bool, err error)
// ExponentialBackoffWithContext repeats a condition check with exponential backoff and stop repeating
// when the context passed to this function is done.
//
// 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 ExponentialBackoffWithContext(ctx context.Context, backoff wait.Backoff, condition ConditionWithContextFunc) error {
return wait.ExponentialBackoff(backoff, func() (bool, error) {
select {
case <-ctx.Done():
return false, wait.ErrWaitTimeout
default:
return condition(ctx)
}
})
}