Files
tyapi-server/docs/每日限流中间件使用指南.md
2025-08-10 14:40:02 +08:00

58 lines
1.4 KiB
Markdown
Raw Permalink 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.

# 每日限流中间件使用指南
## 概述
每日限流中间件实现了多层限流策略:
- 接口一天最大请求200次
- 一个IP一天最多10次
- 支持并发限制和安全防护
## 主要特性
1. **多层限流策略**
- 接口总请求限制200次/天
- IP请求限制10次/天/IP
- 并发请求限制5个/IP
2. **安全防护**
- IP白名单/黑名单
- User-Agent检查
- Referer验证
- 代理检测
## 使用方法
```go
// 配置限流参数
limitConfig := middleware.DailyRateLimitConfig{
MaxRequestsPerDay: 200, // 接口一天最大请求200次
MaxRequestsPerIP: 10, // 一个IP一天最多10次
KeyPrefix: "api_limit", // Redis键前缀
TTL: 24 * time.Hour, // 24小时过期
MaxConcurrent: 5, // 最大并发5个
}
// 创建中间件实例
rateLimitMiddleware := middleware.NewDailyRateLimitMiddleware(
config, redisClient, response, logger, limitConfig)
// 应用到路由
router.Use(rateLimitMiddleware.Handle())
```
## 限流逻辑
1. 检查IP访问权限
2. 验证User-Agent和Referer
3. 检查并发限制
4. 检查接口总请求次数200次/天)
5. 检查IP请求次数10次/天)
6. 更新计数器
## 监控信息
响应头包含隐藏的监控信息:
- `X-Total-Count`: 当前总请求次数
- `X-IP-Count`: 当前IP请求次数
- `X-Reset-Time`: 重置时间