流控接口定义 + 基于redis的流控实现

This commit is contained in:
白茶清欢 2024-06-20 11:50:38 +08:00
parent f093b5c8ef
commit e4bd913390
4 changed files with 199 additions and 0 deletions

25
abstract/limit.go Normal file
View File

@ -0,0 +1,25 @@
// Package abstract ...
//
// Description : abstract ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-06-20 11:33
package abstract
import "context"
// IRateLimit 流控实现接口约束
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:33 2024/6/20
type IRateLimit interface {
Second(ctx context.Context, key string, total, rate int) (bool, error) // 每秒访问次数限制
Minute(ctx context.Context, key string, total, rate int) (bool, error) // 每分钟访问次数限制
Hour(ctx context.Context, key string, total, rate int) (bool, error) // 每小时访问次数限制
Day(ctx context.Context, key string, total, rate int) (bool, error) // 每天访问次数限制
Month(ctx context.Context, key string, total, rate int) (bool, error) // 每月访问次数限制
Year(ctx context.Context, key string, total, rate int) (bool, error) // 每年访问次数限制
Custom(ctx context.Context, timeInfo int64, key string, total, rate int) (bool, error) // 自定义指定时间内的访问次数, timeInfo 单位 : s
}

10
go.mod
View File

@ -1,3 +1,13 @@
module git.zhangdeman.cn/zhangdeman/rate_limit module git.zhangdeman.cn/zhangdeman/rate_limit
go 1.22.4 go 1.22.4
require (
github.com/go-redis/redis/v8 v8.11.5
github.com/go-redis/redis_rate/v9 v9.1.2
)
require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
)

34
go.sum Normal file
View File

@ -0,0 +1,34 @@
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-redis/redis_rate/v9 v9.1.2 h1:H0l5VzoAtOE6ydd38j8MCq3ABlGLnvvbA1xDSVVCHgQ=
github.com/go-redis/redis_rate/v9 v9.1.2/go.mod h1:oam2de2apSgRG8aJzwJddXbNu91Iyz1m8IKJE2vpvlQ=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

130
redis.go Normal file
View File

@ -0,0 +1,130 @@
// Package rate_limit ...
//
// Description : rate_limit ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-03-09 11:31
package rate_limit
import (
"context"
"git.zhangdeman.cn/zhangdeman/rate_limit/abstract"
"time"
redisInstance "github.com/go-redis/redis/v8"
"github.com/go-redis/redis_rate/v9"
)
// NewRedis redis流控实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:43 2024/6/20
func NewRedis(redisClient *redisInstance.Client) abstract.IRateLimit {
return &Redis{
limiter: redis_rate.NewLimiter(redisClient),
}
}
// Redis ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:44 2024/6/20
type Redis struct {
limiter *redis_rate.Limiter // limiter 限流实例
}
func (r *Redis) Second(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.PerSecond(total), rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Minute(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.PerMinute(total), rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Hour(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.PerHour(total), rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Day(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{
Rate: rate,
Period: 24 * time.Hour,
Burst: total,
}, rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Month(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{
Rate: rate,
Period: 24 * 30 * time.Hour,
Burst: total,
}, rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Year(ctx context.Context, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{
Rate: rate,
Period: 24 * 30 * 365 * time.Hour,
Burst: total,
}, rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}
func (r *Redis) Custom(ctx context.Context, timeInfo int64, key string, total, rate int) (bool, error) {
if total <= 0 || rate <= 0 {
return true, nil
}
res, err := r.limiter.AllowN(ctx, key, redis_rate.Limit{
Rate: rate,
Period: time.Duration(timeInfo) * time.Second,
Burst: total,
}, rate)
if nil != err {
return false, err
}
return res.Allowed > 0, nil
}