ratelimit

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackoffRegistry

type BackoffRegistry struct {
	// contains filtered or unexported fields
}

BackoffRegistry tracks backoff state per domain using decorrelated jitter.

func NewBackoffRegistry

func NewBackoffRegistry(initialMs, maxMs int, multiplier float64, maxAttempts int) *BackoffRegistry

NewBackoffRegistry creates a BackoffRegistry from config values.

func (*BackoffRegistry) Attempts

func (r *BackoffRegistry) Attempts(domain string) int

Attempts returns the current backoff attempt count for a domain.

func (*BackoffRegistry) MaxAttempts

func (r *BackoffRegistry) MaxAttempts() int

MaxAttempts returns the configured maximum retry attempts.

func (*BackoffRegistry) RecordError

func (r *BackoffRegistry) RecordError(domain string) time.Duration

RecordError notes a transient error for the given domain and updates backoff. Returns the delay that will be applied before the next attempt.

func (*BackoffRegistry) RecordSuccess

func (r *BackoffRegistry) RecordSuccess(domain string)

RecordSuccess resets the backoff state for the domain.

func (*BackoffRegistry) Wait

func (r *BackoffRegistry) Wait(ctx context.Context, domain string) error

Wait blocks until the backoff delay for the domain has elapsed, or ctx is done. If the domain has reached max_attempts and its delay has expired, the entry is evicted so the map does not grow without bound.

type ErrorClass

type ErrorClass int

ErrorClass categorises an HTTP error for backoff decisions.

const (
	ErrorClassNone      ErrorClass = iota // success
	ErrorClassTransient                   // 429, 502, 503, 504 — back off and retry
	ErrorClassPermanent                   // 400, 403, 404 — log and skip
	ErrorClassFatal                       // context cancelled — drop silently
)

func ClassifyError

func ClassifyError(err error) ErrorClass

ClassifyError checks a Go error for context cancellation.

func ClassifyStatusCode

func ClassifyStatusCode(code int) ErrorClass

ClassifyStatusCode returns the ErrorClass for the given status code. It is used for both HTTP status codes and DNS RCODEs (mapped to HTTP-like codes by the DNS driver before being passed here).

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry maintains per-domain token bucket rate limiters.

func NewRegistry

func NewRegistry(defaultRPS float64, perDomain map[string]float64) *Registry

NewRegistry creates a Registry with the given defaults and per-domain overrides.

func (*Registry) Wait

func (r *Registry) Wait(ctx context.Context, domain string) error

Wait blocks until the rate limiter for the given domain allows the request, or until ctx is cancelled.

Jump to

Keyboard shortcuts

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