From c318733af7ee1ae456667046c652de52c2645dee 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, 9 Mar 2023 12:16:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=A7=92=E3=80=81?= =?UTF-8?q?=E5=88=86=E9=92=9F=E3=80=81=E5=B0=8F=E6=97=B6=E3=80=81=E5=A4=A9?= =?UTF-8?q?=E7=BA=A7=E5=88=AB=E7=9A=84=E6=B5=81=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + go.mod | 1 + go.sum | 2 ++ rate_limit/limit.go | 86 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 rate_limit/limit.go diff --git a/.gitignore b/.gitignore index c9f6b44..72f529f 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ .idea .vscode mail_test.go +*_test.go diff --git a/go.mod b/go.mod index d67a8c3..d2cc83d 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect + github.com/go-redis/redis_rate/v9 v9.1.2 // indirect github.com/goccy/go-json v0.10.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/leodido/go-urn v1.2.1 // indirect diff --git a/go.sum b/go.sum index 45ab253..ac4d632 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,8 @@ github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyh github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= 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/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM= github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= diff --git a/rate_limit/limit.go b/rate_limit/limit.go new file mode 100644 index 0000000..4657b2f --- /dev/null +++ b/rate_limit/limit.go @@ -0,0 +1,86 @@ +// Package rate_limit ... +// +// Description : rate_limit ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-03-09 11:31 +package rate_limit + +import ( + "context" + "time" + + redisInstance "github.com/go-redis/redis/v8" + "github.com/go-redis/redis_rate/v9" +) + +var ( + // limiter 限流实例 + limiter *redis_rate.Limiter +) + +// InitLimiter 初始化限流器实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:40 2023/3/9 +func InitLimiter(redisClient *redisInstance.Client) { + limiter = redis_rate.NewLimiter(redisClient) +} + +// Second 每秒允许的访问数 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:42 2023/3/9 +func Second(ctx context.Context, key string, total, rate int) bool { + res, err := limiter.AllowN(ctx, key, redis_rate.PerSecond(total), rate) + if nil != err { + return false + } + return res.Allowed > 0 +} + +// Minute 每分钟允许访问数 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:46 2023/3/9 +func Minute(ctx context.Context, key string, total, rate int) (bool, error) { + res, err := limiter.AllowN(ctx, key, redis_rate.PerMinute(total), rate) + if nil != err { + return false, err + } + return res.Allowed > 0, nil +} + +// Hour 每小时允许访问数 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:47 2023/3/9 +func Hour(ctx context.Context, key string, total, rate int) (bool, error) { + res, err := limiter.AllowN(ctx, key, redis_rate.PerHour(total), rate) + if nil != err { + return false, err + } + return res.Allowed > 0, nil +} + +// Day 每天允许访问数 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:47 2023/3/9 +func Day(ctx context.Context, key string, total, rate int) (bool, error) { + res, err := 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 +} -- 2.36.6 From 2018e816c705e6e4f37e5a7f1d88f55362d50e35 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, 9 Mar 2023 12:17:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=A0=E5=85=A5=E7=9A=84total=E5=92=8Cra?= =?UTF-8?q?te=E4=BB=BB=E6=84=8F=E4=B8=80=E4=B8=AA=E5=B0=8F=E4=BA=8E?= =?UTF-8?q?=E7=AD=89=E4=BA=8E0=EF=BC=8C=E8=AE=A4=E4=B8=BA=E4=B8=8D?= =?UTF-8?q?=E9=99=90=E9=80=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rate_limit/limit.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/rate_limit/limit.go b/rate_limit/limit.go index 4657b2f..ca10738 100644 --- a/rate_limit/limit.go +++ b/rate_limit/limit.go @@ -34,12 +34,15 @@ func InitLimiter(redisClient *redisInstance.Client) { // Author : go_developer@163.com<白茶清欢> // // Date : 11:42 2023/3/9 -func Second(ctx context.Context, key string, total, rate int) bool { +func Second(ctx context.Context, key string, total, rate int) (bool, error) { + if total <= 0 || rate <= 0 { + return true, nil + } res, err := limiter.AllowN(ctx, key, redis_rate.PerSecond(total), rate) if nil != err { - return false + return false, err } - return res.Allowed > 0 + return res.Allowed > 0, nil } // Minute 每分钟允许访问数 @@ -48,6 +51,9 @@ func Second(ctx context.Context, key string, total, rate int) bool { // // Date : 11:46 2023/3/9 func Minute(ctx context.Context, key string, total, rate int) (bool, error) { + if total <= 0 || rate <= 0 { + return true, nil + } res, err := limiter.AllowN(ctx, key, redis_rate.PerMinute(total), rate) if nil != err { return false, err @@ -61,6 +67,9 @@ func Minute(ctx context.Context, key string, total, rate int) (bool, error) { // // Date : 11:47 2023/3/9 func Hour(ctx context.Context, key string, total, rate int) (bool, error) { + if total <= 0 || rate <= 0 { + return true, nil + } res, err := limiter.AllowN(ctx, key, redis_rate.PerHour(total), rate) if nil != err { return false, err @@ -74,6 +83,9 @@ func Hour(ctx context.Context, key string, total, rate int) (bool, error) { // // Date : 11:47 2023/3/9 func Day(ctx context.Context, key string, total, rate int) (bool, error) { + if total <= 0 || rate <= 0 { + return true, nil + } res, err := limiter.AllowN(ctx, key, redis_rate.Limit{ Rate: rate, Period: 24 * time.Hour, -- 2.36.6