-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: limit count plugin support X-RateLimit-Reset #8578
Changes from 12 commits
6fc3cd0
2ab6f24
613ece6
c332ab6
6813e4f
eae4304
bd32ca8
46285a6
ff6b2fd
34646d3
0feba96
60d0a9a
fa40f8d
e548299
cae2e6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,18 +14,20 @@ | |
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local limit_local_new = require("resty.limit.count").new | ||
local core = require("apisix.core") | ||
local apisix_plugin = require("apisix.plugin") | ||
local tab_insert = table.insert | ||
local ipairs = ipairs | ||
local pairs = pairs | ||
|
||
|
||
local plugin_name = "limit-count" | ||
local limit_redis_cluster_new | ||
local limit_redis_new | ||
local limit_local_new | ||
do | ||
local local_src = "apisix.plugins.limit-count.limit-count-local" | ||
limit_local_new = require(local_src).new | ||
|
||
local redis_src = "apisix.plugins.limit-count.limit-count-redis" | ||
limit_redis_new = require(redis_src).new | ||
|
||
|
@@ -39,7 +41,6 @@ local group_conf_lru = core.lrucache.new({ | |
type = 'plugin', | ||
}) | ||
|
||
|
||
local policy_to_additional_properties = { | ||
redis = { | ||
properties = { | ||
|
@@ -242,7 +243,6 @@ local function gen_limit_obj(conf, ctx) | |
return core.lrucache.plugin_ctx(lrucache, ctx, extra_key, create_limit_obj, conf) | ||
end | ||
|
||
|
||
function _M.rate_limit(conf, ctx) | ||
core.log.info("ver: ", ctx.conf_version) | ||
|
||
|
@@ -283,10 +283,17 @@ function _M.rate_limit(conf, ctx) | |
key = gen_limit_key(conf, ctx, key) | ||
core.log.info("limit key: ", key) | ||
|
||
local delay, remaining = lim:incoming(key, true) | ||
local delay, remaining, reset = lim:incoming(key, true, conf) | ||
if not delay then | ||
local err = remaining | ||
if err == "rejected" then | ||
-- show count limit header when rejected | ||
if conf.show_limit_quota_header then | ||
core.response.set_header("X-RateLimit-Limit", conf.count, | ||
"X-RateLimit-Remaining", 0, | ||
"X-RateLimit-Reset", reset) | ||
end | ||
|
||
if conf.rejected_msg then | ||
return conf.rejected_code, { error_msg = conf.rejected_msg } | ||
end | ||
|
@@ -302,7 +309,8 @@ function _M.rate_limit(conf, ctx) | |
|
||
if conf.show_limit_quota_header then | ||
core.response.set_header("X-RateLimit-Limit", conf.count, | ||
"X-RateLimit-Remaining", remaining) | ||
"X-RateLimit-Remaining", remaining, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why change 4-spaces indentation to 8-spaces here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
"X-RateLimit-Reset", reset) | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
-- | ||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||
-- contributor license agreements. See the NOTICE file distributed with | ||
-- this work for additional information regarding copyright ownership. | ||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
-- (the "License"); you may not use this file except in compliance with | ||
-- the License. You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local limit_local_new = require("resty.limit.count").new | ||
local ngx = ngx | ||
local ngx_time = ngx.time | ||
local assert = assert | ||
local setmetatable = setmetatable | ||
|
||
local _M = {} | ||
|
||
local mt = { | ||
__index = _M | ||
} | ||
|
||
local function set_endtime(self, key, time_window) | ||
-- set an end time | ||
local end_time = ngx_time() + time_window | ||
-- save to dict by key | ||
self.dict:set(key, end_time, time_window) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to check the err returned from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
local reset = time_window | ||
return reset | ||
end | ||
|
||
local function read_reset(self, key) | ||
-- read from dict | ||
local end_time = (self.dict:get(key) or 0) | ||
local reset = end_time - ngx_time() | ||
if reset < 0 then | ||
reset = 0 | ||
end | ||
return reset | ||
end | ||
|
||
function _M.new(plugin_name, limit, window, conf) | ||
assert(limit > 0 and window > 0) | ||
|
||
local self = { | ||
limit_count = limit_local_new(plugin_name, limit, window, conf), | ||
dict = ngx.shared["plugin-limit-count-reset-header"] | ||
} | ||
|
||
return setmetatable(self, mt) | ||
end | ||
|
||
function _M.incoming(self, key, commit, conf) | ||
local delay, remaining = self.limit_count:incoming(key, commit) | ||
local reset = 0 | ||
if not delay then | ||
return delay, remaining, reset | ||
end | ||
|
||
if remaining == conf.count - 1 then | ||
reset = set_endtime(self, key, conf.time_window) | ||
else | ||
reset = read_reset(self, key) | ||
end | ||
|
||
return delay, remaining, reset | ||
end | ||
|
||
return _M |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,32 +30,17 @@ local mt = { | |||||
|
||||||
|
||||||
local script = core.string.compress_script([=[ | ||||||
if redis.call('ttl', KEYS[1]) < 0 then | ||||||
local ttl = redis.call('ttl', KEYS[1]) | ||||||
if ttl < 0 then | ||||||
redis.call('set', KEYS[1], ARGV[1] - 1, 'EX', ARGV[2]) | ||||||
return ARGV[1] - 1 | ||||||
return {ARGV[1] - 1, ARGV[2]} | ||||||
end | ||||||
return redis.call('incrby', KEYS[1], -1) | ||||||
return {redis.call('incrby', KEYS[1], -1), ttl} | ||||||
]=]) | ||||||
|
||||||
|
||||||
function _M.new(plugin_name, limit, window, conf) | ||||||
assert(limit > 0 and window > 0) | ||||||
|
||||||
local self = { | ||||||
limit = limit, | ||||||
window = window, | ||||||
conf = conf, | ||||||
plugin_name = plugin_name, | ||||||
} | ||||||
return setmetatable(self, mt) | ||||||
end | ||||||
|
||||||
|
||||||
function _M.incoming(self, key) | ||||||
local conf = self.conf | ||||||
local function redis_cli(conf) | ||||||
local red = redis_new() | ||||||
local timeout = conf.redis_timeout or 1000 -- 1sec | ||||||
core.log.info("ttl key: ", key, " timeout: ", timeout) | ||||||
|
||||||
red:set_timeouts(timeout, timeout, timeout) | ||||||
|
||||||
|
@@ -85,27 +70,52 @@ function _M.incoming(self, key) | |||||
-- core.log.info(" err: ", err) | ||||||
return nil, err | ||||||
end | ||||||
return red, nil | ||||||
end | ||||||
|
||||||
function _M.new(plugin_name, limit, window, conf) | ||||||
assert(limit > 0 and window > 0) | ||||||
|
||||||
local self = { | ||||||
limit = limit, | ||||||
window = window, | ||||||
conf = conf, | ||||||
plugin_name = plugin_name, | ||||||
} | ||||||
return setmetatable(self, mt) | ||||||
end | ||||||
|
||||||
function _M.incoming(self, key) | ||||||
local conf = self.conf | ||||||
local red, err = redis_cli(conf) | ||||||
if err then | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||
return red, err | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please follow the code style: https://github.com/apache/apisix/blob/master/CODE_STYLE.md#error-handling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||
end | ||||||
|
||||||
local limit = self.limit | ||||||
local window = self.window | ||||||
local remaining | ||||||
local res | ||||||
key = self.plugin_name .. tostring(key) | ||||||
|
||||||
remaining, err = red:eval(script, 1, key, limit, window) | ||||||
local ttl = 0 | ||||||
res, err = red:eval(script, 1, key, limit, window) | ||||||
|
||||||
if err then | ||||||
return nil, err | ||||||
return nil, err, ttl | ||||||
end | ||||||
|
||||||
local remaining = res[1] | ||||||
ttl = res[2] | ||||||
|
||||||
local ok, err = red:set_keepalive(10000, 100) | ||||||
if not ok then | ||||||
return nil, err | ||||||
return nil, err, ttl | ||||||
end | ||||||
|
||||||
if remaining < 0 then | ||||||
return nil, "rejected" | ||||||
return nil, "rejected", ttl | ||||||
end | ||||||
return 0, remaining | ||||||
return 0, remaining, ttl | ||||||
end | ||||||
|
||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed