shreemaan-abhishek commented on code in PR #10866:
URL: https://github.com/apache/apisix/pull/10866#discussion_r1469264127
##########
apisix/plugins/limit-conn/limit-conn-redis-cluster.lua:
##########
@@ -0,0 +1,158 @@
+local rediscluster = require("resty.rediscluster")
+local core = require("apisix.core")
+local assert = assert
+local setmetatable = setmetatable
+local math = require "math"
+local floor = math.floor
+local ipairs = ipairs
+local ngx_timer_at = ngx.timer.at
+
+local _M = {version = 0.1}
+
+
+local mt = {
+ __index = _M
+}
+
+
+local function new_redis_cluster(conf)
Review Comment:
this code is repeated in `limit-count-redis-cluster` we should seperate this
to a different file like `redis-util.lua`
##########
apisix/plugins/limit-conn.lua:
##########
@@ -19,18 +19,95 @@ local limit_conn = require("apisix.plugins.limit-conn.init")
local plugin_name = "limit-conn"
+
+local counter_type_to_additional_properties = {
+ redis = {
+ properties = {
+ redis_host = {
+ type = "string", minLength = 2
+ },
+ redis_port = {
+ type = "integer", minimum = 1, default = 6379,
+ },
+ redis_username = {
+ type = "string", minLength = 1,
+ },
+ redis_password = {
+ type = "string", minLength = 0,
+ },
+ redis_prefix = {
+ type = "string", minLength = 0, default = "limit_conn",
pattern = "^[0-9a-zA-Z|_]+$"
+ },
+ redis_database = {
+ type = "integer", minimum = 0, default = 0,
Review Comment:
why default to zero?
##########
apisix/plugins/limit-conn/limit-conn-redis-cluster.lua:
##########
@@ -0,0 +1,158 @@
+local rediscluster = require("resty.rediscluster")
+local core = require("apisix.core")
+local assert = assert
+local setmetatable = setmetatable
+local math = require "math"
+local floor = math.floor
+local ipairs = ipairs
+local ngx_timer_at = ngx.timer.at
+
+local _M = {version = 0.1}
+
+
+local mt = {
+ __index = _M
+}
+
+
+local function new_redis_cluster(conf)
+ local config = {
+ name = conf.redis_cluster_name,
+ serv_list = {},
+ read_timeout = conf.redis_timeout,
+ auth = conf.redis_password,
+ dict_name = "plugin-limit-conn-redis-cluster-slot-lock",
+ connect_opts = {
+ ssl = conf.redis_cluster_ssl,
+ ssl_verify = conf.redis_cluster_ssl_verify,
+ }
+ }
+
+ 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, conf, max, burst, default_conn_delay)
+ local self = {
+ conf = conf,
+ plugin_name = plugin_name,
+ burst = burst,
+ max = max + 0, -- just to ensure the param is good
+ unit_delay = default_conn_delay,
+ }
+ return setmetatable(self, mt)
+end
+
+
+function _M.incoming(self, key, commit)
+ local max = self.max
+
+ -- init redis
+ local conf = self.conf
+ local red, err = new_redis_cluster(conf)
Review Comment:
this means create a new cluster for every request, please move this to
`_M.new()`
##########
apisix/plugins/limit-conn.lua:
##########
@@ -19,18 +19,95 @@ local limit_conn = require("apisix.plugins.limit-conn.init")
local plugin_name = "limit-conn"
+
+local counter_type_to_additional_properties = {
+ redis = {
+ properties = {
+ redis_host = {
+ type = "string", minLength = 2
+ },
+ redis_port = {
+ type = "integer", minimum = 1, default = 6379,
+ },
+ redis_username = {
+ type = "string", minLength = 1,
+ },
+ redis_password = {
+ type = "string", minLength = 0,
+ },
+ redis_prefix = {
+ type = "string", minLength = 0, default = "limit_conn",
pattern = "^[0-9a-zA-Z|_]+$"
+ },
+ redis_database = {
+ type = "integer", minimum = 0, default = 0,
+ },
+ redis_timeout = {
+ type = "integer", minimum = 1, default = 1000,
+ },
+ redis_ssl = {
+ type = "boolean", default = false,
+ },
+ redis_ssl_verify = {
+ type = "boolean", default = false,
+ },
+ },
+ required = {"redis_host"},
+ },
+ ["shared-dict"] = {
+ properties = {
+ },
+ },
+ ["redis-cluster"] = {
+ properties = {
+ redis_cluster_nodes = {
+ type = "array",
+ minItems = 2,
+ items = {
+ type = "string", minLength = 2, maxLength = 100
+ },
+ },
+ redis_password = {
+ type = "string", minLength = 0,
+ },
+ redis_prefix = {
+ type = "string", minLength = 0, default = "limit_conn",
pattern = "^[0-9a-zA-Z|_]+$"
+ },
+ redis_timeout = {
+ type = "integer", minimum = 1, default = 1000,
+ },
+ redis_cluster_name = {
+ type = "string",
+ },
+ redis_cluster_ssl = {
+ type = "boolean", default = false,
+ },
+ redis_cluster_ssl_verify = {
+ type = "boolean", default = false,
+ },
+ },
+ required = {"redis_cluster_nodes", "redis_cluster_name"},
+ },
+}
local schema = {
type = "object",
properties = {
- conn = {type = "integer", exclusiveMinimum = 0},
+ conn = {type = "integer", exclusiveMinimum = 0}, --
limit.conn max
burst = {type = "integer", minimum = 0},
default_conn_delay = {type = "number", exclusiveMinimum = 0},
only_use_default_delay = {type = "boolean", default = false},
- key = {type = "string"},
- key_type = {type = "string",
+ key = {
+ type = "string"
+ },
+ key_type = {
+ type = "string",
enum = {"var", "var_combination"},
default = "var",
},
+ counter_type = {
Review Comment:
please use `policy` instead of `counter_type` (to be in-line with the
implementation)
https://github.com/apache/apisix/blob/0bea3dbf6b1b868ba35acdb2fd0205f2a352479d/apisix/plugins/limit-count/init.lua#L118
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]