Documentation
¶
Index ¶
- Constants
- Variables
- func DownloadToFile(ctx context.Context, url, destPath string, opts ...DownloadOption) error
- func GetHTTPClient() *http.Client
- func NewRetryTransport(base http.RoundTripper, cfg RetryConfig) http.RoundTripper
- func NewStallTransport(base http.RoundTripper, timeout time.Duration) http.RoundTripper
- func ParseRetryAfter(resp *http.Response) (time.Duration, bool)
- func UserAgent() string
- func WithUserAgent(base http.RoundTripper, agent string) http.RoundTripper
- type DownloadOption
- func SkipIfSameSize() DownloadOption
- func WithBackoff(backoff wait.Backoff) DownloadOption
- func WithClient(client *http.Client) DownloadOption
- func WithHeaders(headers map[string]string) DownloadOption
- func WithMode(mode os.FileMode) DownloadOption
- func WithProgress(wrap func(r io.Reader, totalSize int64) io.Reader) DownloadOption
- func WithStallTimeout(d time.Duration) DownloadOption
- type RetryConfig
- type RetryTransport
- type StallTransport
Constants ¶
const DefaultStallTimeout = 60 * time.Second
Variables ¶
var DefaultDownloadBackoff = wait.Backoff{ Duration: 1 * time.Second, Factor: 2.0, Steps: 3, }
var DefaultRetry = RetryConfig{MaxAttempts: 3, BaseDelay: time.Second, MaxDelay: 30 * time.Second}
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 ¶
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
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
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 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.
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).