Documentation
¶
Overview ¶
Package pace decides when the next request may go: a token bucket per host, jittered exponential backoff between attempts, a retry policy, and a robots.txt check.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RetryAfter ¶
RetryAfter reads a Retry-After header, in seconds or as an HTTP date.
Types ¶
type Backoff ¶
type Backoff struct {
Base time.Duration // default 500ms
Max time.Duration // default 30s
Factor float64 // default 2
Jitter float64 // 0 to 1, default 0.5; negative disables jitter
// Rand returns a number in [0, 1). Nil uses math/rand/v2.
Rand func() float64
}
Backoff computes the delay before a retry. The delay grows as Base * Factor^attempt, is capped at Max, and is then spread by Jitter: with Jitter 0.5 the delay lands anywhere from half to one and a half times the computed value (still capped at Max), so retries from many workers do not arrive together.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter is a token bucket per host. Hosts without their own setting share the default rate, each with a bucket of its own. It is safe for concurrent use.
func NewLimiter ¶
NewLimiter returns a Limiter allowing perSecond requests per second to each host, with bursts of up to burst. A burst below 1 is raised to 1.
type Policy ¶
type Policy struct {
// MaxAttempts is the total number of attempts, including the first.
// Default 3.
MaxAttempts int
Backoff Backoff
// MaxRetryAfter caps how long a server's Retry-After is honored.
// Default 60s.
MaxRetryAfter time.Duration
}
Policy decides whether an attempt is retried and how long to wait first.
func (Policy) ShouldRetry ¶
ShouldRetry reports whether an attempt that ended this way is worth repeating: on a network error (but not a canceled context), or a 429 or 5xx that was not detected as a block. A block or captcha is never retried: the site has said no, and asking again is not the answer. A 404 is never retried either.
type Robots ¶
type Robots struct {
// contains filtered or unexported fields
}
Robots answers whether robots.txt allows a URL, fetching each site's file once and caching it.
A missing robots.txt (any 4xx) allows everything; a 5xx disallows everything until the entry expires, following Google's reading of the standard. A file that cannot be parsed counts as missing. A network error is returned and not cached.
func NewRobots ¶
NewRobots returns a Robots that fetches through f and keeps each file for ttl (24 hours when ttl is zero or negative).
func (*Robots) Allowed ¶
func (r *Robots) Allowed(ctx context.Context, target, agent string, base fetch.Request) (bool, error)
Allowed reports whether agent may fetch target. The robots.txt request is built from base, so it goes out through the same proxy, profile and cookie jar as the page it guards.