refactor: 删除冗余常量文件并整合常量定义
This commit is contained in:
@@ -5,31 +5,30 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/constants/cachekey"
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/redis"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/utils/ip2region"
|
||||
"be.ems/src/framework/vo/result"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/database/redis"
|
||||
"be.ems/src/framework/ip2region"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
)
|
||||
|
||||
const (
|
||||
// 默认策略全局限流
|
||||
// LIMIT_GLOBAL 默认策略全局限流
|
||||
LIMIT_GLOBAL = 1
|
||||
|
||||
// 根据请求者IP进行限流
|
||||
// LIMIT_IP 根据请求者IP进行限流
|
||||
LIMIT_IP = 2
|
||||
|
||||
// 根据用户ID进行限流
|
||||
// LIMIT_USER 根据用户ID进行限流
|
||||
LIMIT_USER = 3
|
||||
)
|
||||
|
||||
// LimitOption 请求限流参数
|
||||
type LimitOption struct {
|
||||
Time int64 `json:"time"` // 限流时间,单位秒
|
||||
Count int64 `json:"count"` // 限流次数
|
||||
Time int64 `json:"time"` // 限流时间,单位秒 5
|
||||
Count int64 `json:"count"` // 限流次数,单位次 10
|
||||
Type int64 `json:"type"` // 限流条件类型,默认LIMIT_GLOBAL
|
||||
}
|
||||
|
||||
@@ -43,8 +42,6 @@ type LimitOption struct {
|
||||
// 以便获取登录用户信息,无用户信息时默认为 GLOBAL
|
||||
func RateLimit(option LimitOption) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
|
||||
// 初始可选参数数据
|
||||
if option.Time < 5 {
|
||||
option.Time = 5
|
||||
@@ -61,40 +58,46 @@ func RateLimit(option LimitOption) gin.HandlerFunc {
|
||||
lastDotIndex := strings.LastIndex(funcName, "/")
|
||||
funcName = funcName[lastDotIndex+1:]
|
||||
// 生成限流key
|
||||
var limitKey string = cachekey.RATE_LIMIT_KEY + funcName
|
||||
limitKey := constants.CACHE_RATE_LIMIT + ":" + funcName
|
||||
|
||||
// 用户
|
||||
if option.Type == LIMIT_USER {
|
||||
loginUser, err := ctx.LoginUser(c)
|
||||
loginUser, err := reqctx.LoginUser(c)
|
||||
if err != nil {
|
||||
c.JSON(401, result.Err(map[string]any{
|
||||
"code": 401,
|
||||
"msg": i18n.TKey(language, err.Error()),
|
||||
}))
|
||||
c.JSON(401, resp.CodeMsg(40003, err.Error()))
|
||||
c.Abort() // 停止执行后续的处理函数
|
||||
return
|
||||
}
|
||||
limitKey = cachekey.RATE_LIMIT_KEY + loginUser.UserID + ":" + funcName
|
||||
limitKey = fmt.Sprintf("%s:%d:%s", constants.CACHE_RATE_LIMIT, loginUser.UserId, funcName)
|
||||
}
|
||||
|
||||
// IP
|
||||
if option.Type == LIMIT_IP {
|
||||
clientIP := ip2region.ClientIP(c.ClientIP())
|
||||
limitKey = cachekey.RATE_LIMIT_KEY + clientIP + ":" + funcName
|
||||
limitKey = constants.CACHE_RATE_LIMIT + ":" + clientIP + ":" + funcName
|
||||
}
|
||||
|
||||
// 在Redis查询并记录请求次数
|
||||
rateCount, _ := redis.RateLimit("", limitKey, option.Time, option.Count)
|
||||
rateTime, _ := redis.GetExpire("", limitKey)
|
||||
rateCount, err := redis.RateLimit("", limitKey, option.Time, option.Count)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.CodeMsg(4013, "访问过于频繁,请稍候再试"))
|
||||
c.Abort() // 停止执行后续的处理函数
|
||||
return
|
||||
}
|
||||
rateTime, err := redis.GetExpire("", limitKey)
|
||||
if err != nil {
|
||||
c.JSON(200, resp.CodeMsg(4013, "访问过于频繁,请稍候再试"))
|
||||
c.Abort() // 停止执行后续的处理函数
|
||||
return
|
||||
}
|
||||
|
||||
// 设置响应头中的限流声明字段
|
||||
c.Header("X-RateLimit-Limit", fmt.Sprintf("%d", option.Count)) // 总请求数限制
|
||||
c.Header("X-RateLimit-Remaining", fmt.Sprintf("%d", option.Count-rateCount)) // 剩余可用请求数
|
||||
c.Header("X-RateLimit-Reset", fmt.Sprintf("%d", time.Now().Unix()+int64(rateTime))) // 重置时间戳
|
||||
c.Header("X-RateLimit-Limit", fmt.Sprintf("%d", option.Count)) // 总请求数限制
|
||||
c.Header("X-RateLimit-Remaining", fmt.Sprintf("%d", option.Count-rateCount)) // 剩余可用请求数
|
||||
c.Header("X-RateLimit-Reset", fmt.Sprintf("%d", time.Now().Unix()+rateTime)) // 重置时间戳
|
||||
|
||||
if rateCount >= option.Count {
|
||||
// 访问过于频繁,请稍候再试
|
||||
c.JSON(200, i18n.TKey(language, "app.common.rateLimitTip"))
|
||||
c.JSON(200, resp.CodeMsg(4013, "访问过于频繁,请稍候再试"))
|
||||
c.Abort() // 停止执行后续的处理函数
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user