retry

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

AWS API Retry Package

This package provides utilities for retrying AWS API calls with exponential backoff. It is designed to be used with the AWS SDK v2 for Go.

Features

  • Exponential Backoff: Automatically retries operations with exponential backoff
  • Error Categorization: Categorizes errors as retryable or non-retryable
  • Context Support: Respects context cancellation and deadlines
  • Logging: Provides detailed logging of retry attempts

Usage

Basic Usage
import (
    "context"
    "github.com/johnlam90/aws-multi-eni-controller/pkg/aws/retry"
    "github.com/go-logr/logr"
)

func ExampleFunction(ctx context.Context, logger logr.Logger) error {
    operation := "describe resource"
    
    err := retry.WithExponentialBackoff(
        ctx,
        logger,
        operation,
        func() (bool, error) {
            // Your AWS API call here
            result, err := awsClient.SomeOperation(ctx, input)
            if err != nil {
                return false, err
            }
            return true, nil
        },
        retry.DefaultRetryableErrors,
        retry.DefaultBackoff(),
    )
    
    return err
}
Custom Error Categorization
func CustomRetryableErrors(err error) bool {
    if err == nil {
        return false
    }
    
    errMsg := err.Error()
    return strings.Contains(errMsg, "RequestLimitExceeded") ||
        strings.Contains(errMsg, "Throttling") ||
        strings.Contains(errMsg, "ConnectionReset")
}

func ExampleWithCustomErrorCategorization(ctx context.Context, logger logr.Logger) error {
    operation := "custom operation"
    
    err := retry.WithExponentialBackoff(
        ctx,
        logger,
        operation,
        func() (bool, error) {
            // Your AWS API call here
            return true, nil
        },
        CustomRetryableErrors,
        retry.DefaultBackoff(),
    )
    
    return err
}
Custom Backoff
func ExampleWithCustomBackoff(ctx context.Context, logger logr.Logger) error {
    operation := "long-running operation"
    
    customBackoff := wait.Backoff{
        Steps:    10,
        Duration: 2 * time.Second,
        Factor:   1.5,
        Jitter:   0.2,
    }
    
    err := retry.WithExponentialBackoff(
        ctx,
        logger,
        operation,
        func() (bool, error) {
            // Your AWS API call here
            return true, nil
        },
        retry.DefaultRetryableErrors,
        customBackoff,
    )
    
    return err
}

Helper Functions

  • WithContext: Wraps a RetryableFunc with context cancellation
  • WithLogging: Wraps a RetryableFunc with logging
  • DefaultBackoff: Returns the default backoff configuration
  • DefaultRetryableErrors: Checks if an error is a retryable AWS error

Documentation

Overview

Package retry provides utilities for retrying AWS API calls with exponential backoff.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultBackoff

func DefaultBackoff() wait.Backoff

DefaultBackoff returns the default backoff configuration

func DefaultRetryableErrors

func DefaultRetryableErrors(err error) bool

DefaultRetryableErrors checks if an error is a retryable AWS error

func WithExponentialBackoff

func WithExponentialBackoff(
	ctx context.Context,
	logger logr.Logger,
	operation string,
	fn RetryableFunc,
	isRetryable ErrorCategorizer,
	backoff wait.Backoff,
) error

WithExponentialBackoff retries a function with exponential backoff

Types

type ErrorCategorizer

type ErrorCategorizer func(error) bool

ErrorCategorizer categorizes errors as retryable or not

type RetryableFunc

type RetryableFunc func() (bool, error)

RetryableFunc is a function that can be retried

func WithContext

func WithContext(ctx context.Context, fn RetryableFunc) RetryableFunc

WithContext wraps a RetryableFunc with context cancellation

func WithLogging

func WithLogging(logger logr.Logger, operation string, fn RetryableFunc) RetryableFunc

WithLogging wraps a RetryableFunc with logging

Jump to

Keyboard shortcuts

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