添加redis/内存流控实现 #1
@ -7,7 +7,11 @@
|
|||||||
// Date : 2024-06-20 11:33
|
// Date : 2024-06-20 11:33
|
||||||
package abstract
|
package abstract
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/rate_limit/define"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
// IRateLimit 流控实现接口约束
|
// IRateLimit 流控实现接口约束
|
||||||
//
|
//
|
||||||
@ -15,11 +19,14 @@ import "context"
|
|||||||
//
|
//
|
||||||
// Date : 11:33 2024/6/20
|
// Date : 11:33 2024/6/20
|
||||||
type IRateLimit interface {
|
type IRateLimit interface {
|
||||||
Second(ctx context.Context, key string, total, rate int) (bool, error) // 每秒访问次数限制
|
// Second(ctx context.Context, key string, total, rate int) (bool, error) // 每秒访问次数限制
|
||||||
Minute(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) // 每小时访问次数限制
|
// Hour(ctx context.Context, key string, total, rate int) (bool, error) // 每小时访问次数限制
|
||||||
Day(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) // 每月访问次数限制
|
// Month(ctx context.Context, key string, total, rate int) (bool, error) // 每月访问次数限制
|
||||||
Year(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
|
// 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 // 重置
|
||||||
}
|
}
|
||||||
|
20
define/limit.go
Normal file
20
define/limit.go
Normal file
@ -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
|
||||||
|
}
|
26
go.mod
26
go.mod
@ -3,11 +3,31 @@ module git.zhangdeman.cn/zhangdeman/rate_limit
|
|||||||
go 1.22.4
|
go 1.22.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-redis/redis/v8 v8.11.5
|
git.zhangdeman.cn/zhangdeman/redis v0.0.0-20240620102217-f11c0a8216f6
|
||||||
github.com/go-redis/redis_rate/v9 v9.1.2
|
github.com/go-redis/redis_rate/v10 v10.0.1
|
||||||
|
github.com/redis/go-redis/v9 v9.5.3
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
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/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
|
||||||
)
|
)
|
||||||
|
95
go.sum
95
go.sum
@ -1,34 +1,73 @@
|
|||||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7 h1:QR8vMXOTy0NFKdodsGKA4gTNHJMfob3yRFYMXrZj7ek=
|
||||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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 h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
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/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||||
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
github.com/go-redis/redis_rate/v10 v10.0.1 h1:calPxi7tVlxojKunJwQ72kwfozdy25RjA0bCj1h0MUo=
|
||||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
github.com/go-redis/redis_rate/v10 v10.0.1/go.mod h1:EMiuO9+cjRkR7UvdvwMO7vbgqJkltQHtwbdIQvaBKIU=
|
||||||
github.com/go-redis/redis_rate/v9 v9.1.2 h1:H0l5VzoAtOE6ydd38j8MCq3ABlGLnvvbA1xDSVVCHgQ=
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
github.com/go-redis/redis_rate/v9 v9.1.2/go.mod h1:oam2de2apSgRG8aJzwJddXbNu91Iyz1m8IKJE2vpvlQ=
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
||||||
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
github.com/redis/go-redis/v9 v9.5.3 h1:fOAp1/uJG+ZtcITgZOfYFmTKPE7n4Vclj1wZFgRciUU=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/redis/go-redis/v9 v9.5.3/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||||
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
|
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||||
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
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=
|
||||||
|
52
redis.go
52
redis.go
@ -9,23 +9,16 @@ package rate_limit
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"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"
|
"time"
|
||||||
|
|
||||||
redisInstance "github.com/go-redis/redis/v8"
|
|
||||||
"github.com/go-redis/redis_rate/v9"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewRedis redis流控实例
|
var (
|
||||||
//
|
RedisClient = &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 ...
|
// Redis ...
|
||||||
//
|
//
|
||||||
@ -33,10 +26,36 @@ func NewRedis(redisClient *redisInstance.Client) abstract.IRateLimit {
|
|||||||
//
|
//
|
||||||
// Date : 11:44 2024/6/20
|
// Date : 11:44 2024/6/20
|
||||||
type Redis struct {
|
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 {
|
if total <= 0 || rate <= 0 {
|
||||||
return true, nil
|
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
|
return res.Allowed > 0, nil
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
56
redis_test.go
Normal file
56
redis_test.go
Normal file
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user