-
Notifications
You must be signed in to change notification settings - Fork 5
/
lua.go
79 lines (66 loc) · 1.97 KB
/
lua.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
70
71
72
73
74
75
76
77
78
79
package limiter
const ResetScript = `
local routeKey = KEYS[1]
local staticKey = KEYS[2]
local routeDeadline = ARGV[1]
redis.call('HSET', staticKey, "Count", 1)
redis.call('HSET', routeKey, "Count", 1, "Deadline", routeDeadline)
return 0
`
const Script = `
local result = {}
local routeKey = KEYS[1]
local staticKey = KEYS[2]
local routeLimit = tonumber(ARGV[1])
local staticLimit = tonumber(ARGV[2])
local routeDeadline = tonumber(ARGV[3]) -- 如果過期或者初次造訪 要更新的時間
local now = tonumber(ARGV[4])
local staticCount = redis.call('HGET', staticKey, "Count")
-- First time visit
if not staticCount then
redis.call('HSET', staticKey, "Count", 1)
redis.call('HSET', routeKey, "Count", 1, "Deadline", routeDeadline)
result[1] = staticLimit - 1
result[2] = routeLimit - 1
result[3] = routeDeadline
return result
end
local routeInfo = redis.call('HGETALL', routeKey)
if #routeInfo == 0 then
if tonumber(staticCount) < staticLimit then
result[1] = staticLimit - redis.call('HINCRBY', staticKey, "Count", 1)
else
result[1] = -1
end
redis.call('HSET', routeKey, "Count", 1, "Deadline", routeDeadline)
result[2] = routeLimit - 1
result[3] = routeDeadline
return result
end
local rDead = tonumber(routeInfo[4]) -- expired time
local rCount = tonumber(routeInfo[2])
local sCount = tonumber(staticCount)
if rDead < now then -- 過期
if tonumber(staticCount) < staticLimit then
result[1] = staticLimit - redis.call('HINCRBY', staticKey, "Count", 1)
else
result[1] = -1
end
redis.call('HSET', routeKey, "Count", 1, "Deadline", routeDeadline)
result[2] = routeLimit - 1
result[3] = routeDeadline
return result
end
if sCount < staticLimit then
result[1] = staticLimit - redis.call('HINCRBY', staticKey, "Count", 1)
else
result[1] = -1
end
if rCount < routeLimit then
result[2] = redis.call('HINCRBY', routeKey, "Count", 1)
else
result[2] = -1
end
result[3] = rDead
return result
`