http

package
v1.7.0-beta.5 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MPL-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultStallTimeout = 60 * time.Second

Variables

View Source
var DefaultDownloadBackoff = wait.Backoff{
	Duration: 1 * time.Second,
	Factor:   2.0,
	Steps:    3,
}
View Source
var DefaultRetry = RetryConfig{MaxAttempts: 3, BaseDelay: time.Second, MaxDelay: 30 * time.Second}
View Source
var ErrStalled = errors.New("stalled: no progress within timeout")

Functions

func DownloadToFile

func DownloadToFile(ctx context.Context, url, destPath string, opts ...DownloadOption) error

DownloadToFile downloads url into destPath, retrying transient failures (connection errors, 5xx, 429, truncated transfers) and failing fast on 4xx.

func GetHTTPClient

func GetHTTPClient() *http.Client

GetHTTPClient returns the shared client: base transport, stall guard, user-agent, and retry of idempotent requests. The stall guard bounds idle header/body waits, so it must not be used for intentionally-idle streams.

func NewRetryTransport

func NewRetryTransport(base http.RoundTripper, cfg RetryConfig) http.RoundTripper

func NewStallTransport

func NewStallTransport(base http.RoundTripper, timeout time.Duration) http.RoundTripper

func ParseRetryAfter

func ParseRetryAfter(resp *http.Response) (time.Duration, bool)

ParseRetryAfter reads a Retry-After header in delta-seconds or HTTP-date form.

func UserAgent

func UserAgent() string

UserAgent is the value sent on requests made through the package's clients.

func WithUserAgent

func WithUserAgent(base http.RoundTripper, agent string) http.RoundTripper

WithUserAgent wraps base so requests that don't already carry a User-Agent header get the given value. An empty agent returns base unchanged.

Types

type DownloadOption

type DownloadOption func(*downloadConfig)

DownloadOption customizes DownloadToFile.

func SkipIfSameSize

func SkipIfSameSize() DownloadOption

SkipIfSameSize skips the download when destPath already exists and its size matches the advertised Content-Length, reusing the cached file.

func WithBackoff

func WithBackoff(backoff wait.Backoff) DownloadOption

WithBackoff overrides the retry backoff policy.

func WithClient

func WithClient(client *http.Client) DownloadOption

WithClient overrides the HTTP client used for the download.

func WithHeaders

func WithHeaders(headers map[string]string) DownloadOption

WithHeaders sets request headers sent on every download attempt.

func WithMode

func WithMode(mode os.FileMode) DownloadOption

WithMode sets the permission bits for the destination file (default 0o600), for downloads that must be executable.

func WithProgress

func WithProgress(wrap func(r io.Reader, totalSize int64) io.Reader) DownloadOption

WithProgress wraps the response body on each attempt, e.g. to report download progress. wrap receives the body reader and the advertised size (-1 when unknown) and returns the reader to copy from.

func WithStallTimeout

func WithStallTimeout(d time.Duration) DownloadOption

WithStallTimeout swaps in a client with the given inactivity timeout.

type RetryConfig

type RetryConfig struct {
	MaxAttempts int // <=1 disables retry
	BaseDelay   time.Duration
	MaxDelay    time.Duration
}

type RetryTransport

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

RetryTransport retries idempotent requests on connection errors, 5xx and 429, honoring Retry-After. Non-idempotent and cancelled requests pass through.

func (*RetryTransport) RoundTrip

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

type StallTransport

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

StallTransport aborts a request that makes no progress within timeout. Unsafe for intentionally-idle responses (long-poll, watch streams).

func (*StallTransport) RoundTrip

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

Jump to

Keyboard shortcuts

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