Files
CamTalk/backend/internal/ratelimit/limiter.go
hhs 3b6226394b feat: 实现内存令牌桶限流器
- Limiter 接口定义(Allow + Stop 方法)
- TokenBucket 实现(容量、填充速率、并发安全)
- MemoryLimiter 管理多用户令牌桶
- 后台 goroutine 定期清理不活跃桶(10 分钟)
- 完整单元测试覆盖(11 个测试用例,全部通过)
- 边界情况处理(rate=0、capacity=0、并发安全)
2026-06-20 23:53:36 +08:00

18 lines
428 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package ratelimit
import (
"context"
"time"
)
// Limiter 速率限制器接口。
type Limiter interface {
// Allow 判断 key 是否允许执行一次操作。
// key 通常为 "userID:action" 格式。
// 返回 (allowed, retryAfter)。retryAfter 表示需要等待的时间。
Allow(ctx context.Context, key string) (bool, time.Duration)
// Stop 停止限流器,清理资源(如后台 goroutine
Stop()
}