retry

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 9 Imported by: 5

README

retry

Simple retry package to get a retryable *http.Client or http.RoundTripper that wraps github.com/cenkalti/backoff/v5.

How to use

You can customise retry.Config as documented.

Retryable http.Client
import (
    "github.com/smithy-security/pkg/retry"
)

...

client, err := retry.NewClient(retry.Config{
	MaxRetries: 10,
})
...
Retryable http.RoundTripper
import (
    "github.com/smithy-security/pkg/retry"
)

...

rt, err := retry.NewRoundTripper(retry.Config{
	MaxRetries: 10,
})
...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(config Config) (*http.Client, error)

NewClient returns a new http.Client with retry behaviour.

func NewRoundTripper

func NewRoundTripper(config Config) (http.RoundTripper, error)

NewRoundTripper returns a new http.RoundTripper with retry behaviour.

Types

type Config

type Config struct {
	BaseClient *http.Client
	// BaseTransport allows to specify a base http.RoundTripper.
	BaseTransport http.RoundTripper
	// Logger allows to specify a custom logger. *slog.Logger will satisfy this.
	Logger Logger
	// NextRetryInSecondsFunc allows to specify a custom retry function.
	// By default, exponential fibonacci like function is used.
	NextRetryInSecondsFunc NextRetryInSeconds
	// MaxRetries allows to specify the number of max retries before returning a fatal error.
	// 5 is the default.
	MaxRetries uint
	// RetryableStatusCodes allows to specify the retryable status codes.
	// defaultRetryableStatusCodes are the default.
	RetryableStatusCodes map[int]struct{}
	// AcceptedStatusCodes allows to specify the non-retryable status codes.
	// defaultAcceptedStatusCodes are the default.
	AcceptedStatusCodes map[int]struct{}
	// ResponseInfoLoggerFunc when set will be used to check the response
	// returned by the API
	ResponseInfoLoggerFunc ResponseInfoLogger
}

Config allows configuring the client.

func (Config) Validate

func (c Config) Validate() error

Validate validates the client configuration.

type Logger

type Logger interface {
	Error(msg string, keysAndValues ...any)
	Info(msg string, keysAndValues ...any)
	Debug(msg string, keysAndValues ...any)
	Warn(msg string, keysAndValues ...any)
}

Logger allows to inject a custom logger in the client.

type NextRetryInSeconds

type NextRetryInSeconds func(currAttempt uint) int

NextRetryInSeconds allows customising the behaviour for the calculating the next retry.

type ResponseInfoLogger added in v0.0.4

type ResponseInfoLogger func(res *http.Response, logger Logger)

ResponseInfoLogger is allowed to inspect the retryable response and print more info about it that might be useful to the caller for debugging

Jump to

Keyboard shortcuts

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