ratelimit

package
v0.59.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PoolUnaryClientInterceptor

func PoolUnaryClientInterceptor(pool *Pool, container googlecloud.ResourceContainer) grpc.UnaryClientInterceptor

PoolUnaryClientInterceptor returns a grpc.UnaryClientInterceptor that resolves an AdaptiveRateLimiter from the pool based on the ResourceContainer.

func UnaryClientInterceptor

func UnaryClientInterceptor(limiter *AdaptiveRateLimiter) grpc.UnaryClientInterceptor

UnaryClientInterceptor returns a grpc.UnaryClientInterceptor that throttles RPCs using an AdaptiveRateLimiter.

Types

type AdaptiveRateLimiter

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

AdaptiveRateLimiter dynamically controls request rates using an Additive Increase / Multiplicative Decrease (AIMD) algorithm.

func NewAdaptiveRateLimiter

func NewAdaptiveRateLimiter(config AdaptiveRateLimiterConfig) *AdaptiveRateLimiter

NewAdaptiveRateLimiter creates a new AdaptiveRateLimiter instance with the specified configuration.

func (*AdaptiveRateLimiter) Config

Config returns the configuration of the limiter.

func (*AdaptiveRateLimiter) CurrentRate

func (a *AdaptiveRateLimiter) CurrentRate() float64

CurrentRate returns the current permitted QPS.

func (*AdaptiveRateLimiter) OnResourceExhausted

func (a *AdaptiveRateLimiter) OnResourceExhausted()

OnResourceExhausted adjusts the rate downwards using Multiplicative Decrease with cooldown suppression.

func (*AdaptiveRateLimiter) OnSuccess

func (a *AdaptiveRateLimiter) OnSuccess()

OnSuccess adjusts the rate upwards using Additive Increase.

func (*AdaptiveRateLimiter) Wait

Wait blocks until the underlying rate limiter permits an event or the context is cancelled.

type AdaptiveRateLimiterConfig

type AdaptiveRateLimiterConfig struct {
	// InitialQPS is the starting request rate in queries per second.
	InitialQPS float64
	// MinQPS is the minimum lower bound for request rate.
	MinQPS float64
	// MaxQPS is the maximum upper bound for request rate.
	MaxQPS float64
	// IncreaseStep is the additive increment added to QPS on consecutive successes.
	IncreaseStep float64
	// DecreaseFactor is the multiplicative multiplier applied to QPS upon encountering a 429 ResourceExhausted error.
	DecreaseFactor float64
	// DecreaseCooldown is the minimum duration between successive rate reductions to prevent cascade decreases.
	DecreaseCooldown time.Duration
	// Burst is the maximum burst size permitted by the underlying token bucket.
	Burst int
}

AdaptiveRateLimiterConfig defines configuration parameters for AdaptiveRateLimiter.

func DefaultAdaptiveRateLimiterConfig

func DefaultAdaptiveRateLimiterConfig() AdaptiveRateLimiterConfig

DefaultAdaptiveRateLimiterConfig returns standard default configuration values.

type Pool

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

Pool manages and caches AdaptiveRateLimiter instances per resource container or quota project.

func NewPool

func NewPool(config AdaptiveRateLimiterConfig, quotaProjectID string) *Pool

NewPool creates a new Pool instance.

func (*Pool) GetLimiter

func (p *Pool) GetLimiter(container googlecloud.ResourceContainer) *AdaptiveRateLimiter

GetLimiter returns the AdaptiveRateLimiter corresponding to the given ResourceContainer or quota project.

func (*Pool) GetLimiterByKey

func (p *Pool) GetLimiterByKey(key string) *AdaptiveRateLimiter

GetLimiterByKey returns the AdaptiveRateLimiter corresponding to the specified key.

Jump to

Keyboard shortcuts

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