Documentation
¶
Index ¶
- type BackoffOptions
- type ConcurrentRateLimiter
- func (r *ConcurrentRateLimiter) Backoff(ctx context.Context, resource string, opts ...BackoffOptions)
- func (r *ConcurrentRateLimiter) BaseDelay() time.Duration
- func (r *ConcurrentRateLimiter) Jitter() time.Duration
- func (r *ConcurrentRateLimiter) ResetBackoff(host string)
- func (r *ConcurrentRateLimiter) ResolveDelay(ctx context.Context, host string) time.Duration
- func (r *ConcurrentRateLimiter) ResourceTimings() map[string]resourceTiming
- func (r *ConcurrentRateLimiter) SetBaseDelay(baseDelay time.Duration)
- func (r *ConcurrentRateLimiter) SetDebugLogger(logger DebugLogger)
- func (r *ConcurrentRateLimiter) SetJitter(jitter time.Duration)
- func (r *ConcurrentRateLimiter) SetResourceDelay(resource string, delay time.Duration)
- func (r *ConcurrentRateLimiter) Wait(ctx context.Context, resource string) error
- type DebugLogger
- type NoOpLogger
- type RateLimitReason
- type RateLimiter
- type RateLimiterOption
- func WithDebugLogger(debugLogger DebugLogger, attrs ...any) RateLimiterOption
- func WithInitialDuration(d time.Duration) RateLimiterOption
- func WithJitter(d time.Duration) RateLimiterOption
- func WithLogAttrs(attrs ...any) RateLimiterOption
- func WithMaxDuration(d time.Duration) RateLimiterOption
- func WithMultiplier(m float64) RateLimiterOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BackoffOptions ¶
type BackoffOptions struct {
// ServerDelay is an optional server-suggested delay (e.g., from Retry-After header).
ServerDelay time.Duration
}
BackoffOptions configures backoff behavior.
type ConcurrentRateLimiter ¶
type ConcurrentRateLimiter struct {
// contains filtered or unexported fields
}
func NewConcurrentRateLimiter ¶
func NewConcurrentRateLimiter(opts ...RateLimiterOption) *ConcurrentRateLimiter
func (*ConcurrentRateLimiter) Backoff ¶
func (r *ConcurrentRateLimiter) Backoff(ctx context.Context, resource string, opts ...BackoffOptions)
Backoff triggers exponential backoff for the given resource. It increments the backoff counter and computes the delay. The optional opts parameter allows the caller to provide a server-suggested delay (e.g., from Retry-After header or response body). The initial backoff will be max(serverDelay, initialDuration).
func (*ConcurrentRateLimiter) BaseDelay ¶
func (r *ConcurrentRateLimiter) BaseDelay() time.Duration
func (*ConcurrentRateLimiter) Jitter ¶
func (r *ConcurrentRateLimiter) Jitter() time.Duration
func (*ConcurrentRateLimiter) ResetBackoff ¶
func (r *ConcurrentRateLimiter) ResetBackoff(host string)
ResetBackoff resets the backoff counter for the given host. Called after a successful request to clear backoff state.
func (*ConcurrentRateLimiter) ResolveDelay ¶
Compute the final delay resolution for given host FinalDelay = max(BaseDelay, crawlDelay, BackoffDelay) + Jitter
func (*ConcurrentRateLimiter) ResourceTimings ¶
func (r *ConcurrentRateLimiter) ResourceTimings() map[string]resourceTiming
func (*ConcurrentRateLimiter) SetBaseDelay ¶
func (r *ConcurrentRateLimiter) SetBaseDelay(baseDelay time.Duration)
func (*ConcurrentRateLimiter) SetDebugLogger ¶
func (r *ConcurrentRateLimiter) SetDebugLogger(logger DebugLogger)
SetDebugLogger sets the debug logger for the rate limiter. This allows users to provide custom logging implementation.
func (*ConcurrentRateLimiter) SetJitter ¶
func (r *ConcurrentRateLimiter) SetJitter(jitter time.Duration)
func (*ConcurrentRateLimiter) SetResourceDelay ¶
func (r *ConcurrentRateLimiter) SetResourceDelay(resource string, delay time.Duration)
Set delay to given resource, separated from global base delay
func (*ConcurrentRateLimiter) Wait ¶
func (r *ConcurrentRateLimiter) Wait(ctx context.Context, resource string) error
Wait blocks until the resource is ready to be consumed. It resolves the delay, waits if necessary, and marks the last consumed time. Returns nil when ready to proceed, or error if context is cancelled.
type DebugLogger ¶
type DebugLogger interface {
// Enabled returns true if debug logging is enabled.
// When false, the retry handler will skip logging entirely for efficiency.
Enabled() bool
LogBackoff(ctx context.Context, resource string, count int, delay time.Duration, attrs ...any)
LogRateLimit(ctx context.Context, resource string, delay time.Duration, reason RateLimitReason, attrs ...any)
}
DebugLogger provides structured debug logging capabilities for the rate limiter package. Users can implement this interface to provide custom logging behavior. When debug mode is disabled, use NoOpLogger for zero overhead.
type NoOpLogger ¶
type NoOpLogger struct{}
NoOpLogger is a no-operation implementation of DebugLogger. It provides zero overhead when debug mode is disabled. All methods are empty and Enabled() always returns false.
func NewNoOpLogger ¶
func NewNoOpLogger() *NoOpLogger
NewNoOpLogger creates a new NoOpLogger instance.
func (*NoOpLogger) Enabled ¶
func (n *NoOpLogger) Enabled() bool
Enabled returns false - debug logging is disabled.
func (*NoOpLogger) LogBackoff ¶
LogBackoff is no-op.
func (*NoOpLogger) LogRateLimit ¶
func (n *NoOpLogger) LogRateLimit(_ context.Context, _ string, _ time.Duration, _ RateLimitReason, _ ...any)
LogRetry is a no-op.
type RateLimitReason ¶
type RateLimitReason string
RateLimitReason describes why a rate limit was applied.
const ( // RateLimitReasonBaseDelay indicates the base delay was applied. RateLimitReasonBaseDelay RateLimitReason = "base_delay" // RateLimitReasonCrawlDelay indicates crawl-delay from robots.txt was applied. RateLimitReasonConsumeDelay RateLimitReason = "consume_delay" // RateLimitReasonBackoff indicates exponential backoff was applied. RateLimitReasonBackoff RateLimitReason = "backoff" )
type RateLimiter ¶
type RateLimiter interface {
SetBaseDelay(baseDelay time.Duration)
SetJitter(jitter time.Duration)
SetDebugLogger(logger DebugLogger)
SetResourceDelay(resource string, delay time.Duration)
Backoff(ctx context.Context, resource string, opts ...BackoffOptions)
ResetBackoff(resource string)
Wait(ctx context.Context, resource string) error
ResolveDelay(ctx context.Context, resource string) time.Duration
}
RateLimiter Specialized component to manage rate limiting during resource consumption Responsibilities: - Bookkeep each reasource's last consumed timestamp - Compute the final delay for each resource given various factors - Make sure the consumption process respect the resource's policy
type RateLimiterOption ¶
type RateLimiterOption func(*rateLimiterConfig)
RateLimiterOption is a functional option for configuring retry behavior.
func WithDebugLogger ¶
func WithDebugLogger(debugLogger DebugLogger, attrs ...any) RateLimiterOption
func WithInitialDuration ¶
func WithInitialDuration(d time.Duration) RateLimiterOption
WithInitialDuration sets the initial backoff duration. Default is 1 second.
func WithJitter ¶
func WithJitter(d time.Duration) RateLimiterOption
WithJitter sets the maximum random duration added to backoff delays. This helps avoid thundering herd problems. Default is 0 (no jitter).
func WithLogAttrs ¶
func WithLogAttrs(attrs ...any) RateLimiterOption
WithLogAttrs sets additional attributes to be passed to the logger. Attributes follow Go's slog convention for structured logging - alternating key-value pairs (string, any, string, any, ...). These attributes are passed to LogRetry calls for structured logging context.
func WithMaxDuration ¶
func WithMaxDuration(d time.Duration) RateLimiterOption
WithMaxDuration sets the maximum backoff duration. Default is 1 minute.
func WithMultiplier ¶
func WithMultiplier(m float64) RateLimiterOption
WithMultiplier sets the backoff multiplier. Each subsequent delay is multiplied by this value. Default is 2.0.
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
multi_service_fetch
command
Package main contains a fake HTTP server for demonstrating the rate limiter package.
|
Package main contains a fake HTTP server for demonstrating the rate limiter package. |
|
simple_fetch
command
Package main demonstrates a simple rate-limited HTTP client.
|
Package main demonstrates a simple rate-limited HTTP client. |
|
simple_fetch/simple_logger
Package simple_logger provides a simple implementation of the ratelimiter.DebugLogger interface that prints retry information to stdout.
|
Package simple_logger provides a simple implementation of the ratelimiter.DebugLogger interface that prints retry information to stdout. |