Documentation
¶
Index ¶
- Variables
- type AdaptiveLimiter
- type AdaptiveLimiterOptions
- type FixWindowLimiter
- type FixWindowLimiterOptions
- type LeakyBucketLimiter
- type LeakyBucketLimiterOptions
- type LimiterStats
- type RedisFixWindowLimiter
- type RedisFixWindowLimiterOptions
- type RedisSlideWindowLimiter
- type RedisSlideWindowLimiterOptions
- type SlideWindowLimiter
- type SlideWindowLimiterOptions
- type TokenBucket
- type TokenBucketLimiter
- type TokenBucketLimiterOptions
- type TokenBucketOptions
Constants ¶
This section is empty.
Variables ¶
var (
ErrLimitExceeded = errors.New("rate limit exceeded")
)
定义错误类型
Functions ¶
This section is empty.
Types ¶
type AdaptiveLimiter ¶ added in v0.1.1
type AdaptiveLimiter struct {
// contains filtered or unexported fields
}
AdaptiveLimiter 自适应限流器,根据系统负载动态调整请求速率
func NewAdaptiveLimiter ¶ added in v0.1.1
func NewAdaptiveLimiter(opts AdaptiveLimiterOptions) *AdaptiveLimiter
NewAdaptiveLimiter 创建新的自适应限流器
func (*AdaptiveLimiter) Allow ¶ added in v0.1.1
func (l *AdaptiveLimiter) Allow() bool
Allow 检查请求是否可以通过
func (*AdaptiveLimiter) AllowWithContext ¶ added in v0.1.1
func (l *AdaptiveLimiter) AllowWithContext(ctx context.Context) error
AllowWithContext 带上下文的限流检查
func (*AdaptiveLimiter) Close ¶ added in v0.1.1
func (l *AdaptiveLimiter) Close() error
Close 关闭限流器并释放资源
func (*AdaptiveLimiter) GetStats ¶ added in v0.1.1
func (l *AdaptiveLimiter) GetStats() LimiterStats
GetStats 获取限流器统计信息
func (*AdaptiveLimiter) RecordMetrics ¶ added in v0.1.1
func (l *AdaptiveLimiter) RecordMetrics(ctx context.Context)
RecordMetrics 记录请求执行的指标
type AdaptiveLimiterOptions ¶ added in v0.1.1
type AdaptiveLimiterOptions struct {
// 初始QPS限制
InitialRate float64
// 最小QPS限制
MinRate float64
// 最大QPS限制
MaxRate float64
// CPU使用率阈值 (0-1),超过此值将降低限制
HighLoadThreshold float64
// CPU使用率阈值 (0-1),低于此值将提高限制
LowLoadThreshold float64
// 降低系数,当负载过高时QPS限制的降低系数
DecreaseFactorOnHeavyLoad float64
// 增加系数,当负载过低时QPS限制的增加系数
IncreaseFactorOnLightLoad float64
// 响应时间阈值,超过此值将视为系统负载较高
ResponseTimeThreshold time.Duration
// 响应时间采样窗口大小
ResponseTimeSampleSize int
// 采样间隔,多久检查一次系统负载
SamplingInterval time.Duration
// 是否在过载时直接拒绝请求而不是延迟
RejectOnOverload bool
// 采样窗口大小
WindowSize int
}
AdaptiveLimiterOptions 自适应限流器的配置选项
func DefaultAdaptiveLimiterOptions ¶ added in v0.1.1
func DefaultAdaptiveLimiterOptions() AdaptiveLimiterOptions
DefaultAdaptiveLimiterOptions 返回默认的自适应限流器配置
type FixWindowLimiter ¶
type FixWindowLimiter struct {
// contains filtered or unexported fields
}
FixWindowLimiter implements rate limiting using a fixed time window algorithm.
func NewFixWindowLimiter ¶
func NewFixWindowLimiter(interval time.Duration, rate int64) *FixWindowLimiter
NewFixWindowLimiter creates a new FixWindowLimiter with the given interval and rate.
func (*FixWindowLimiter) LimitUnary ¶
func (l *FixWindowLimiter) LimitUnary() grpc.UnaryServerInterceptor
LimitUnary returns a grpc.UnaryServerInterceptor that imposes rate limiting based on the fixed window algorithm.
type FixWindowLimiterOptions ¶
type FixWindowLimiterOptions func(l *FixWindowLimiter)
FixWindowLimiterOptions defines a function type for setting options on FixWindowLimiter.
func FixWindowMarkFailed ¶
func FixWindowMarkFailed() FixWindowLimiterOptions
FixWindowMarkFailed provides an option to set the markFailedStrategy as the rejection strategy.
type LeakyBucketLimiter ¶
type LeakyBucketLimiter struct {
// contains filtered or unexported fields
}
LeakyBucketLimiter implements rate limiting using the leaky bucket algorithm.
func NewLeakyBucketLimiter ¶
func NewLeakyBucketLimiter(interval time.Duration) *LeakyBucketLimiter
NewLeakyBucketLimiter initializes and returns a new LeakyBucketLimiter.
func (*LeakyBucketLimiter) Close ¶
func (l *LeakyBucketLimiter) Close() error
Close stops the ticker and releases associated resources.
func (*LeakyBucketLimiter) LimitUnary ¶
func (l *LeakyBucketLimiter) LimitUnary() grpc.UnaryServerInterceptor
LimitUnary returns a grpc.UnaryServerInterceptor that imposes rate limiting based on the leaky bucket algorithm.
type LeakyBucketLimiterOptions ¶
type LeakyBucketLimiterOptions func(l *LeakyBucketLimiter)
LeakyBucketLimiterOptions defines a function type for setting options on LeakyBucketLimiter.
func LeakyBucketMarkFailed ¶
func LeakyBucketMarkFailed() LeakyBucketLimiterOptions
LeakyBucketMarkFailed provides an option to set the markFailedStrategy as the rejection strategy.
type LimiterStats ¶ added in v0.1.1
LimiterStats 限流器统计信息
type RedisFixWindowLimiter ¶
type RedisFixWindowLimiter struct {
// contains filtered or unexported fields
}
RedisFixWindowLimiter implements rate limiting using a fixed window approach with Redis.
func NewRedisFixWindowLimiter ¶
func NewRedisFixWindowLimiter(client redis.Cmdable, service string, interval time.Duration, rate int) *RedisFixWindowLimiter
NewRedisFixWindowLimiter returns a new instance of RedisFixWindowLimiter with the provided configuration.
func (*RedisFixWindowLimiter) LimitUnary ¶
func (l *RedisFixWindowLimiter) LimitUnary() grpc.UnaryServerInterceptor
LimitUnary returns a grpc.UnaryServerInterceptor that imposes rate limiting based on the fixed window mechanism.
type RedisFixWindowLimiterOptions ¶
type RedisFixWindowLimiterOptions func(l *RedisFixWindowLimiter)
RedisFixWindowLimiterOptions defines the function signature for configuration options for RedisFixWindowLimiter.
func RedisFixWindowMarkFailed ¶
func RedisFixWindowMarkFailed() RedisFixWindowLimiterOptions
RedisFixWindowMarkFailed provides an option to set the markFailedStrategy as the reject strategy.
type RedisSlideWindowLimiter ¶
type RedisSlideWindowLimiter struct {
// contains filtered or unexported fields
}
RedisSlideWindowLimiter implements rate limiting using a sliding window approach with redis.
func NewRedisSlideWindowLimiter ¶
func NewRedisSlideWindowLimiter(client redis.Cmdable, service string, interval time.Duration, rate int) *RedisSlideWindowLimiter
NewRedisSlideWindowLimiter returns a new instance of RedisSlideWindowLimiter with the provided configuration.
func (*RedisSlideWindowLimiter) LimitUnary ¶
func (l *RedisSlideWindowLimiter) LimitUnary() grpc.UnaryServerInterceptor
LimitUnary returns a grpc.UnaryServerInterceptor that imposes rate limiting based on the sliding window mechanism.
type RedisSlideWindowLimiterOptions ¶
type RedisSlideWindowLimiterOptions func(l *RedisSlideWindowLimiter)
RedisSlideWindowLimiterOptions defines the function signature for configuration options of RedisSlideWindowLimiter.
func RedisSlideWindowMarkFailed ¶
func RedisSlideWindowMarkFailed() RedisSlideWindowLimiterOptions
RedisSlideWindowMarkFailed provides an option to set the markFailedStrategy as the reject strategy.
type SlideWindowLimiter ¶
type SlideWindowLimiter struct {
// contains filtered or unexported fields
}
SlideWindowLimiter implements rate limiting using a sliding window approach.
func NewSlideWindowLimiter ¶
func NewSlideWindowLimiter(interval time.Duration, rate int) *SlideWindowLimiter
NewSlideWindowLimiter returns a new instance of SlideWindowLimiter with the specified interval and rate.
func (*SlideWindowLimiter) LimitUnary ¶
func (l *SlideWindowLimiter) LimitUnary() grpc.UnaryServerInterceptor
LimitUnary returns a grpc.UnaryServerInterceptor that imposes rate limiting based on the sliding window mechanism.
type SlideWindowLimiterOptions ¶
type SlideWindowLimiterOptions func(l *SlideWindowLimiter)
SlideWindowLimiterOptions defines the function signature for configuration options of SlideWindowLimiter.
func SlideWindowMarkFailed ¶
func SlideWindowMarkFailed() SlideWindowLimiterOptions
SlideWindowMarkFailed provides an option to set the markFailedStrategy as the reject strategy.
type TokenBucket ¶ added in v0.1.1
type TokenBucket struct {
// contains filtered or unexported fields
}
TokenBucket 令牌桶实现
func NewTokenBucket ¶ added in v0.1.1
func NewTokenBucket(opts TokenBucketOptions) *TokenBucket
NewTokenBucket 创建一个新的令牌桶
func (*TokenBucket) UpdateRate ¶ added in v0.1.1
func (tb *TokenBucket) UpdateRate(newRate float64)
UpdateRate 更新令牌桶的速率
type TokenBucketLimiter ¶
type TokenBucketLimiter struct {
// contains filtered or unexported fields
}
TokenBucketLimiter controls access to a resource by enforcing a rate limit using a token bucket strategy.
func NewTokenBucketLimiter ¶
func NewTokenBucketLimiter(capacity int, interval time.Duration) *TokenBucketLimiter
NewTokenBucketLimiter constructs a new TokenBucketLimiter with the given capacity and token refill interval.
func (*TokenBucketLimiter) Close ¶
func (l *TokenBucketLimiter) Close() error
Close cleanly stops the token refill and shuts down the limiter.
func (*TokenBucketLimiter) LimitUnary ¶
func (l *TokenBucketLimiter) LimitUnary() grpc.UnaryServerInterceptor
LimitUnary returns a grpc.UnaryServerInterceptor that enforces the token bucket rate limiting.
type TokenBucketLimiterOptions ¶
type TokenBucketLimiterOptions func(l *TokenBucketLimiter)
TokenBucketLimiterOptions defines the type for configuration options on the TokenBucketLimiter.
func TokenBucketMarkFailed ¶
func TokenBucketMarkFailed() TokenBucketLimiterOptions
TokenBucketMarkFailed provides a configuration option to use the markFailedStrategy when a request is rejected.
type TokenBucketOptions ¶ added in v0.1.1
TokenBucketOptions 令牌桶配置选项