-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add redis and redis-cluster in limit-req (#10874)
- Loading branch information
1 parent
72f6364
commit 1439b13
Showing
11 changed files
with
1,524 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
-- | ||
-- 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 redis_cluster = require("apisix.utils.rediscluster") | ||
local setmetatable = setmetatable | ||
local util = require("apisix.plugins.limit-req.util") | ||
|
||
local _M = {version = 0.1} | ||
|
||
|
||
local mt = { | ||
__index = _M | ||
} | ||
|
||
|
||
function _M.new(plugin_name, conf, rate, burst) | ||
local red_cli, err = redis_cluster.new(conf, "plugin-limit-req-redis-cluster-slot-lock") | ||
if not red_cli then | ||
return nil, err | ||
end | ||
local self = { | ||
conf = conf, | ||
plugin_name = plugin_name, | ||
burst = burst * 1000, | ||
rate = rate * 1000, | ||
red_cli = red_cli, | ||
} | ||
return setmetatable(self, mt) | ||
end | ||
|
||
|
||
function _M.incoming(self, key, commit) | ||
return util.incoming(self, self.red_cli, key, commit) | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
-- | ||
-- 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 redis = require("apisix.utils.redis") | ||
local setmetatable = setmetatable | ||
local util = require("apisix.plugins.limit-req.util") | ||
|
||
local setmetatable = setmetatable | ||
|
||
|
||
local _M = {version = 0.1} | ||
|
||
|
||
local mt = { | ||
__index = _M | ||
} | ||
|
||
|
||
function _M.new(plugin_name, conf, rate, burst) | ||
local self = { | ||
conf = conf, | ||
plugin_name = plugin_name, | ||
burst = burst * 1000, | ||
rate = rate * 1000, | ||
} | ||
return setmetatable(self, mt) | ||
end | ||
|
||
|
||
function _M.incoming(self, key, commit) | ||
local conf = self.conf | ||
local red, err = redis.new(conf) | ||
if not red then | ||
return red, err | ||
end | ||
|
||
return util.incoming(self, red, key, commit) | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
-- | ||
-- 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 math = require "math" | ||
local abs = math.abs | ||
local max = math.max | ||
local ngx_now = ngx.now | ||
local ngx_null = ngx.null | ||
local tonumber = tonumber | ||
|
||
|
||
local _M = {version = 0.1} | ||
|
||
|
||
-- the "commit" argument controls whether should we record the event in shm. | ||
function _M.incoming(self, red, key, commit) | ||
local rate = self.rate | ||
local now = ngx_now() * 1000 | ||
|
||
key = "limit_req" .. ":" .. key | ||
local excess_key = key .. "excess" | ||
local last_key = key .. "last" | ||
|
||
local excess, err = red:get(excess_key) | ||
if err then | ||
return nil, err | ||
end | ||
local last, err = red:get(last_key) | ||
if err then | ||
return nil, err | ||
end | ||
|
||
if excess ~= ngx_null and last ~= ngx_null then | ||
excess = tonumber(excess) | ||
last = tonumber(last) | ||
local elapsed = now - last | ||
excess = max(excess - rate * abs(elapsed) / 1000 + 1000, 0) | ||
|
||
if excess > self.burst then | ||
return nil, "rejected" | ||
end | ||
else | ||
excess = 0 | ||
end | ||
|
||
if commit then | ||
local ok | ||
local err | ||
ok, err = red:set(excess_key, excess) | ||
if not ok then | ||
return nil, err | ||
end | ||
|
||
ok, err = red:set(last_key, now) | ||
if not ok then | ||
return nil, err | ||
end | ||
end | ||
|
||
-- return the delay in seconds, as well as excess | ||
return excess / rate, excess / 1000 | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.