httputils

package
v0.0.0-...-4f221f3 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultHTTPTimeout is the default timeout for HTTP requests.
	DefaultHTTPTimeout = 30
	// DefaultHTTPRetryMax is the default maximum number of retries for HTTP requests.
	DefaultHTTPRetryMax = 10
)
View Source
const (
	// DefaultRetryCount is the default number of times to retry a request.
	DefaultRetryCount int = 5
	// DefaultBackoffFactor is the default factor (or base) to use for exponential backoff.
	DefaultBackoffFactor float64 = 1.0
	// DefaultMaxRetryDuration is the default maximum duration allowed to wait before retrying a request.
	DefaultMaxRetryDuration time.Duration = 30 * time.Second
)

Variables

View Source
var (
	// DefaultRetryStatusCodes is a list of status codes that should be retried.
	// This includes 500, 502, 503, 504 and 429 status codes.
	DefaultRetryStatusCodes = []int{
		http.StatusBadGateway,
		http.StatusServiceUnavailable,
		http.StatusGatewayTimeout,
		http.StatusInternalServerError,
		http.StatusTooManyRequests,
	}
)

Functions

func DefaultShouldRetry

func DefaultShouldRetry(err error, resp *http.Response, retryConfig *RetryConfig) bool

DefaultShouldRetry is a function that determines if a request should be retried. This will retry on network errors and the configured status codes.

func ExponentialBackoff

func ExponentialBackoff(retries int, retryConfig *RetryConfig) time.Duration

ExponentialBackoff returns a time.Duration for exponential backoff to be used in the retryable transport for a HTTP client.

func ExponentialBackoffWithJitter

func ExponentialBackoffWithJitter(retries int, retryConfig *RetryConfig) time.Duration

ExponentialBackoffWithJitter returns a time.Duration for exponential backoff to be used in the retryable transport for a HTTP client.

func NewHTTPClient

func NewHTTPClient(opts ...HTTPClientOption) *http.Client

NewHTTPClient creates a new instance of a HTTP client configured with a timeout.

Types

type HTTPClientOption

type HTTPClientOption func(*http.Client)

HTTPClientOption is a function that configures a http.Client.

func WithHTTPClientRetryableTransport

func WithHTTPClientRetryableTransport(
	retryConfig *RetryConfig,
) HTTPClientOption

WithHTTPClientRetryableTransport configures a http.Client instance with a retryable transport that support retries with exponential backoff.

func WithHTTPClientTimeout

func WithHTTPClientTimeout(timeout int) HTTPClientOption

WithHTTPClientTimeout configures a http.Client instance with a timeout, where timeout is in seconds.

func WithNativeHTTPClientInstrumentation

func WithNativeHTTPClientInstrumentation() HTTPClientOption

WithHTTPClientInstrumentation configures a http.Client instance with OpenTelemetry instrumentation.

type RetryConfig

type RetryConfig struct {
	// RetryCount is the number of times to retry a request.
	RetryCount int
	// BackoffFactor is the factor to use for exponential backoff.
	BackoffFactor float64
	// MaxRetryDuration is the maximum duration allowed to wait before retrying a request.
	MaxRetryDuration time.Duration
	// RetryStatusCodes is a list of HTTP status codes that should be retried.
	RetryStatusCodes []int
	// ShouldRetry is a function that determines if a request should be retried.
	ShouldRetry func(error, *http.Response, *RetryConfig) bool
	// BackoffFunction is a function that returns a time.Duration for exponential backoff.
	BackoffFunction func(int, *RetryConfig) time.Duration
}

RetryConfig provides configuration options for retrying requests with exponential backoff.

type RetryableTransport

type RetryableTransport struct {
	// contains filtered or unexported fields
}

RetryableTransport is a transport that retries requests based on the configuration.

func NewRetryableTransport

func NewRetryableTransport(
	transport http.RoundTripper,
	opts ...RetryableTransportOption,
) *RetryableTransport

NewRetryableTransport creates a new RetryableTransport with the provided underlying transport and options.

func (*RetryableTransport) RoundTrip

func (t *RetryableTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes a single HTTP transaction and returns a Response for the provided Request with retries based on the configuration.

type RetryableTransportOption

type RetryableTransportOption func(*RetryableTransport)

RetryableTransportOption is a function that configures a RetryableTransport.

func WithTransportRetryConfig

func WithTransportRetryConfig(
	provider *RetryConfig,
) RetryableTransportOption

WithTransportRetryConfig is an option that sets the retry configuration for a RetryableTransport.

Jump to

Keyboard shortcuts

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