xhttp

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package xhttp provides utilities and functions for working with HTTP.

Index

Constants

View Source
const (
	// DefaultMaxIddleConns is the default maximum number of idle connections in
	// the pool.
	DefaultMaxIddleConns int = 100

	// DefaultMaxIddleConnsPerHost is the default maximum number of idle connections in
	// the pool per host.
	DefaultMaxIddleConnsPerHost int = 10

	// DefaultLRUClientSessionCacheCapacity is the default capacity of the LRU client session cache.
	DefaultLRUClientSessionCacheCapacity int = 64
)
View Source
const (
	// ErrCannotDrainResponse is returned when a response body cannot be drained.
	ErrCannotDrainResponse xerrors.Error = "cannot drain response body"

	// ErrCannotCloseResponse is returned when a response body cannot be closed.
	ErrCannotCloseResponse xerrors.Error = "cannot close response body"
)
View Source
const (
	// DefaultMinRetryDelay is the default minimum duration to wait before
	// retrying a request.
	DefaultMinRetryDelay time.Duration = 1 * time.Second

	// DefaultMaxRetryDelay is the default maximum duration to wait before
	// retrying a request.
	DefaultMaxRetryDelay time.Duration = 30 * time.Second
)

Variables

This section is empty.

Functions

func DefaultIsRetryable added in v1.1.0

func DefaultIsRetryable(resp *http.Response, err error) bool

DefaultIsRetryable defines the default logic to determine if a request should be retried.

It returns true if an error occurs or the response status code indicates a retry may be successful (e.g., 202, 408, 429, 502, 503, 504).

func DrainResponseBody

func DrainResponseBody(resp *http.Response) error

DrainResponseBody reads and discards the remaining content of the response body until EOF, then closes it. If an error occurs while draining or closing the response body, an error is returned.

func NewClient

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

NewClient creates a new HTTP client with sane defaults given the provided timeout.

Types

type RetryPolicy added in v1.1.0

type RetryPolicy struct {
	// IsRetryable determines whether a given response and error combination
	// should be retried.
	IsRetryable func(*http.Response, error) bool

	// MaxRetries is the maximum number of times a request will be retried.
	MaxRetries int

	// MinRetryDelay is the minimum duration to wait before retrying a request.
	MinRetryDelay time.Duration

	// MaxRetryDelay is the maximum duration to wait before retrying a request.
	MaxRetryDelay time.Duration
}

RetryPolicy defines a policy for retrying HTTP requests.

func (*RetryPolicy) Wait added in v1.1.0

func (p *RetryPolicy) Wait(ctx context.Context, attempt int) error

Wait calculates the time to wait before the next retry attempt and blocks until it is time to retry the request or the context is canceled. It incorporates an exponential backoff strategy with jitter to prevent the "thundering herd" problem.

If the context is canceled before the wait is over, Wait returns the context's error.

Jump to

Keyboard shortcuts

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