httpmw

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package httpmw provides outbound HTTP-client middleware: composable http.RoundTripper wrappers for resilient calls to upstream services.

RetryTransport retries throttled/unavailable responses (429, 503 by default) using the shared worker.Backoff policy, honoring a server-supplied Retry-After header (RFC 7231 §7.1.3) when present.

client := &http.Client{
	Transport: httpmw.NewRetryTransport(nil, httpmw.RetryConfig{
		Backoff: worker.Backoff{Base: 200 * time.Millisecond, Max: 5 * time.Second,
			MaxAttempts: 4, Jitter: 0.2},
	}),
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RetryConfig

type RetryConfig struct {
	// Backoff drives the delay between attempts and the total-attempt budget via
	// its MaxAttempts field. MaxAttempts <= 1 disables retries (a single try).
	Backoff worker.Backoff
	// Statuses are the response status codes that trigger a retry. When empty it
	// defaults to {429 Too Many Requests, 503 Service Unavailable}.
	Statuses []int
	// DisableRetryAfter turns off honoring the server's Retry-After header. By
	// default (false) a Retry-After value (delta-seconds or HTTP-date) on a
	// retryable response is used in preference to the backoff delay, clamped to
	// Backoff.Max when Max > 0.
	DisableRetryAfter bool
	// Rand returns a pseudo-random value in [0,1) used for backoff jitter. It
	// defaults to math/rand/v2; override it in tests for determinism.
	Rand func() float64
}

RetryConfig configures a RetryTransport.

type RetryTransport

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

RetryTransport is an http.RoundTripper that retries retryable responses according to its RetryConfig. The zero value is not usable; build it with NewRetryTransport.

func NewRetryTransport

func NewRetryTransport(next http.RoundTripper, cfg RetryConfig) *RetryTransport

NewRetryTransport wraps next (or http.DefaultTransport when nil) with retry behavior. The result is safe for concurrent use if next is.

func (*RetryTransport) RoundTrip

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

RoundTrip executes the request, retrying on configured statuses until the attempt budget is exhausted or the context is cancelled. The request body is rewound between attempts.

Jump to

Keyboard shortcuts

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