retry-on-conflict

command module
v0.0.0-...-e5add16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

Retry on Conflict

retry.RetryOnConflict(backoff wait.Backoff, fn func() error) error is a useful function when creating custom controllers or operators. RetryOnConflict can help reduce the number of transient errors within your program. For more in depth information see the official docs.

Usage

Typical usage for RetryOnConflict occurs when there's a possibility of many clients interacting with a resource at the same time or multiple controllers are interacting with the same resource, or a mixture of both.

A typical implementation would be:

err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
    // Always fetch the new version of the resource
    pod, err := c.Pods("mynamespace").Get(name, metav1.GetOptions{})
    if err != nil {
        return err
    }

    // *************
    // Make some form of long running change, hitting external apis,
    // spinning up and validating external resources, etc
    // *************

    // Try to update
    _, err = c.Pods("mynamespace").UpdateStatus(pod)
    // You have to return err itself here (not wrapped inside another error)
    // so that RetryOnConflict can identify it correctly.
    return err
})
if err != nil {
    return err
}

This Example

This example is intended to fail once. The following is the expected outcome:

$ go run main.go 
Operation cannot be fulfilled on configmaps "foobar": the object has been modified; please apply your changes to the latest version and try again
Successfully updated ConfigMap

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL