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 ¶
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 ¶
func NewRetryTransport(base http.RoundTripper, cfg RetryConfig) http.RoundTripper
func NewStallTransport ¶
func NewStallTransport(base http.RoundTripper, timeout time.Duration) http.RoundTripper
func ParseRetryAfter ¶
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 ¶
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 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.
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).