httpclient

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// Retry strategy values consumed by RetryConfig.Strategy.
	RetryStrategyNone    = "none"
	RetryStrategyBackoff = "backoff"

	// RetryStrategyDefault is used when retry strategy is not explicitly set.
	RetryStrategyDefault = RetryStrategyBackoff

	// DefaultRetryMaxAttempts is the default total number of attempts,
	// including the first request.
	// Example: with factor=2 and 3 total attempts there are 2 waits: 1s then 2s.
	DefaultRetryMaxAttempts = 3

	// DefaultRetryInitialIntervalMS is the default initial backoff interval in milliseconds.
	DefaultRetryInitialIntervalMS = 1_000

	// DefaultRetryMaxIntervalMS is the default maximum backoff interval in milliseconds.
	DefaultRetryMaxIntervalMS = 60_000

	// DefaultRetryBackoffFactor is the default exponential backoff multiplier.
	DefaultRetryBackoffFactor = 2.0

	// DefaultRetryConnectionErrors controls whether transport-level connection
	// errors are retried by default.
	DefaultRetryConnectionErrors = false

	// MaxRetryMaxAttempts is the upper bound for total attempts.
	// Allowed attempts range in practice: [1..MaxRetryMaxAttempts] when enabled.
	MaxRetryMaxAttempts = 10

	// MinRetryInitialIntervalMS is the minimum configurable initial backoff interval.
	// Allowed range: [MinRetryInitialIntervalMS..MaxRetryInitialIntervalMS].
	// Lower values would hammer the server too aggressively.
	MinRetryInitialIntervalMS = 500

	// MaxRetryInitialIntervalMS is the maximum configurable initial backoff interval.
	// Allowed range: [MinRetryInitialIntervalMS..MaxRetryInitialIntervalMS].
	MaxRetryInitialIntervalMS = 30_000

	// MinRetryMaxIntervalMS is the minimum configurable backoff ceiling.
	// Allowed range: [MinRetryMaxIntervalMS..MaxRetryMaxIntervalMS].
	MinRetryMaxIntervalMS = 1_000

	// MaxRetryMaxIntervalMS is the maximum configurable backoff ceiling.
	// Allowed range: [MinRetryMaxIntervalMS..MaxRetryMaxIntervalMS].
	// 2 minutes covers any realistic API rate-limit reset window.
	MaxRetryMaxIntervalMS = 120_000

	// MinRetryBackoffFactor is the minimum configurable exponential backoff multiplier.
	// Lower values behave like steady polling instead of backing off under failure.
	MinRetryBackoffFactor = 1.5

	// MaxRetryBackoffFactor is the maximum configurable exponential backoff multiplier.
	// Higher values produce surprising waits without improving service protection.
	MaxRetryBackoffFactor = 3.0

	// MaxRetryTotalBackoffMS is the maximum cumulative sleep budget across all
	// retry attempts for one request. This limits retry amplification while still
	// allowing long enough windows for Konnect eventual consistency cases.
	MaxRetryTotalBackoffMS = 180_000
)
View Source
const (
	DefaultHTTPClientTimeout = 60 * time.Second
)

Variables

This section is empty.

Functions

func EstimatedRetryBackoffMS added in v1.0.0

func EstimatedRetryBackoffMS(cfg RetryConfig) int

EstimatedRetryBackoffMS returns the nominal cumulative retry sleep budget in milliseconds, without jitter. MaxAttempts includes the first request, so a MaxAttempts value of 3 has two retry sleeps.

func NewHTTPClient added in v0.8.0

func NewHTTPClient(timeout time.Duration) *http.Client

func NewHTTPClientWithConfig added in v0.8.0

func NewHTTPClientWithConfig(cfg ClientConfig) *http.Client

Types

type ClientConfig added in v0.8.0

type ClientConfig struct {
	Timeout          time.Duration
	Jar              http.CookieJar
	TransportOptions TransportOptions
}

type LoggingHTTPClient

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

LoggingHTTPClient wraps an HTTP client to add centralized request/response logging.

func NewLoggingHTTPClient

func NewLoggingHTTPClient(logger *slog.Logger) *LoggingHTTPClient

NewLoggingHTTPClient creates a new logging HTTP client

func NewLoggingHTTPClientWithClient

func NewLoggingHTTPClientWithClient(client *http.Client, logger *slog.Logger) *LoggingHTTPClient

NewLoggingHTTPClientWithClient wraps an existing HTTP client

func (*LoggingHTTPClient) Do

func (c *LoggingHTTPClient) Do(req *http.Request) (*http.Response, error)

Do implements the HTTPClient interface with logging

type RetryClientOption added in v1.0.0

type RetryClientOption func(*RetryingHTTPClient)

RetryClientOption is a functional option for RetryingHTTPClient.

func WithRetryableCodes added in v1.0.0

func WithRetryableCodes(codes ...int) RetryClientOption

WithRetryableCodes overrides the set of HTTP status codes that trigger a retry.

func WithRetryableMethods added in v1.0.0

func WithRetryableMethods(methods ...string) RetryClientOption

WithRetryableMethods restricts retries to specific HTTP methods. An empty slice (the default) means all methods are eligible for retry.

type RetryConfig added in v1.0.0

type RetryConfig struct {
	Strategy              string
	MaxAttempts           int
	InitialIntervalMS     int
	MaxIntervalMS         int
	BackoffFactor         float64
	RetryConnectionErrors bool
}

RetryConfig holds retry/backoff parameters resolved from flags/config. All time intervals are stored as milliseconds. MaxAttempts is total attempts including the first request.

type RetryingHTTPClient added in v1.0.0

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

RetryingHTTPClient wraps an inner kk.HTTPClient and transparently retries requests that fail with retryable HTTP status codes, using exponential backoff with jitter. It is intended to be placed in the HTTP client chain between the RefreshingHTTPClient and the SDK so that retries are handled at the transport layer rather than per-operation via SDK options.

func NewRetryingHTTPClient added in v1.0.0

func NewRetryingHTTPClient(
	inner kk.HTTPClient,
	cfg RetryConfig,
	logger *slog.Logger,
	opts ...RetryClientOption,
) *RetryingHTTPClient

NewRetryingHTTPClient creates a new RetryingHTTPClient. When cfg.Strategy is RetryStrategyNone or cfg.MaxAttempts <= 1, Do simply delegates to inner without any retry logic.

func (*RetryingHTTPClient) Do added in v1.0.0

Do executes the request, retrying on retryable status codes with exponential backoff and jitter. Retry attempts and outcomes are logged at warn level.

type TransportOptions added in v0.8.0

type TransportOptions struct {
	TCPUserTimeout            time.Duration
	DisableKeepAlives         bool
	RecycleConnectionsOnError bool
}

Jump to

Keyboard shortcuts

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