feat: 增加接口流量配置中间件
This commit is contained in:
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,6 @@
|
||||
// Date : 2024-07-20 22:57
|
||||
package router
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
const (
|
||||
PrefixFuncName = "RouterPrefix" // 路由前缀函数名称
|
||||
MiddlewareFuncName = "RouterMiddleware" // 路由中间件函数名称
|
||||
@@ -30,30 +26,12 @@ const (
|
||||
TagNameHookSync = "hook-sync" // hook 同步执行
|
||||
TagNameNoLogin = "no-login" // 接口是否需要登录(无需登录, 则有token就验证, 无token不验证)
|
||||
TagNameMaxExecTime = "max-exec-time" // 接口最大执行时间, 单位: s, 配置为0则不验证
|
||||
TagNameRateLimit = "rate-limit" // 接口限流
|
||||
TagNameBinding = "binding" // gin 内置的验证规则tag
|
||||
TagNameValidate = "validate" // validator v10 默认的验证规则tag
|
||||
TagNameErrMsg = "err" // 验证失败错误信息 tag
|
||||
)
|
||||
|
||||
// UriConfig 接口配置
|
||||
type UriConfig struct {
|
||||
Path string `json:"path"` // 接口路由, 必须配置
|
||||
RequestMethod []string `json:"request_method"` // 接口请求方法, 必须配置
|
||||
TagList []string `json:"tag_list"` // 接口分组
|
||||
Desc string `json:"desc"` // 接口描述
|
||||
IsSse bool `json:"is_sse"` // 是否 SSE 连接
|
||||
IsWebsocket bool `json:"is_ws"` // 是否 websocket 连接
|
||||
OutputStrict bool `json:"output_strict"` // 接口是否为严格模式 : 不配置,可返回任意类型, 配置, 必须返回结构体或者map
|
||||
MaxExecTime int64 `json:"max_exec_time"` // 接口最大执行时间, 单位: ms, 配置为0则不验证, 注意, 超时后不会报错, 会打印一条warn日志, 如果配置了报警策略, 也会发送报警信息
|
||||
HookSync bool `json:"hook_sync"` // 接口主逻辑执行完成之后,hook是否同步执行, 默认异步执行
|
||||
NoLogin bool `json:"no_login"` // 接口是否需要登录(无需登录, 则有token就验证, 无token不验证)
|
||||
ParamIsPtr bool `json:"param_is_ptr"` // 参数是否指针类型
|
||||
FormDataType reflect.Type `json:"-"` // 表单数据类型
|
||||
ResultDataType reflect.Type `json:"-"` // 返回值数据类型
|
||||
ApiStructValue reflect.Value `json:"-"` // 逻辑函数所属结构体取值
|
||||
ApiLogicFunc reflect.Method `json:"-"` // 自定义的接口逻辑
|
||||
}
|
||||
|
||||
// UriParam 接口参数配置
|
||||
type UriParam struct {
|
||||
Field string `json:"field"` // 结构体字段
|
||||
|
||||
@@ -29,7 +29,7 @@ import (
|
||||
"github.com/mcuadros/go-defaults"
|
||||
)
|
||||
|
||||
func (s *server) getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, error) {
|
||||
func (s *server) getFormInitValue(ctx *gin.Context, uriCfg define.UriConfig) (any, error) {
|
||||
var (
|
||||
formParam reflect.Value
|
||||
formValue any
|
||||
@@ -58,7 +58,7 @@ func (s *server) getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, erro
|
||||
return formValue, nil
|
||||
}
|
||||
|
||||
func (s *server) initRequest(ctx *gin.Context, uriCfg UriConfig) (any, reflect.Value, exception.IException) {
|
||||
func (s *server) initRequest(ctx *gin.Context, uriCfg define.UriConfig) (any, reflect.Value, exception.IException) {
|
||||
var (
|
||||
err error
|
||||
formValue any
|
||||
@@ -92,7 +92,7 @@ func (s *server) initRequest(ctx *gin.Context, uriCfg UriConfig) (any, reflect.V
|
||||
}
|
||||
|
||||
// callFunc 调用方法
|
||||
func (s *server) callFunc(ctx *gin.Context, uriCfg UriConfig, inputValue reflect.Value) (any, exception.IException) {
|
||||
func (s *server) callFunc(ctx *gin.Context, uriCfg define.UriConfig, inputValue reflect.Value) (any, exception.IException) {
|
||||
|
||||
var (
|
||||
firstParam = reflect.ValueOf(ctx)
|
||||
@@ -135,7 +135,7 @@ func (s *server) formatError(ctx *gin.Context, err any) exception.IException {
|
||||
}
|
||||
|
||||
// RequestHandler 获取请求处理方法
|
||||
func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
|
||||
func (s *server) RequestHandler(uriCfg define.UriConfig) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
var (
|
||||
e exception.IException
|
||||
@@ -164,7 +164,7 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
|
||||
}
|
||||
|
||||
// SseHandler sse连接请求
|
||||
func (s *server) SseHandler(uriCfg UriConfig) gin.HandlerFunc {
|
||||
func (s *server) SseHandler(uriCfg define.UriConfig) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
// hook 执行hook逻辑
|
||||
func (s *server) hook(ctx *gin.Context, uriCfg UriConfig) {
|
||||
func (s *server) hook(ctx *gin.Context, uriCfg define.UriConfig) {
|
||||
var (
|
||||
exists bool
|
||||
isSuccess any
|
||||
@@ -47,7 +47,7 @@ func (s *server) hook(ctx *gin.Context, uriCfg UriConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *server) hookAfter(ctx *gin.Context, uriCfg UriConfig, hookInstance *define.LogicAfterResponse, success bool) {
|
||||
func (s *server) hookAfter(ctx *gin.Context, uriCfg define.UriConfig, hookInstance *define.LogicAfterResponse, success bool) {
|
||||
innerContext := util.GinCtxToContext(ctx)
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
apiDocDefine "git.zhangdeman.cn/zhangdeman/api-doc/define"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/middleware"
|
||||
"git.zhangdeman.cn/zhangdeman/rate_limit/abstract"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -30,6 +31,14 @@ type serverOption struct {
|
||||
disableInitRequest bool // 禁用初始化请求
|
||||
loggerCfg *middleware.AccessConfig // 日志配置
|
||||
initContextData gin.HandlerFunc // 初始化一些请求数据
|
||||
rateLimitInstance abstract.IRateLimit // 服务流控实例
|
||||
}
|
||||
|
||||
// WithRateLimitInstance 设置流控实例, 配置为 nil, 代表禁用
|
||||
func WithRateLimitInstance(i abstract.IRateLimit) SetServerOptionFunc {
|
||||
return func(so *serverOption) {
|
||||
so.rateLimitInstance = i
|
||||
}
|
||||
}
|
||||
|
||||
// WithDisableInitRequest 禁用自从初始化请求
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/gin/define"
|
||||
"git.zhangdeman.cn/zhangdeman/graceful"
|
||||
"github.com/jedib0t/go-pretty/v6/table"
|
||||
"github.com/jedib0t/go-pretty/v6/text"
|
||||
@@ -90,11 +91,13 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
lock: &sync.RWMutex{},
|
||||
commonParam: map[string]GetCommonParam{},
|
||||
consoleOutput: [][]any{},
|
||||
globalMiddlewareList: []gin.HandlerFunc{},
|
||||
globalMiddlewareDescList: []string{},
|
||||
uriTable: map[string]define.UriConfig{},
|
||||
}
|
||||
globalMiddlewareList := s.getGlobalMiddlewareList(option)
|
||||
s.getGlobalMiddlewareList(option)
|
||||
// 注册全局中间件
|
||||
s.router.Use(globalMiddlewareList...)
|
||||
s.router.Use(s.globalMiddlewareList...)
|
||||
// 启用pprof, 注册相关路由
|
||||
if option.enablePprof {
|
||||
pprof.Register(s.router)
|
||||
@@ -109,27 +112,30 @@ type server struct {
|
||||
option *serverOption
|
||||
commonParam map[string]GetCommonParam // 结构体字段名, 注意, 不是TAG
|
||||
lock *sync.RWMutex
|
||||
consoleOutput [][]any // 控制台输出的提示信息
|
||||
globalMiddlewareDescList []string // 全局中间件描述列表
|
||||
consoleOutput [][]any // 控制台输出的提示信息
|
||||
globalMiddlewareList []gin.HandlerFunc // 全局中间件列表
|
||||
globalMiddlewareDescList []string // 全局中间件描述列表
|
||||
uriTable map[string]define.UriConfig // uri配置表
|
||||
}
|
||||
|
||||
func (s *server) getGlobalMiddlewareList(option *serverOption) []gin.HandlerFunc {
|
||||
globalMiddlewareList := make([]gin.HandlerFunc, 0)
|
||||
func (s *server) getGlobalMiddlewareList(option *serverOption) {
|
||||
// 全局 panic 捕获
|
||||
globalMiddlewareList = append(globalMiddlewareList, middleware.CustomRecover())
|
||||
s.globalMiddlewareList = append(s.globalMiddlewareList, middleware.CustomRecover())
|
||||
// 全局流控中间件
|
||||
s.globalMiddlewareList = append(s.globalMiddlewareList, middleware.RateLimit(s.option.rateLimitInstance, s.uriTable))
|
||||
if nil != option.initContextData {
|
||||
globalMiddlewareList = append(globalMiddlewareList, option.initContextData) // 初始化一些全局的变量
|
||||
s.globalMiddlewareList = append(s.globalMiddlewareList, option.initContextData) // 初始化一些全局的变量
|
||||
}
|
||||
if !option.disableInitRequest { // 启用了初始化请求
|
||||
globalMiddlewareList = append(globalMiddlewareList, middleware.InitRequest) // 初始化请求
|
||||
s.globalMiddlewareList = append(s.globalMiddlewareList, middleware.InitRequest) // 初始化请求
|
||||
}
|
||||
if nil != option.loggerCfg {
|
||||
// 请求日志记录中间件
|
||||
globalMiddlewareList = append(globalMiddlewareList, middleware.LogRequest(option.loggerCfg))
|
||||
s.globalMiddlewareList = append(s.globalMiddlewareList, middleware.LogRequest(option.loggerCfg))
|
||||
}
|
||||
if option.enableCors {
|
||||
// 跨域中间件
|
||||
globalMiddlewareList = append(globalMiddlewareList, request_cors.New(request_cors.Config{
|
||||
s.globalMiddlewareList = append(s.globalMiddlewareList, request_cors.New(request_cors.Config{
|
||||
AllowAllOrigins: true,
|
||||
AllowOrigins: nil,
|
||||
AllowOriginFunc: nil,
|
||||
@@ -146,10 +152,9 @@ func (s *server) getGlobalMiddlewareList(option *serverOption) []gin.HandlerFunc
|
||||
}
|
||||
if len(option.globalMiddlewareList) > 0 {
|
||||
// 自定义全局中间件追加
|
||||
globalMiddlewareList = append(globalMiddlewareList, option.globalMiddlewareList...)
|
||||
s.globalMiddlewareList = append(s.globalMiddlewareList, option.globalMiddlewareList...)
|
||||
}
|
||||
s.globalMiddlewareDescList = s.getMiddlewareDescList(globalMiddlewareList)
|
||||
return globalMiddlewareList
|
||||
s.globalMiddlewareDescList = s.getMiddlewareDescList(s.globalMiddlewareList)
|
||||
}
|
||||
|
||||
// Start 启动服务
|
||||
@@ -205,7 +210,7 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co
|
||||
} else {
|
||||
apiMiddlewareList = append(apiMiddlewareList, runtime.FuncForPC(reflect.ValueOf(s.RequestHandler).Pointer()).Name())
|
||||
}
|
||||
// 设置 超时 函数描述
|
||||
// 设置 超时 函数
|
||||
apiMiddlewareList = append(apiMiddlewareList, runtime.FuncForPC(reflect.ValueOf(middleware.Timeout).Pointer()).Name())
|
||||
// 设置 logic 函数描述
|
||||
apiMiddlewareList = append(apiMiddlewareList, runtime.FuncForPC(itemUriCfg.ApiLogicFunc.Func.Pointer()).Name())
|
||||
@@ -222,8 +227,10 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co
|
||||
if len(routerPrefix) > 0 {
|
||||
routerPrefix = "/" + strings.TrimSuffix(strings.TrimPrefix(routerPrefix, "/"), "/")
|
||||
}
|
||||
fullUriPath := routerPrefix + "/" + strings.TrimPrefix(itemUriCfg.Path, "/")
|
||||
s.uriTable[fullUriPath] = itemUriCfg // 注册路由时存储 接口路径 => 接口配置的信息
|
||||
s.consoleOutput = append(s.consoleOutput, []any{
|
||||
routerPrefix + "/" + strings.TrimPrefix(itemUriCfg.Path, "/"),
|
||||
fullUriPath,
|
||||
strings.ToUpper(method),
|
||||
strings.Join(apiMiddlewareList, "\n"),
|
||||
})
|
||||
@@ -234,7 +241,7 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co
|
||||
}
|
||||
|
||||
// registerRouter 注册路由
|
||||
func (s *server) registerRouter(routerGroup *gin.RouterGroup, method string, itemUriCfg UriConfig, handleFunc gin.HandlerFunc) {
|
||||
func (s *server) registerRouter(routerGroup *gin.RouterGroup, method string, itemUriCfg define.UriConfig, handleFunc gin.HandlerFunc) {
|
||||
funcList := []gin.HandlerFunc{
|
||||
middleware.Timeout(itemUriCfg.MaxExecTime), // 超时处理
|
||||
handleFunc,
|
||||
|
||||
@@ -10,6 +10,7 @@ package router
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/rate_limit"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -18,13 +19,13 @@ type testCommon struct {
|
||||
}
|
||||
|
||||
type testForm struct {
|
||||
Meta `json:"-" method:"get" path:"test"`
|
||||
Meta `json:"-" method:"get" path:"test" rate-limit:"1/5/60"`
|
||||
testCommon
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func TestNewServer(t *testing.T) {
|
||||
s := NewServer(9087)
|
||||
s := NewServer(9087, WithRateLimitInstance(rate_limit.MemoryClient))
|
||||
s.AddCommonParamRule("UserID", func(ctx *gin.Context) (any, error) {
|
||||
return uint(123456), nil
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user