tonglin0325的个人主页

go学习笔记——gin限流

如果想在Gin Web服务中实现限流功能,可以使用ulule/limiter

ulule/limiter是一款支持分布式限流的框架,其可以在Redis中存储和共享限流状态,从而在分布式环境中实现一致的限流逻辑。

1.引用

1
2
go get github.com/ulule/limiter/v3@v3.11.2

2.Gin限流中间件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package middleware

import (
"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"
redisstore "github.com/ulule/limiter/v3/drivers/store/redis"
"time"
)

func RateLimitMiddleWare(rdb *redis.Client) gin.HandlerFunc {
// 定义限流规则:1 请求/10秒
rate := limiter.Rate{
Period: 10 * time.Second,
Limit: 1,
}
// 创建限流存储
store, err := redisstore.NewStoreWithOptions(rdb, limiter.StoreOptions{
Prefix: "rate_limiter", // Redis 键的前缀
})
if err != nil {
panic(err)
}
// 创建限流实例
instance := limiter.New(store, rate)
middleware := mgin.NewMiddleware(instance)
return middleware
}

3.在router中使用限流中间件

1
2
3
4
// ratelimit
rateLimitMiddleWare := middleware.RateLimitMiddleWare(redisClient)
authGroup.Use(rateLimitMiddleWare)

4.限流效果,如果限流的话,接口将会返回429的状态码

1
2
3
4
5
6
7
8
9
10
11
12
curl -i --request GET 'http://172.17.0.229:18080/api/v1/user/3' \
--header 'Content-Type: application/json'
HTTP/1.1 429 Too Many Requests
Content-Type: text/plain; charset=utf-8
X-Ratelimit-Limit: 1
X-Ratelimit-Remaining: 0
X-Ratelimit-Reset: 1734772666
Date: Sat, 21 Dec 2024 09:17:38 GMT
Content-Length: 14

Limit exceeded

在redis中会存储限流状态,如下

其中默认的key是prefix+ip,value是API的请求次数