httpretry

package
v0.98.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package httpretry retries idempotent HTTP calls with exponential backoff. It is meant for GET requests against external services (TMDB, Jackett, OpenSubtitles, indexer .torrent links, image search) where a transient failure — a network blip, a 429, or a 5xx — should not surface to the user as a hard error. Non-idempotent calls (POST that mutates) must NOT use this.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, client *http.Client, req *http.Request, p Policy) (*http.Response, error)

Do retries an idempotent request. The request must have a nil body or a non-nil GetBody so it can be replayed across attempts. Use DoFunc when you need to rebuild the request from scratch each time.

func DoFunc

func DoFunc(ctx context.Context, p Policy, build func() (*http.Request, error), do func(*http.Request) (*http.Response, error)) (*http.Response, error)

DoFunc runs do(build()) with retry/backoff. build is called once per attempt so each try gets a fresh request (and body). It honours ctx between attempts, respects Retry-After, and drains+closes the body of discarded responses so connections are reused.

func RetryOnStatuses

func RetryOnStatuses(extra ...int) func(*http.Response, error) bool

RetryOnStatuses returns a RetryOn that retries on the default transient conditions (network error, 429, 5xx) PLUS any extra status codes given — e.g. 404 for a cold indexer that briefly 404s a freshly-published .torrent link.

Types

type Policy

type Policy struct {
	MaxAttempts int           // total attempts including the first (default 3)
	BaseDelay   time.Duration // first backoff; doubles each attempt (default 300ms)
	MaxDelay    time.Duration // cap for any single wait (default 5s)
	// RetryOn decides whether a result is worth retrying. Default: network
	// errors, 429, and 5xx. Build a custom one with RetryOnStatuses.
	RetryOn func(resp *http.Response, err error) bool
}

Policy controls the retry behaviour. The zero value is usable — missing fields fall back to sane defaults (see withDefaults).

Jump to

Keyboard shortcuts

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