Documentation
¶
Overview ¶
Package httpx provides small HTTP transport helpers shared across the proxy.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCircuitOpen = errors.New("upstream throttled: circuit open")
ErrCircuitOpen is returned (wrapped) when a request is fast-failed because the destination host is in its post-throttle cooldown.
Functions ¶
This section is empty.
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker is an http.RoundTripper that fast-fails requests to a host that recently returned HTTP 429/503 instead of retrying it. Retrying a throttled upstream "harder" only deepens the penalty (per Sonatype's Maven Central guidance), so on a 429/503 the breaker opens for a cooldown — honoring Retry-After, else jittered exponential backoff — during which every request to that host fails immediately with ErrCircuitOpen. The handler's multi-upstream fallback then serves from a mirror right away instead of waiting on or hammering the throttled host. The breaker closes on the first success after the cooldown.
It performs no retries and never blocks, so it does not consume the caller's request timeout while a host is throttled.
func NewCircuitBreaker ¶
func NewCircuitBreaker(base http.RoundTripper, baseDelay, maxDelay time.Duration) *CircuitBreaker
NewCircuitBreaker wraps base. baseDelay and maxDelay bound the exponential cooldown used when an upstream sends no Retry-After. A nil base uses http.DefaultTransport.
type ConcurrencyLimiter ¶
type ConcurrencyLimiter struct {
// contains filtered or unexported fields
}
ConcurrencyLimiter is an http.RoundTripper that caps the number of concurrent in-flight requests per destination host. Parallel dependency resolution (Gradle, Maven, npm) fans out many simultaneous requests to a single registry; left unbounded that burst trips the registry's rate limit, which responds with HTTP 429. The cap keeps outbound concurrency under the limit so throttling does not happen in the first place.
Requests beyond the cap block until a slot frees or the request's context is cancelled. A single limiter is meant to be shared by every http.Client that talks to upstreams (metadata, transparent proxy, artifact download) so the cap is per host across all of them, not per client.
func NewConcurrencyLimiter ¶
func NewConcurrencyLimiter(base http.RoundTripper, maxPerHost int) *ConcurrencyLimiter
NewConcurrencyLimiter wraps base, capping concurrent requests to maxPerHost per destination host. A nil base uses http.DefaultTransport; maxPerHost <= 0 disables limiting (every request passes straight through).
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is an http.RoundTripper that caps the request *rate* to each destination host using a token bucket. A concurrency cap alone does not bound rate — a few fast workers can still emit hundreds of requests per second — and registries such as Maven Central throttle by rate, so this is what actually prevents HTTP 429 under sustained load. Requests block until a token is available or the request's context is cancelled.
func NewRateLimiter ¶
func NewRateLimiter(base http.RoundTripper, perSecond float64, burst int) *RateLimiter
NewRateLimiter wraps base, limiting requests to perSecond per destination host with the given burst (clamped to >= 1). A nil base uses http.DefaultTransport; perSecond <= 0 disables limiting (every request passes straight through).