Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/douyacun/go-redis-lua
go redis lua常用脚本封装
https://github.com/douyacun/go-redis-lua
Last synced: 4 days ago
JSON representation
go redis lua常用脚本封装
- Host: GitHub
- URL: https://github.com/douyacun/go-redis-lua
- Owner: douyacun
- License: mit
- Created: 2024-05-28T02:42:01.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-06-05T02:09:34.000Z (5 months ago)
- Last Synced: 2024-07-13T09:13:47.272Z (4 months ago)
- Language: Go
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-redis-lua
go redis lua常用脚本封装### 令牌桶算法
测试每3秒5次限流,更多测试实例移步[token_bucket_limiter_test.go](token_bucket_limiter_test.go)
```go
// TestTokenBucketLimit_Allowed_3s5 测试每3秒5次限流
func TestTokenBucketLimit_Allowed_3s5(t *testing.T) {
rdb := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
DB: 0,
})
limiter := NewTokenBucketLimit(&TokenBucketLimitOption{
RDB: rdb,
Count: 5,
Duration: time.Second * 3,
Burst: 10,
})wg := sync.WaitGroup{}
ctx := context.Background()
ch := make(chan struct{}, 10)
for i := 1; i <= 100; i++ {
i := i
wg.Add(1)
if i%10 == 0 {
t.Logf("第%d秒", (i/10-1)*3)
}
go func() {
ch <- struct{}{}
allowed, remain, err := limiter.Allowed(ctx, 1)
t.Logf("i: %d allowed: %v remain: %d err %v", i, allowed, remain, err)
wg.Done()
<-ch
}()
if i%10 == 0 {
time.Sleep(3 * time.Second)
}
}
wg.Wait()
}
``````shell
token_bucket_limiter_test.go:66: 第0秒
token_bucket_limiter_test.go:71: i: 3 allowed: true remain: 4 err
token_bucket_limiter_test.go:71: i: 8 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 9 allowed: true remain: 3 err
token_bucket_limiter_test.go:71: i: 7 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 5 allowed: true remain: 5 err
token_bucket_limiter_test.go:71: i: 6 allowed: true remain: 1 err
token_bucket_limiter_test.go:71: i: 2 allowed: true remain: 0 err
token_bucket_limiter_test.go:71: i: 1 allowed: true remain: 2 err
token_bucket_limiter_test.go:71: i: 10 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 4 allowed: false remain: 0 err
token_bucket_limiter_test.go:66: 第3秒
token_bucket_limiter_test.go:71: i: 14 allowed: true remain: 4 err
token_bucket_limiter_test.go:71: i: 13 allowed: true remain: 3 err
token_bucket_limiter_test.go:71: i: 11 allowed: true remain: 2 err
token_bucket_limiter_test.go:71: i: 17 allowed: true remain: 1 err
token_bucket_limiter_test.go:71: i: 19 allowed: true remain: 0 err
token_bucket_limiter_test.go:71: i: 18 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 15 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 16 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 12 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 20 allowed: false remain: 0 err
token_bucket_limiter_test.go:66: 第6秒
token_bucket_limiter_test.go:71: i: 29 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 28 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 27 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 24 allowed: true remain: 0 err
token_bucket_limiter_test.go:71: i: 23 allowed: true remain: 1 err
token_bucket_limiter_test.go:71: i: 22 allowed: true remain: 2 err
token_bucket_limiter_test.go:71: i: 25 allowed: true remain: 3 err
token_bucket_limiter_test.go:71: i: 21 allowed: true remain: 4 err
token_bucket_limiter_test.go:71: i: 30 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 26 allowed: false remain: 0 err
token_bucket_limiter_test.go:66: 第9秒
token_bucket_limiter_test.go:71: i: 34 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 33 allowed: true remain: 4 err
token_bucket_limiter_test.go:71: i: 32 allowed: true remain: 3 err
token_bucket_limiter_test.go:71: i: 35 allowed: true remain: 1 err
token_bucket_limiter_test.go:71: i: 38 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 37 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 36 allowed: true remain: 0 err
token_bucket_limiter_test.go:71: i: 39 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 40 allowed: false remain: 0 err
token_bucket_limiter_test.go:71: i: 31 allowed: true remain: 2 err
```