-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update limiter & style check (#2)
* feat: 更新限流器 * update: style check
- Loading branch information
Showing
10 changed files
with
35 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ signs: | |
"--output", | ||
"${signature}", | ||
"--detach-sign", | ||
"${artifact}", | ||
"${artifact}" | ||
] | ||
|
||
snapshot: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
singleQuote: false | ||
semi: false | ||
printWidth: 100 | ||
trailingComma: none | ||
endOfLine: crlf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters