-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrate.lua
296 lines (213 loc) · 5.78 KB
/
rate.lua
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
-- Copyright (C) UPYUN, Inc.
-- limit request rate using the token bucket method:
-- https://en.wikipedia.org/wiki/Token_bucket
local ffi = require "ffi"
local math = require "math"
local lock = require "resty.lock"
local ffi_cast = ffi.cast
local ffi_str = ffi.string
local type = type
local assert = assert
local ngx_now = ngx.now
local floor = math.floor
local ngx_shared = ngx.shared
local setmetatable = setmetatable
ffi.cdef[[
struct lua_resty_limit_rate_rec {
int64_t avail;
uint64_t last; /* time in milliseconds */
};
]]
local const_rec_ptr_type = ffi.typeof("const struct lua_resty_limit_rate_rec*")
local rec_size = ffi.sizeof("struct lua_resty_limit_rate_rec")
local rec_cdata = ffi.new("struct lua_resty_limit_rate_rec")
local _M = {
_VERSION = "0.1",
}
local mt = {
__index = _M
}
local function acquire_lock(self, key)
if not self.lock_enable then
return true
end
local lock, err = lock:new(self.locks_shdict_name)
if not lock then
return nil, err
end
self.lock = lock
return lock:lock(key)
end
local function release_lock(self)
if not self.lock_enable then
return true
end
local lock = self.lock
return lock:unlock()
end
local function update(self, key, avail, last)
local dict = self.dict
rec_cdata.avail = avail
rec_cdata.last = last
dict:set(key, ffi_str(rec_cdata, rec_size))
-- ngx.log(ngx.ERR, "key = ", key, " avail = ", avail, " last = ", last)
end
local function adjust(self, key, now)
local dict = self.dict
local res = {
last = now,
avail = self.capacity
}
local v = dict:get(key)
if v then
if type(v) ~= "string" or #v ~= rec_size then
return nil, "shdict abused by other users"
end
local rec = ffi_cast(const_rec_ptr_type, v)
res.last = tonumber(rec.last)
res.avail = tonumber(rec.avail)
end
local tick = floor((now - res.last) / self.interval)
res.last = res.last + tick * self.interval
if res.avail >= self.capacity then
return res
end
res.avail = res.avail + tick * self.quantum
if res.avail > self.capacity then
res.avail = self.capacity
end
return res
end
function _M.new(dict_name, interval, capacity, quantum, max_wait, opts)
local dict = ngx_shared[dict_name]
if not dict then
return nil, "shared dict not found"
end
if not quantum then
quantum = 1
end
assert(interval > 0 and capacity >= 0 and quantum > 0)
if not opts then
opts = {}
end
local lock_enable = opts.lock_enable or false
local locks_shdict_name = opts.locks_shdict_name or "locks"
local self = {
dict = dict,
interval = interval,
capacity = capacity,
quantum = quantum,
max_wait = max_wait,
lock_enable = lock_enable,
locks_shdict_name = locks_shdict_name,
}
return setmetatable(self, mt)
end
function _M.set_max_wait(self, max_wait)
self.max_wait = max_wait
end
function _M.take(self, key, count, commit, fake_now)
assert(key and count > 0)
local now = ngx_now() * 1000
-- just for testing
if type(fake_now) == "number" then
now = fake_now
end
local res, err = acquire_lock(self, key)
if not res then
return nil, err
end
local res, err = adjust(self, key, now)
if not res then
release_lock(self)
return nil, err
end
local last = res.last
local avail = res.avail
avail = avail - count
if avail >= 0 then
if commit then
update(self, key, avail, last)
end
release_lock(self)
return 0, avail
end
local quantum = self.quantum
local tick = floor((-avail + quantum - 1) / quantum)
local wait_time = tick * self.interval - (now - last)
local max_wait = self.max_wait
if type(max_wait) == "number" and wait_time > max_wait then
if commit then
update(self, key, avail + count, last)
end
release_lock(self)
return nil, "rejected"
end
if commit then
update(self, key, avail, last)
end
release_lock(self)
return wait_time / 1000, avail
end
function _M.take_available(self, key, count, fake_now)
if type(key) ~= "string" or count <= 0 then
return 0
end
local now = ngx_now() * 1000
-- just for testing
if type(fake_now) == "number" then
now = fake_now
end
local res, err = acquire_lock(self, key)
if not res then
return nil, err
end
local res, err = adjust(self, key, now)
if not res then
release_lock(self)
return nil, err
end
local last = res.last
local avail = res.avail
if avail <= 0 then
update(self, key, avail, last)
release_lock(self)
return 0
end
if count > avail then
count = avail
end
avail = avail - count
update(self, key, avail, last)
release_lock(self)
return count
end
function _M.incoming(self, key, commit)
return self:take(key, 1, commit)
end
function _M.uncommit(self, key)
assert(key)
local res, err = acquire_lock(self, key)
if not res then
return nil, err
end
local dict = self.dict
local v = dict:get(key)
if not v then
release_lock(self)
return nil, "not found"
end
if type(v) ~= "string" or #v ~= rec_size then
release_lock(self)
return nil, "shdict abused by other users"
end
local rec = ffi_cast(const_rec_ptr_type, v)
local avail = tonumber(rec.avail) + 1
if avail > self.capacity then
avail = self.capacity
end
update(self, key, avail, rec.last)
release_lock(self)
return true
end
return _M