-
Notifications
You must be signed in to change notification settings - Fork 0
xlimit
omeyang edited this page Sep 17, 2026
·
1 revision
稳定性:Stable · 覆盖率:95.3% · 源码:
pkg/resilience/xlimit
分布式限流器。Token Bucket 算法 + Redis 后端。
- API 限流(每秒 N 个请求)
- 多副本协同(避免每个 pod 独立计数)
- 单副本:用
golang.org/x/time/rate更轻量
import "github.com/omeyang/xkit/pkg/resilience/xlimit"
l := xlimit.NewTokenBucket(redisClient, "api:foo",
xlimit.WithRate(100), // 100 tokens/sec
xlimit.WithCapacity(200),
)
if !l.Allow(ctx) {
return errors.New("rate limit exceeded")
}| 名称 | 说明 |
|---|---|
Limiter interface |
Allow(ctx) bool / Wait(ctx) error
|
NewTokenBucket(client, key, opts...) |
Redis 后端 |
WithRate / WithCapacity |
配置 |
- Lua 原子:避免 race
- 告警与文档:对抗审查加强了 Burst/Rate 边界的告警与文档