NewTokenBucketLimiter 创建令牌桶限流器
(rate, capacity int, window time.Duration)
| 41 | |
| 42 | // NewTokenBucketLimiter 创建令牌桶限流器 |
| 43 | func NewTokenBucketLimiter(rate, capacity int, window time.Duration) *TokenBucketLimiter { |
| 44 | limiter := &TokenBucketLimiter{ |
| 45 | buckets: make(map[string]*bucket), |
| 46 | rate: rate, |
| 47 | capacity: capacity, |
| 48 | window: window, |
| 49 | } |
| 50 | |
| 51 | // 启动清理 goroutine |
| 52 | go limiter.cleanup() |
| 53 | |
| 54 | return limiter |
| 55 | } |
| 56 | |
| 57 | // Allow 检查是否允许请求 |
| 58 | func (l *TokenBucketLimiter) Allow(key string) bool { |
no test coverage detected