-
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
feature: plugin limit-count
support to use redis
cluster
#2406
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
90d2227
feature: Support for using redis cluster in limit-count plugin.
liuhengloveyou faa84e6
CI: revert to original version.
membphis d97a684
change: change the shdict name to `plugin-limit-count-redis-cluster-s…
membphis e077315
test: added test case.
membphis 1eaee33
CI: revert.
membphis 3a038d2
test: code style and add more test case.
membphis 4dd73bd
test: use `5` sec as default timeout.
membphis 0823423
CI: 3 nodes for redis cluster.
membphis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
134 changes: 134 additions & 0 deletions
134
apisix/plugins/limit-count/limit-count-redis-cluster.lua
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,134 @@ | ||
-- | ||
-- 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 rediscluster = require("resty.rediscluster") | ||
local core = require("apisix.core") | ||
local resty_lock = require("resty.lock") | ||
local setmetatable = setmetatable | ||
local tostring = tostring | ||
local ipairs = ipairs | ||
|
||
local _M = {} | ||
|
||
|
||
local mt = { | ||
__index = _M | ||
} | ||
|
||
|
||
local function new_redis_cluster(conf) | ||
local config = { | ||
name = "apisix-redis-cluster", | ||
serv_list = {}, | ||
read_timeout = conf.redis_timeout, | ||
auth = conf.redis_password, | ||
dict_name = "plugin-limit-count-redis-cluster-slot-lock", | ||
} | ||
|
||
for i, conf_item in ipairs(conf.redis_cluster_nodes) do | ||
local host, port, err = core.utils.parse_addr(conf_item) | ||
if err then | ||
return nil, "failed to parse address: " .. conf_item | ||
.. " err: " .. err | ||
end | ||
|
||
config.serv_list[i] = {ip = host, port = port} | ||
end | ||
|
||
local red_cli, err = rediscluster:new(config) | ||
if not red_cli then | ||
return nil, "failed to new redis cluster: " .. err | ||
end | ||
|
||
return red_cli | ||
end | ||
|
||
|
||
function _M.new(plugin_name, limit, window, conf) | ||
local red_cli, err = new_redis_cluster(conf) | ||
if not red_cli then | ||
return nil, err | ||
end | ||
|
||
local self = { | ||
limit = limit, window = window, conf = conf, | ||
plugin_name = plugin_name, red_cli =red_cli | ||
} | ||
|
||
return setmetatable(self, mt) | ||
end | ||
|
||
|
||
function _M.incoming(self, key) | ||
local red = self.red_cli | ||
local limit = self.limit | ||
local window = self.window | ||
local remaining | ||
key = self.plugin_name .. tostring(key) | ||
|
||
local ret, err = red:ttl(key) | ||
if not ret then | ||
return false, "failed to get redis `" .. key .."` ttl: " .. err | ||
end | ||
|
||
core.log.info("ttl key: ", key, " ret: ", ret, " err: ", err) | ||
if ret < 0 then | ||
local lock, err = resty_lock:new("plugin-limit-count") | ||
if not lock then | ||
return false, "failed to create lock: " .. err | ||
end | ||
|
||
local elapsed, err = lock:lock(key) | ||
if not elapsed then | ||
return false, "failed to acquire the lock: " .. err | ||
end | ||
|
||
ret = red:ttl(key) | ||
nic-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ret < 0 then | ||
local ok, err = lock:unlock() | ||
if not ok then | ||
return false, "failed to unlock: " .. err | ||
end | ||
|
||
ret, err = red:set(key, limit -1, "EX", window) | ||
if not ret then | ||
return nil, err | ||
end | ||
|
||
return 0, limit -1 | ||
membphis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
local ok, err = lock:unlock() | ||
if not ok then | ||
return false, "failed to unlock: " .. err | ||
end | ||
end | ||
|
||
remaining, err = red:incrby(key, -1) | ||
if not remaining then | ||
return nil, err | ||
end | ||
|
||
if remaining < 0 then | ||
return nil, "rejected" | ||
end | ||
|
||
return 0, remaining | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think copy-paste is good
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.
in
limit-count-redis
, the main process ofincoming
isconnect
->process
->keepalive
.but in
limit-count-redis-cluster
, the main process ofincoming
is only theprocess
. they were different.I think it is acceptable for those copied code.
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.
@moonming I think we can merge this PR now. please take a look at this PR.