Skip to content

Commit

Permalink
feat: update limiter & style check (#2)
Browse files Browse the repository at this point in the history
* feat: 更新限流器

* update: style check
  • Loading branch information
Tohrusky authored Jul 12, 2024
1 parent 2f17288 commit 94b719c
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 47 deletions.
4 changes: 1 addition & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ linters:
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
# - goimports
- goprintffuncname
- gosimple
- govet
Expand All @@ -27,7 +26,6 @@ linters:
- unconvert
- unparam
- whitespace
- gofumpt
disable:
- unused

Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ signs:
"--output",
"${signature}",
"--detach-sign",
"${artifact}",
"${artifact}"
]

snapshot:
Expand Down
5 changes: 5 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
singleQuote: false
semi: false
printWidth: 100
trailingComma: none
endOfLine: crlf
5 changes: 2 additions & 3 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"

"github.com/TensoRaws/NuxBT-Backend/internal/router"
"github.com/urfave/cli/v2"

"github.com/TensoRaws/NuxBT-Backend/internal/middleware/cache"
"github.com/TensoRaws/NuxBT-Backend/module/cache"
"github.com/TensoRaws/NuxBT-Backend/module/config"
"github.com/TensoRaws/NuxBT-Backend/module/db"
"github.com/TensoRaws/NuxBT-Backend/module/log"
"github.com/TensoRaws/NuxBT-Backend/module/oss"
"github.com/urfave/cli/v2"
)

// CmdWeb api 子命令
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-gonic/gin v1.10.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/redis/go-redis/v9 v9.5.3
github.com/redis/go-redis/v9 v9.5.4
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/ulule/limiter/v3 v3.11.2
github.com/urfave/cli/v2 v2.27.2
golang.org/x/crypto v0.25.0
gorm.io/driver/mysql v1.5.7
Expand Down Expand Up @@ -78,6 +79,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,15 @@ github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.5.3 h1:fOAp1/uJG+ZtcITgZOfYFmTKPE7n4Vclj1wZFgRciUU=
github.com/redis/go-redis/v9 v9.5.3/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.5.4 h1:vOFYDKKVgrI5u++QvnMT7DksSMYg7Aw/Np4vLJLKLwY=
github.com/redis/go-redis/v9 v9.5.4/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down Expand Up @@ -200,6 +204,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/ulule/limiter/v3 v3.11.2 h1:P4yOrxoEMJbOTfRJR2OzjL90oflzYPPmWg+dvwN2tHA=
github.com/ulule/limiter/v3 v3.11.2/go.mod h1:QG5GnFOCV+k7lrL5Y8kgEeeflPH3+Cviqlqa8SVSQxI=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
Expand Down
43 changes: 12 additions & 31 deletions internal/middleware/cache/ip_limiter.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,26 @@
package cache

import (
"fmt"
"time"

"github.com/TensoRaws/NuxBT-Backend/module/cache"
"github.com/TensoRaws/NuxBT-Backend/module/log"
"github.com/TensoRaws/NuxBT-Backend/module/util"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
"github.com/ulule/limiter/v3"
mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
redisLimiter "github.com/ulule/limiter/v3/drivers/store/redis"
)

func NewRateLimiter(redisClient *cache.Client, key string, limit int, slidingWindow time.Duration) gin.HandlerFunc {
_, err := redisClient.Ping().Result()
func NewRateLimiter(redisClient *cache.Client, limit int, slidingWindow time.Duration) gin.HandlerFunc {
rate := limiter.Rate{
Period: slidingWindow,
Limit: int64(limit),
}
store, err := redisLimiter.NewStore(redisClient.C)
if err != nil {
panic(fmt.Sprint("error init redis", err.Error()))
log.Logger.Error(err)
}
l := limiter.New(store, rate, limiter.WithClientIPHeader("True-Client-IP"))

return func(c *gin.Context) {
now := time.Now().UnixNano()
log.Logger.Infof("-------------------> path: %v", c.Request.URL.Path)
userCntKey := fmt.Sprint(c.ClientIP(), ":", key, ":", c.Request.URL.Path)

_, err := redisClient.ZRemRangeByScore(userCntKey,
"0",
fmt.Sprint(now-(slidingWindow.Nanoseconds()))).Result()
if err != nil {
log.Logger.Error(err)
return
}

reqs, _ := redisClient.ZRange(userCntKey, 0, -1).Result()

if len(reqs) >= limit {
util.AbortWithMsg(c, "Too many request...")
log.Logger.Warnf("------------------> too many request, key: %v", userCntKey)
return
}

c.Next()
redisClient.ZAddNX(userCntKey, redis.Z{Score: float64(now), Member: float64(now)})
redisClient.Expire(userCntKey, slidingWindow)
}
return mgin.NewMiddleware(l)
}
2 changes: 1 addition & 1 deletion internal/router/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewAPI() *gin.Engine {
r := gin.New()
r.Use(logger.DefaultLogger(), gin.Recovery()) // 日志中间件
r.Use(middleware_cache.NewRateLimiter(
middleware_cache.Clients[cache.IPLimit], "general", config.ServerConfig.RequestLimit, 60*time.Second))
cache.Clients[cache.IPLimit], config.ServerConfig.RequestLimit, 60*time.Second))

r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
Expand Down
9 changes: 4 additions & 5 deletions internal/middleware/cache/cache.go → module/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"bytes"
"sync"

"github.com/TensoRaws/NuxBT-Backend/module/cache"
"github.com/gin-gonic/gin"
)

var once sync.Once

var Clients = map[cache.RDB]*cache.Client{
cache.IPLimit: {},
cache.User: {},
var Clients = map[RDB]*Client{
IPLimit: {},
User: {},
}

type responseWriter struct {
Expand All @@ -29,6 +28,6 @@ func (w responseWriter) Write(b []byte) (int, error) {

func Init() {
once.Do(func() {
cache.NewRedisClients(Clients)
NewRedisClients(Clients)
})
}
2 changes: 0 additions & 2 deletions module/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ func NewRedisClient(n int) *Client {
return &Client{C: r, Ctx: ctx}
}

// 封装常用接口

// ClientGetName returns the name of the connection.
func (c Client) ClientGetName() *redis.StringCmd {
return c.C.ClientGetName(c.Ctx)
Expand Down

0 comments on commit 94b719c

Please sign in to comment.