From e4bd9133907bded20bbd04c950a1169a3c0c97a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 20 Jun 2024 11:50:38 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=B5=81=E6=8E=A7=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=20+=20=E5=9F=BA=E4=BA=8Eredis=E7=9A=84?= =?UTF-8?q?=E6=B5=81=E6=8E=A7=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/limit.go | 25 +++++++++ go.mod | 10 ++++ go.sum | 34 ++++++++++++ redis.go | 130 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 abstract/limit.go create mode 100644 go.sum create mode 100644 redis.go diff --git a/abstract/limit.go b/abstract/limit.go new file mode 100644 index 0000000..ce650e1 --- /dev/null +++ b/abstract/limit.go @@ -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 +} diff --git a/go.mod b/go.mod index 35220c3..67e4610 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,13 @@ module git.zhangdeman.cn/zhangdeman/rate_limit 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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f268e97 --- /dev/null +++ b/go.sum @@ -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= diff --git a/redis.go b/redis.go new file mode 100644 index 0000000..3714ff5 --- /dev/null +++ b/redis.go @@ -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 +} From 54a77d170aba203c50f25ad9bb4ed7a63432144f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 20 Jun 2024 18:32:16 +0800 Subject: [PATCH 2/6] =?UTF-8?q?redis=E6=B5=81=E6=8E=A7=20+=20=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/limit.go | 23 ++++++++---- define/limit.go | 20 ++++++++++ go.mod | 26 +++++++++++-- go.sum | 95 +++++++++++++++++++++++++++++++++-------------- redis.go | 52 ++++++++++++++++++-------- redis_test.go | 56 ++++++++++++++++++++++++++++ 6 files changed, 217 insertions(+), 55 deletions(-) create mode 100644 define/limit.go create mode 100644 redis_test.go diff --git a/abstract/limit.go b/abstract/limit.go index ce650e1..dc6ffef 100644 --- a/abstract/limit.go +++ b/abstract/limit.go @@ -7,7 +7,11 @@ // Date : 2024-06-20 11:33 package abstract -import "context" +import ( + "context" + "git.zhangdeman.cn/zhangdeman/rate_limit/define" + "github.com/redis/go-redis/v9" +) // IRateLimit 流控实现接口约束 // @@ -15,11 +19,14 @@ import "context" // // 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 + // 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 + SetRedisClient(client *redis.ClusterClient) // 设置redis客户端 + AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenCnt int) (bool, error) // 申请N个令牌, N >= 0 + Reset(ctx context.Context, limitCfg *define.LimitConfig) error // 重置 } diff --git a/define/limit.go b/define/limit.go new file mode 100644 index 0000000..7e9ad56 --- /dev/null +++ b/define/limit.go @@ -0,0 +1,20 @@ +// Package define ... +// +// Description : define ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-06-20 14:36 +package define + +// LimitConfig ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:17 2024/6/20 +type LimitConfig struct { + Key string `json:"key"` // 操作的key + Total int `json:"total"` // 令牌桶大小 + Rate int `json:"rate"` // 同步速率 + TimeInterval int64 `json:"time_interval"` // 同步的时间间隔, 单位 : s +} diff --git a/go.mod b/go.mod index 67e4610..55645bb 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,31 @@ module git.zhangdeman.cn/zhangdeman/rate_limit go 1.22.4 require ( - github.com/go-redis/redis/v8 v8.11.5 - github.com/go-redis/redis_rate/v9 v9.1.2 + git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6 + github.com/go-redis/redis_rate/v10 v10.0.1 + github.com/redis/go-redis/v9 v9.5.3 ) require ( - github.com/cespare/xxhash/v2 v2.1.2 // indirect + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed // indirect + git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect + git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect + git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd // indirect + git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e // indirect + git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e // indirect + github.com/BurntSushi/toml v1.4.0 // indirect + github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/go-ini/ini v1.67.0 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/mozillazg/go-pinyin v0.20.0 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/spaolacci/murmur3 v1.1.0 // indirect + github.com/tidwall/gjson v1.17.1 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f268e97..acfdf50 100644 --- a/go.sum +++ b/go.sum @@ -1,34 +1,73 @@ -github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= -github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7 h1:QR8vMXOTy0NFKdodsGKA4gTNHJMfob3yRFYMXrZj7ek= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed h1:BGv+y6ZdtkxI4HeSIHun0QRrIzjLnUht0bKdyO2t6n4= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0= +git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U= +git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y= +git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620100925-63e4412d67e1 h1:sI8ON6xfATy12jNLscZnUgnr2SoRRD5eob7Rzc+d1bo= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620100925-63e4412d67e1/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6 h1:2q6yC/vBhDOLLjqziqMJUSuF3bgA3wFDC/swFMOrTPU= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd/go.mod h1:6+7whkCmb4sJDIfH3HxNuXRveaM0gCCNWd2uXZqNtIE= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e h1:+PeWa2QdYBWnL32CfAAgy0dlaRCVNmYZDH4q+9w7Gfg= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e/go.mod h1:US/pcq2vstE3iyxIHf53w8IeXKkZys7bj/ozLWkRYeE= +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= +github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/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/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/go-redis/redis_rate/v10 v10.0.1 h1:calPxi7tVlxojKunJwQ72kwfozdy25RjA0bCj1h0MUo= +github.com/go-redis/redis_rate/v10 v10.0.1/go.mod h1:EMiuO9+cjRkR7UvdvwMO7vbgqJkltQHtwbdIQvaBKIU= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ= +github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc= +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 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= +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/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= +github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg= +github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U= +github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/redis.go b/redis.go index 3714ff5..038fab3 100644 --- a/redis.go +++ b/redis.go @@ -9,23 +9,16 @@ package rate_limit import ( "context" - "git.zhangdeman.cn/zhangdeman/rate_limit/abstract" + "git.zhangdeman.cn/zhangdeman/rate_limit/define" + "github.com/go-redis/redis_rate/v10" + redisInstance "github.com/redis/go-redis/v9" + "sync" "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), - } -} +var ( + RedisClient = &Redis{} +) // Redis ... // @@ -33,10 +26,36 @@ func NewRedis(redisClient *redisInstance.Client) abstract.IRateLimit { // // Date : 11:44 2024/6/20 type Redis struct { - limiter *redis_rate.Limiter // limiter 限流实例 + limiter *redis_rate.Limiter // limiter 限流实例 + redisClient *redisInstance.ClusterClient // redis 客户端实例 + lock *sync.RWMutex } -func (r *Redis) Second(ctx context.Context, key string, total, rate int) (bool, error) { +func (r *Redis) SetRedisClient(client *redisInstance.ClusterClient) { + r.redisClient = client + r.limiter = redis_rate.NewLimiter(r.redisClient) +} + +func (r *Redis) AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenCnt int) (bool, error) { + if limitCfg.Total <= 0 || limitCfg.Rate <= 0 { + return true, nil + } + res, err := r.limiter.AllowN(ctx, limitCfg.Key, redis_rate.Limit{ + Rate: limitCfg.Rate, + Period: time.Duration(limitCfg.TimeInterval) * time.Second, + Burst: limitCfg.Total, + }, tokenCnt) + if nil != err { + return false, err + } + return res.Allowed > 0, nil +} + +func (r *Redis) Reset(ctx context.Context, limitCfg *define.LimitConfig) error { + return r.limiter.Reset(ctx, limitCfg.Key) +} + +/*func (r *Redis) Second(ctx context.Context, key string, total, rate int) (bool, error) { if total <= 0 || rate <= 0 { return true, nil } @@ -128,3 +147,4 @@ func (r *Redis) Custom(ctx context.Context, timeInfo int64, key string, total, r } return res.Allowed > 0, nil } +*/ diff --git a/redis_test.go b/redis_test.go new file mode 100644 index 0000000..c0b1e9e --- /dev/null +++ b/redis_test.go @@ -0,0 +1,56 @@ +// Package rate_limit ... +// +// Description : rate_limit ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-06-20 18:01 +package rate_limit + +import ( + "context" + "fmt" + "git.zhangdeman.cn/zhangdeman/rate_limit/define" + wrapRedis "git.zhangdeman.cn/zhangdeman/redis" + wrapRedisDefine "git.zhangdeman.cn/zhangdeman/redis/define" + "testing" +) + +func TestRedis_AllowN(t *testing.T) { + _ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.ClusterOptions{ + Addrs: []string{"127.0.0.1:6379"}, + ClientName: "", + MaxRedirects: 0, + ReadOnly: false, + RouteByLatency: false, + RouteRandomly: false, + Protocol: 0, + Username: "", + Password: "123456", + MaxRetries: 0, + MinRetryBackoff: 0, + MaxRetryBackoff: 0, + DialTimeout: 0, + ReadTimeout: 0, + WriteTimeout: 0, + ContextTimeoutEnabled: false, + PoolFIFO: false, + PoolSize: 0, + PoolTimeout: 0, + MinIdleConn: 0, + MaxIdleConn: 0, + MaxActiveConn: 0, + ConnMaxIdleTime: 0, + ConnMaxLifetime: 0, + DisableIdentity: false, + IdentitySuffix: "", + }) + RedisClient.SetRedisClient(wrapRedis.Client.GetRealClient("test_rate_limit")) + allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{ + Key: "deman", + Total: 600, + Rate: 10, + TimeInterval: 60, + }, 8) + fmt.Println(allow, err) +} From 545523e324bb9df40f2cae086e0527dcc4608314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 20 Jun 2024 18:43:27 +0800 Subject: [PATCH 3/6] =?UTF-8?q?redis=E6=B5=81=E6=8E=A7=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/limit.go | 2 +- go.mod | 2 +- go.sum | 2 ++ redis.go | 6 +++--- redis_test.go | 27 ++++++++++++++------------- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/abstract/limit.go b/abstract/limit.go index dc6ffef..df0f8f3 100644 --- a/abstract/limit.go +++ b/abstract/limit.go @@ -26,7 +26,7 @@ type IRateLimit interface { // 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 - SetRedisClient(client *redis.ClusterClient) // 设置redis客户端 + SetRedisClient(client *redis.Client) // 设置redis客户端 AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenCnt int) (bool, error) // 申请N个令牌, N >= 0 Reset(ctx context.Context, limitCfg *define.LimitConfig) error // 重置 } diff --git a/go.mod b/go.mod index 55645bb..d0d7c33 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.zhangdeman.cn/zhangdeman/rate_limit go 1.22.4 require ( - git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6 + git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391 github.com/go-redis/redis_rate/v10 v10.0.1 github.com/redis/go-redis/v9 v9.5.3 ) diff --git a/go.sum b/go.sum index acfdf50..6c3cd3e 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620100925-63e4412d67e1 h1:sI8ON6x git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620100925-63e4412d67e1/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6 h1:2q6yC/vBhDOLLjqziqMJUSuF3bgA3wFDC/swFMOrTPU= git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391 h1:DLzEEPHoFPqQlowIU6VPT/45FVnmVG6lVoeI/VHZ+Ho= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w= diff --git a/redis.go b/redis.go index 038fab3..d12a2c7 100644 --- a/redis.go +++ b/redis.go @@ -26,12 +26,12 @@ var ( // // Date : 11:44 2024/6/20 type Redis struct { - limiter *redis_rate.Limiter // limiter 限流实例 - redisClient *redisInstance.ClusterClient // redis 客户端实例 + limiter *redis_rate.Limiter // limiter 限流实例 + redisClient *redisInstance.Client // redis 客户端实例 lock *sync.RWMutex } -func (r *Redis) SetRedisClient(client *redisInstance.ClusterClient) { +func (r *Redis) SetRedisClient(client *redisInstance.Client) { r.redisClient = client r.limiter = redis_rate.NewLimiter(r.redisClient) } diff --git a/redis_test.go b/redis_test.go index c0b1e9e..6b8c6a0 100644 --- a/redis_test.go +++ b/redis_test.go @@ -14,16 +14,14 @@ import ( wrapRedis "git.zhangdeman.cn/zhangdeman/redis" wrapRedisDefine "git.zhangdeman.cn/zhangdeman/redis/define" "testing" + "time" ) func TestRedis_AllowN(t *testing.T) { - _ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.ClusterOptions{ - Addrs: []string{"127.0.0.1:6379"}, + _ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.Options{ + Addr: "127.0.0.1:6379", + Network: "TCP", ClientName: "", - MaxRedirects: 0, - ReadOnly: false, - RouteByLatency: false, - RouteRandomly: false, Protocol: 0, Username: "", Password: "123456", @@ -46,11 +44,14 @@ func TestRedis_AllowN(t *testing.T) { IdentitySuffix: "", }) RedisClient.SetRedisClient(wrapRedis.Client.GetRealClient("test_rate_limit")) - allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{ - Key: "deman", - Total: 600, - Rate: 10, - TimeInterval: 60, - }, 8) - fmt.Println(allow, err) + for i := 0; i < 100; i++ { + allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{ + Key: "deman", + Total: 600, + Rate: 10, + TimeInterval: 60, + }, 8) + fmt.Println(allow, err) + time.Sleep(time.Second) + } } From 40bfaafa5a6a7a344753deec656963349385fef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 20 Jun 2024 18:48:54 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E8=B0=83=E8=AF=95=E5=AE=8C=E6=88=90redis?= =?UTF-8?q?=E6=B5=81=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- go.sum | 2 ++ redis_test.go | 30 +++++------------------------- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index d0d7c33..b3e5537 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.zhangdeman.cn/zhangdeman/rate_limit go 1.22.4 require ( - git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391 + git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620104455-27e193a0ea47 github.com/go-redis/redis_rate/v10 v10.0.1 github.com/redis/go-redis/v9 v9.5.3 ) diff --git a/go.sum b/go.sum index 6c3cd3e..d17a7a5 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6 h1:2q6yC/v git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391 h1:DLzEEPHoFPqQlowIU6VPT/45FVnmVG6lVoeI/VHZ+Ho= git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620104455-27e193a0ea47 h1:Taq1XyvDgFM/xTSEw3cUOWh4krgqfEDIzcVrqQ7Z3UE= +git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620104455-27e193a0ea47/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w= diff --git a/redis_test.go b/redis_test.go index 6b8c6a0..fc36fbc 100644 --- a/redis_test.go +++ b/redis_test.go @@ -19,35 +19,15 @@ import ( func TestRedis_AllowN(t *testing.T) { _ = wrapRedis.Client.AddClient("test_rate_limit", &wrapRedisDefine.Options{ - Addr: "127.0.0.1:6379", - Network: "TCP", - ClientName: "", - Protocol: 0, - Username: "", - Password: "123456", - MaxRetries: 0, - MinRetryBackoff: 0, - MaxRetryBackoff: 0, - DialTimeout: 0, - ReadTimeout: 0, - WriteTimeout: 0, - ContextTimeoutEnabled: false, - PoolFIFO: false, - PoolSize: 0, - PoolTimeout: 0, - MinIdleConn: 0, - MaxIdleConn: 0, - MaxActiveConn: 0, - ConnMaxIdleTime: 0, - ConnMaxLifetime: 0, - DisableIdentity: false, - IdentitySuffix: "", + Addr: "127.0.0.1:6379", + Password: "123456", + DB: 6, }) RedisClient.SetRedisClient(wrapRedis.Client.GetRealClient("test_rate_limit")) - for i := 0; i < 100; i++ { + for i := 0; i < 1000; i++ { allow, err := RedisClient.AllowN(context.Background(), &define.LimitConfig{ Key: "deman", - Total: 600, + Total: 60, Rate: 10, TimeInterval: 60, }, 8) From 7c31a1111ee1b12e544bd57eed121d6e2b66a4c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 20 Jun 2024 20:39:53 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/limit.go | 9 --------- go.sum | 14 -------------- 2 files changed, 23 deletions(-) diff --git a/abstract/limit.go b/abstract/limit.go index df0f8f3..0dd3382 100644 --- a/abstract/limit.go +++ b/abstract/limit.go @@ -10,7 +10,6 @@ package abstract import ( "context" "git.zhangdeman.cn/zhangdeman/rate_limit/define" - "github.com/redis/go-redis/v9" ) // IRateLimit 流控实现接口约束 @@ -19,14 +18,6 @@ import ( // // 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 - SetRedisClient(client *redis.Client) // 设置redis客户端 AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenCnt int) (bool, error) // 申请N个令牌, N >= 0 Reset(ctx context.Context, limitCfg *define.LimitConfig) error // 重置 } diff --git a/go.sum b/go.sum index d17a7a5..9664f02 100644 --- a/go.sum +++ b/go.sum @@ -1,21 +1,11 @@ -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7 h1:QR8vMXOTy0NFKdodsGKA4gTNHJMfob3yRFYMXrZj7ek= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed h1:BGv+y6ZdtkxI4HeSIHun0QRrIzjLnUht0bKdyO2t6n4= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI= -git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620100925-63e4412d67e1 h1:sI8ON6xfATy12jNLscZnUgnr2SoRRD5eob7Rzc+d1bo= -git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620100925-63e4412d67e1/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= -git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6 h1:2q6yC/vBhDOLLjqziqMJUSuF3bgA3wFDC/swFMOrTPU= -git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= -git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391 h1:DLzEEPHoFPqQlowIU6VPT/45FVnmVG6lVoeI/VHZ+Ho= -git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620103823-e8b63fb86391/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620104455-27e193a0ea47 h1:Taq1XyvDgFM/xTSEw3cUOWh4krgqfEDIzcVrqQ7Z3UE= git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620104455-27e193a0ea47/go.mod h1:OImVgoNTw9qWxGrgrym0wtEMdx0ekaH7vJJKIgSpdEg= -git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0= -git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd/go.mod h1:6+7whkCmb4sJDIfH3HxNuXRveaM0gCCNWd2uXZqNtIE= git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI= @@ -30,8 +20,6 @@ github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= -github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= -github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -56,8 +44,6 @@ github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0b github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg= -github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U= github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= From d509496d72a55f101e40b1260d98bcea6ea9e3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 20 Jun 2024 20:58:15 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=86=85=E5=AD=98?= =?UTF-8?q?=E7=9A=84=E6=B5=81=E6=8E=A7=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ memory.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ memory_test.go | 27 +++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 memory.go create mode 100644 memory_test.go diff --git a/go.mod b/go.mod index b3e5537..926c626 100644 --- a/go.mod +++ b/go.mod @@ -29,5 +29,6 @@ require ( github.com/tidwall/pretty v1.2.1 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.27.0 // indirect + golang.org/x/time v0.5.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 9664f02..69ef50f 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/memory.go b/memory.go new file mode 100644 index 0000000..c61dc1c --- /dev/null +++ b/memory.go @@ -0,0 +1,54 @@ +// Package rate_limit ... +// +// Description : rate_limit ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-06-20 20:40 +package rate_limit + +import ( + "context" + "git.zhangdeman.cn/zhangdeman/rate_limit/define" + "golang.org/x/time/rate" + "sync" + "time" +) + +var ( + MemoryClient = &Memory{ + lock: &sync.RWMutex{}, + limiterTable: make(map[string]*rate.Limiter), + } +) + +// Memory ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:40 2024/6/20 +type Memory struct { + lock *sync.RWMutex + limiterTable map[string]*rate.Limiter +} + +func (m *Memory) AllowN(ctx context.Context, limitCfg *define.LimitConfig, tokenCnt int) (bool, error) { + limiter := m.getLimiter(limitCfg) + return limiter.AllowN(time.Now(), tokenCnt), nil +} + +func (m *Memory) Reset(ctx context.Context, limitCfg *define.LimitConfig) error { + m.lock.Lock() + defer m.lock.Unlock() + delete(m.limiterTable, limitCfg.Key) + return nil +} + +func (m *Memory) getLimiter(limitCfg *define.LimitConfig) *rate.Limiter { + m.lock.Lock() + defer m.lock.Unlock() + if _, exist := m.limiterTable[limitCfg.Key]; !exist { + m.limiterTable[limitCfg.Key] = rate.NewLimiter(rate.Limit(limitCfg.Rate), limitCfg.Total) + } + return m.limiterTable[limitCfg.Key] +} diff --git a/memory_test.go b/memory_test.go new file mode 100644 index 0000000..9dfdb94 --- /dev/null +++ b/memory_test.go @@ -0,0 +1,27 @@ +// Package rate_limit ... +// +// Description : rate_limit ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-06-20 20:55 +package rate_limit + +import ( + "context" + "fmt" + "git.zhangdeman.cn/zhangdeman/rate_limit/define" + "testing" +) + +func TestMemory_AllowN(t *testing.T) { + + for i := 0; i < 10; i++ { + fmt.Println(MemoryClient.AllowN(context.Background(), &define.LimitConfig{ + Key: "deman", + Total: 15, + Rate: 10, + TimeInterval: 10, + }, 8)) + } +}