http

package
v1.9.1-beta.3 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 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 added in v1.10.0

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 added in v1.10.0

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

func NewStallTransport added in v1.10.0

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

func ParseRetryAfter added in v1.10.0

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

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

func UserAgent added in v1.10.0

func UserAgent() string

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

func WithUserAgent added in v1.10.0

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 added in v1.10.0

type DownloadOption func(*downloadConfig)

DownloadOption customizes DownloadToFile.

func SkipIfSameSize added in v1.10.0

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 added in v1.10.0

func WithBackoff(backoff wait.Backoff) DownloadOption

WithBackoff overrides the retry backoff policy.

func WithClient added in v1.10.0

func WithClient(client *http.Client) DownloadOption

WithClient overrides the HTTP client used for the download.

func WithHeaders added in v1.10.0

func WithHeaders(headers map[string]string) DownloadOption

WithHeaders sets request headers sent on every download attempt.

func WithMode added in v1.10.0

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 added in v1.10.0

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 added in v1.10.0

func WithStallTimeout(d time.Duration) DownloadOption

WithStallTimeout swaps in a client with the given inactivity timeout.

type RetryConfig added in v1.10.0

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

type RetryTransport added in v1.10.0

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 added in v1.10.0

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

type StallTransport added in v1.10.0

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 added in v1.10.0

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