-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (59 loc) · 1.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"github.com/go-redis/redis"
"fmt"
"time"
)
var redisdb *redis.Client
var counterLuaScript = `
-- 记录行为
redis.pcall("zadd", KEYS[1], ARGV[1], ARGV[1]); -- value 和 score 都使用纳秒时间戳,即ARGV[1]
redis.pcall("zremrangebyscore", KEYS[1], 0, ARGV[2]); -- 移除时间窗口之前的行为记录,剩下的都是时间窗口内的
local count = redis.pcall("zcard", KEYS[1]); -- 获取窗口内的行为数量
redis.pcall("expire", KEYS[1], ARGV[3]); -- 设置 zset 过期时间,避免冷用户持续占用内存
return count -- 返回窗口内行为数量`
var evalSha string
func init() {
initRedisClient()
}
func initRedisClient() {
redisdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
var err error
evalSha, err = redisdb.ScriptLoad(counterLuaScript).Result()
if err != nil {
panic(err)
}
}
// period单位为秒
func isAllowed(uid string, action string, period, maxCount int) bool {
key := fmt.Sprintf("%v_%v", uid, action)
now := time.Now().UnixNano()
beforeTime := now - int64(period*1000000000)
res, err := redisdb.EvalSha(evalSha, []string{key}, now, beforeTime, period).Result()
if err != nil {
panic(err)
}
if res.(int64) > int64(maxCount) {
return false
}
return true
}
func CreateOrder() {
canCreateOrder := isAllowed("berryjam", "createOrder", 5, 10)
if canCreateOrder {
// 处理下单逻辑
// ...
fmt.Println("下单成功")
} else { // 返回请求或者抛出异常、panic
panic("下单次数超限")
}
}
func main() {
for i := 0; i < 100; i++ {
CreateOrder()
}
}