【新增】添加接口调用限流

This commit is contained in:
PandaGoAdmin
2022-01-19 17:29:01 +08:00
parent 3d603d8fde
commit c8449e0432
7 changed files with 49 additions and 2 deletions

View File

@@ -342,7 +342,7 @@ func (u *UserApi) InsertSysUser(rc *ctx.ReqCtx) {
// @Tags 用户
// @Accept application/json
// @Product application/json
// @Param data body entity.SysUser true "用户数据"
// @Param data body entity.SysUser true "用户数据"g
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /system/user/sysUser [put]

View File

@@ -6,6 +6,7 @@ type Server struct {
Port int `yaml:"port"`
Model string `yaml:"model"`
Cors bool `yaml:"cors"`
Rate *Rate `yaml:"rate"`
IsInitTable bool `yaml:"isInitTable"`
DbType string `yaml:"db-type"`
ExcelDir string `yaml:"excel-dir"`
@@ -33,3 +34,8 @@ type Tls struct {
KeyFile string `yaml:"key-file"` // 私钥文件路径
CertFile string `yaml:"cert-file"` // 证书文件路径
}
type Rate struct {
IsRate bool `yaml:"is-rate"` // 是否限流
RateNum float64 `yaml:"rate-num"` // 限流数量
}

View File

@@ -7,6 +7,9 @@ server:
model: release
port: 7788
cors: true
rate:
is-rate: true
rate-num: 100
db-type: mysql
isInitTable: false
excel-dir: ./resource/excel/
@@ -37,7 +40,7 @@ redis:
port: 6379
mysql:
host: 127.0.0.1:3306
host: 47.104.252.2:3306
username: pandax
password: pandax
db-name: pandax

2
go.mod
View File

@@ -19,6 +19,7 @@ require (
github.com/casbin/casbin/v2 v2.37.4
github.com/casbin/gorm-adapter/v3 v3.4.6
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/didip/tollbooth v4.0.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible // indirect
@@ -29,6 +30,7 @@ require (
github.com/mojocn/base64Captcha v1.3.5
github.com/mssola/user_agent v0.5.3
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.8.1
github.com/qiniu/go-sdk/v7 v7.11.0
github.com/robfig/cron/v3 v3.0.1

4
go.sum
View File

@@ -40,6 +40,8 @@ github.com/denisenkom/go-mssqldb v0.11.0 h1:9rHa233rhdOyrz2GcP9NM+gi2psgJZ4GWDpL
github.com/denisenkom/go-mssqldb v0.11.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/didip/tollbooth v4.0.2+incompatible h1:fVSa33JzSz0hoh2NxpwZtksAzAgd7zjmGO20HCZtF4M=
github.com/didip/tollbooth v4.0.2+incompatible/go.mod h1:A9b0665CE6l1KmzpDws2++elm/CsuWBMa5Jv4WY0PEY=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
@@ -254,6 +256,8 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJ
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

View File

@@ -51,6 +51,10 @@ func InitRouter() *gin.Engine {
if serverConfig.Cors {
router.Use(middleware.Cors())
}
// 流量限制
if serverConfig.Rate.IsRate {
router.Use(middleware.Rate())
}
// api接口
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// 设置路由组

28
middleware/rate.go Normal file
View File

@@ -0,0 +1,28 @@
package middleware
import (
"github.com/didip/tollbooth"
"github.com/gin-gonic/gin"
"pandax/base/config"
)
/**
* @Description 添加qq群467890197 交流学习
* @Author 熊猫
* @Date 2022/1/19 8:28
**/
//限流中间件
func Rate() gin.HandlerFunc {
lmt := tollbooth.NewLimiter(config.Conf.Server.Rate.RateNum, nil)
lmt.SetMessage("已经超出接口请求限制,稍后再试.")
return func(c *gin.Context) {
httpError := tollbooth.LimitByRequest(lmt, c.Writer, c.Request)
if httpError != nil {
c.Data(httpError.StatusCode, lmt.GetMessageContentType(), []byte(httpError.Message))
c.Abort()
} else {
c.Next()
}
}
}