mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
make util/retry more generic
This commit is contained in:
parent
c2a4369ba4
commit
2a0856a4d8
@ -42,12 +42,12 @@ var DefaultBackoff = wait.Backoff{
|
|||||||
Jitter: 0.1,
|
Jitter: 0.1,
|
||||||
}
|
}
|
||||||
|
|
||||||
// RetryConflict executes the provided function repeatedly, retrying if the server returns a conflicting
|
// OnError executes the provided function repeatedly, retrying if the server returns a specified
|
||||||
// write. Callers should preserve previous executions if they wish to retry changes. It performs an
|
// error. Callers should preserve previous executions if they wish to retry changes. It performs an
|
||||||
// exponential backoff.
|
// exponential backoff.
|
||||||
//
|
//
|
||||||
// var pod *api.Pod
|
// var pod *api.Pod
|
||||||
// err := RetryOnConflict(DefaultBackoff, func() (err error) {
|
// err := retry.OnError(DefaultBackoff, errors.IsConflict, func() (err error) {
|
||||||
// pod, err = c.Pods("mynamespace").UpdateStatus(podStatus)
|
// pod, err = c.Pods("mynamespace").UpdateStatus(podStatus)
|
||||||
// return
|
// return
|
||||||
// })
|
// })
|
||||||
@ -58,14 +58,14 @@ var DefaultBackoff = wait.Backoff{
|
|||||||
// ...
|
// ...
|
||||||
//
|
//
|
||||||
// TODO: Make Backoff an interface?
|
// TODO: Make Backoff an interface?
|
||||||
func RetryOnConflict(backoff wait.Backoff, fn func() error) error {
|
func OnError(backoff wait.Backoff, errorFunc func(error) bool, fn func() error) error {
|
||||||
var lastConflictErr error
|
var lastConflictErr error
|
||||||
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
|
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
|
||||||
err := fn()
|
err := fn()
|
||||||
switch {
|
switch {
|
||||||
case err == nil:
|
case err == nil:
|
||||||
return true, nil
|
return true, nil
|
||||||
case errors.IsConflict(err):
|
case errorFunc(err):
|
||||||
lastConflictErr = err
|
lastConflictErr = err
|
||||||
return false, nil
|
return false, nil
|
||||||
default:
|
default:
|
||||||
@ -77,3 +77,8 @@ func RetryOnConflict(backoff wait.Backoff, fn func() error) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RetryOnConflict executes the function function repeatedly, retrying if the server returns a conflicting
|
||||||
|
func RetryOnConflict(backoff wait.Backoff, fn func() error) error {
|
||||||
|
return OnError(backoff, errors.IsConflict, fn)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user