mirror of
https://github.com/kubernetes/client-go.git
synced 2025-08-10 11:41:47 +00:00
Restore retry.RetryOnConflict docs, fix up retry.OnError docs/naming
Kubernetes-commit: b098e013242be8b480340d30063388b0020ae32e
This commit is contained in:
parent
67a413f31a
commit
0e80a77286
@ -42,12 +42,35 @@ var DefaultBackoff = wait.Backoff{
|
|||||||
Jitter: 0.1,
|
Jitter: 0.1,
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnError executes the provided function repeatedly, retrying if the server returns a specified
|
// OnError allows the caller to retry fn in case the error returned by fn is retriable
|
||||||
// error. Callers should preserve previous executions if they wish to retry changes. It performs an
|
// according to the provided function. backoff defines the maximum retries and the wait
|
||||||
|
// interval between two retries.
|
||||||
|
func OnError(backoff wait.Backoff, retriable func(error) bool, fn func() error) error {
|
||||||
|
var lastErr error
|
||||||
|
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
|
||||||
|
err := fn()
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
return true, nil
|
||||||
|
case retriable(err):
|
||||||
|
lastErr = err
|
||||||
|
return false, nil
|
||||||
|
default:
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err == wait.ErrWaitTimeout {
|
||||||
|
err = lastErr
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// RetryOnConflict executes the function function repeatedly, retrying if the server returns a conflicting
|
||||||
|
// write. 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 := retry.OnError(DefaultBackoff, errors.IsConflict, func() (err error) {
|
// err := RetryOnConflict(DefaultBackoff, func() (err error) {
|
||||||
// pod, err = c.Pods("mynamespace").UpdateStatus(podStatus)
|
// pod, err = c.Pods("mynamespace").UpdateStatus(podStatus)
|
||||||
// return
|
// return
|
||||||
// })
|
// })
|
||||||
@ -58,27 +81,6 @@ var DefaultBackoff = wait.Backoff{
|
|||||||
// ...
|
// ...
|
||||||
//
|
//
|
||||||
// TODO: Make Backoff an interface?
|
// TODO: Make Backoff an interface?
|
||||||
func OnError(backoff wait.Backoff, errorFunc func(error) bool, fn func() error) error {
|
|
||||||
var lastConflictErr error
|
|
||||||
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
|
|
||||||
err := fn()
|
|
||||||
switch {
|
|
||||||
case err == nil:
|
|
||||||
return true, nil
|
|
||||||
case errorFunc(err):
|
|
||||||
lastConflictErr = err
|
|
||||||
return false, nil
|
|
||||||
default:
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if err == wait.ErrWaitTimeout {
|
|
||||||
err = lastConflictErr
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// RetryOnConflict executes the function function repeatedly, retrying if the server returns a conflicting
|
|
||||||
func RetryOnConflict(backoff wait.Backoff, fn func() error) error {
|
func RetryOnConflict(backoff wait.Backoff, fn func() error) error {
|
||||||
return OnError(backoff, errors.IsConflict, fn)
|
return OnError(backoff, errors.IsConflict, fn)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user