feat: 增加接口流量配置中间件

This commit is contained in:
2026-01-04 15:28:11 +08:00
parent 3788d2b4b6
commit ded502746c
13 changed files with 215 additions and 79 deletions

View File

@@ -11,6 +11,7 @@ import (
"reflect"
"strings"
"git.zhangdeman.cn/zhangdeman/gin/define"
"git.zhangdeman.cn/zhangdeman/gin/logger"
"git.zhangdeman.cn/zhangdeman/util"
)
@@ -26,8 +27,8 @@ type controllerParser struct {
// 1. 函数接受两个入参, 第一个参数为 gin.Context , 第二个参数为 任意结构体指针, 但是必须声明 Meta 相关信息, 否则会报错
//
// 2. 函数有两个返回值, 第一个返回值为任意结构体/结构体指针(限制死不能为map/slice, 方便后续统一标准化) , 第二个返回值为 error , 代表处理的异常, 会自动适配 exception.IException 类型
func (c controllerParser) Parse(inputController any) map[string]UriConfig {
parseRes := make(map[string]UriConfig)
func (c controllerParser) Parse(inputController any) map[string]define.UriConfig {
parseRes := make(map[string]define.UriConfig)
if nil == inputController {
return parseRes
}
@@ -47,7 +48,7 @@ func (c controllerParser) Parse(inputController any) map[string]UriConfig {
}
// preCheckMethod 预检查方法是否可以注册为接口
func (c controllerParser) preCheckMethod(reflectMethod reflect.Method, config *UriConfig) (bool, reflect.Type, reflect.StructField) {
func (c controllerParser) preCheckMethod(reflectMethod reflect.Method, config *define.UriConfig) (bool, reflect.Type, reflect.StructField) {
var (
metaField reflect.StructField
metaFieldExist bool
@@ -84,11 +85,11 @@ func (c controllerParser) preCheckMethod(reflectMethod reflect.Method, config *U
// 参数 : 方法反射结果
//
// 返回值 : 第一个 -> 解析出的接口配置 第二个 -> 是否要注册为接口
func (c controllerParser) methodConfig(reflectMethod reflect.Method) (UriConfig, bool) {
func (c controllerParser) methodConfig(reflectMethod reflect.Method) (define.UriConfig, bool) {
var (
needRegister bool
metaField reflect.StructField
cfg UriConfig
cfg define.UriConfig
)
methodType := reflectMethod.Type
@@ -97,7 +98,7 @@ func (c controllerParser) methodConfig(reflectMethod reflect.Method) (UriConfig,
// num2: 第二个参数
if needRegister, cfg.FormDataType, metaField = c.preCheckMethod(reflectMethod, &cfg); !needRegister {
logger.Instance.Info("接口方法不符合要求, 不注册为接口, 方法名: " + reflectMethod.Name + ", 方法签名: " + methodType.String())
return UriConfig{}, false
return cfg, false
}
cfg.ResultDataType = methodType.Out(0)
@@ -135,7 +136,7 @@ func (c controllerParser) methodConfig(reflectMethod reflect.Method) (UriConfig,
}
// setUriMeta 设置接口的 meta 信息
func (c controllerParser) setUriMeta(metaField reflect.StructField, cfg *UriConfig) {
func (c controllerParser) setUriMeta(metaField reflect.StructField, cfg *define.UriConfig) {
// 解析 meta 信息
cfg.Path = metaField.Tag.Get(TagNamePath) // 接口路由
cfg.RequestMethod = strings.Split(strings.ToUpper(metaField.Tag.Get(TagNameMethod)), ",") // 请求方法
@@ -159,4 +160,25 @@ func (c controllerParser) setUriMeta(metaField reflect.StructField, cfg *UriConf
panic(cfg.Path + " : 最大执行时间配置错误(配置的值必须是无符号整型), 请检查配置 : " + err.Error())
}
}
// 流控配置, 格式: {单次消耗次数}/{总次数}/{时间间隔}
rateLimitStr := strings.TrimSpace(metaField.Tag.Get(TagNameRateLimit))
if rateLimitStr != "" {
cfgArr := strings.Split(rateLimitStr, "/")
if len(cfgArr) != 3 {
panic(cfg.Path + " : 流控配置错误(配置的值必须为3个用 / 分隔的数字, {单次消耗次数}/{总次数}/{时间间隔}), 请检查配置 : " + rateLimitStr)
}
if err := util.ConvertAssign(&cfg.RateLimitConfig.Use, cfgArr[0]); nil != err {
panic(cfg.Path + " : 流控单次消耗次数不是一个合法数字 : " + err.Error())
}
if err := util.ConvertAssign(&cfg.RateLimitConfig.Total, cfgArr[1]); nil != err {
panic(cfg.Path + " : 流控总次数不是一个合法数字 : " + err.Error())
}
if cfg.RateLimitConfig.Total < cfg.RateLimitConfig.Use {
panic(cfg.Path + " : 流控总次数不能小于单次消耗次数 : " + rateLimitStr)
}
if err := util.ConvertAssign(&cfg.RateLimitConfig.TimeInterval, cfgArr[2]); nil != err {
panic(cfg.Path + " : 流控时间间隔不是一个合法数字 : " + err.Error())
}
}
}