Documentation
¶
Overview ¶
Package ratelimit 提供通用限流机制。
本包定义限流策略、决策结果、Limiter 端口、本地 token bucket 限流器和 基于共享 Backend 的分布式限流器。它不绑定 HTTP、gRPC、Redis keyspace 或业务观测模型;项目层可以把 Decision 映射为自己的响应头、指标或治理事件。
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend interface {
Allow(ctx context.Context, key string, ratePerSecond float64, burst int) (bool, time.Duration, error)
}
Backend 是 DistributedLimiter 使用的共享 token bucket 存储端口。
type Decision ¶
type Decision struct {
Allowed bool
RetryAfter time.Duration
RetryAfterSeconds int
Policy Policy
Outcome Outcome
}
Decision 表示一次与传输协议无关的限流检查结果。
type DistributedLimiter ¶
type DistributedLimiter struct {
// contains filtered or unexported fields
}
DistributedLimiter 将共享 token bucket backend 适配为限流决策器。
func NewDistributedLimiter ¶
func NewDistributedLimiter(backend Backend, policy Policy) *DistributedLimiter
type LocalLimiter ¶
type LocalLimiter struct {
// contains filtered or unexported fields
}
LocalLimiter 是进程内 token bucket 限流器。
func NewKeyedLocalLimiter ¶
func NewKeyedLocalLimiter(policy Policy) *LocalLimiter
func NewLocalLimiter ¶
func NewLocalLimiter(policy Policy) *LocalLimiter
Directories
¶
| Path | Synopsis |
|---|---|
|
Package redisadapter 提供 ratelimit.Backend 的 Redis 实现。
|
Package redisadapter 提供 ratelimit.Backend 的 Redis 实现。 |
Click to show internal directories.
Click to hide internal directories.