membphis commented on a change in pull request #2340: URL: https://github.com/apache/apisix/pull/2340#discussion_r501552477
########## File path: apisix/plugins/limit-count/limit-count-redis-cluster.lua ########## @@ -0,0 +1,138 @@ +-- +-- 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 core = require("apisix.core") +local resty_lock = require("resty.lock") +local assert = assert +local setmetatable = setmetatable +local tostring = tostring + + +local _M = {version = 0.3} + + +local mt = { + __index = _M +} + +-- https://github.com/steve0511/resty-redis-cluster +local function new_redis_cluster(conf) + local config = { + dict_name = "redis_cluster_slot_locks", --shared dictionary name for locks + name = "apisix-rediscluster", --rediscluster name + keepalive_timeout = 60000, --redis connection pool idle timeout + keepalive_cons = 1000, --redis connection pool size + connect_timeout = 1000, --timeout while connecting + max_redirection = 5, --maximum retry attempts for redirection + max_connection_attempts = 1, --maximum retry attempts for connection + read_timeout = conf.redis_timeout or 1000, + enable_slave_read = true, + serv_list = {}, + } + + for key, value in ipairs(conf.redis_serv_list) do + if value['redis_host'] and value['redis_port'] then + config.serv_list[key] = {ip = value['redis_host'], port = value['redis_port']} + end + end + + if conf.redis_password then + config.auth = conf.redis_password --set password while setting auth + end + + local redis_cluster = require "resty.rediscluster" + local red_c = redis_cluster:new(config) + + return red_c +end + + +function _M.new(plugin_name, limit, window, conf) + assert(limit > 0 and window > 0) + + _M.red_c = new_redis_cluster(conf) + + 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 = _M.red_c + core.log.info("ttl key: ", key, " timeout: ", conf.redis_timeout or 1000) Review comment: use JSONSchema to set the default value ########## File path: apisix/plugins/limit-count/limit-count-redis-cluster.lua ########## @@ -0,0 +1,138 @@ +-- +-- 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 core = require("apisix.core") +local resty_lock = require("resty.lock") +local assert = assert +local setmetatable = setmetatable +local tostring = tostring + + +local _M = {version = 0.3} + + +local mt = { + __index = _M +} + +-- https://github.com/steve0511/resty-redis-cluster +local function new_redis_cluster(conf) + local config = { + dict_name = "redis_cluster_slot_locks", --shared dictionary name for locks + name = "apisix-rediscluster", --rediscluster name + keepalive_timeout = 60000, --redis connection pool idle timeout + keepalive_cons = 1000, --redis connection pool size + connect_timeout = 1000, --timeout while connecting + max_redirection = 5, --maximum retry attempts for redirection + max_connection_attempts = 1, --maximum retry attempts for connection + read_timeout = conf.redis_timeout or 1000, + enable_slave_read = true, + serv_list = {}, + } + + for key, value in ipairs(conf.redis_serv_list) do + if value['redis_host'] and value['redis_port'] then + config.serv_list[key] = {ip = value['redis_host'], port = value['redis_port']} + end + end + + if conf.redis_password then + config.auth = conf.redis_password --set password while setting auth + end + + local redis_cluster = require "resty.rediscluster" + local red_c = redis_cluster:new(config) + + return red_c +end + + +function _M.new(plugin_name, limit, window, conf) + assert(limit > 0 and window > 0) + + _M.red_c = new_redis_cluster(conf) + + 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 = _M.red_c + core.log.info("ttl key: ", key, " timeout: ", conf.redis_timeout or 1000) + + ngx.log(ngx.ERR, ">>>>>>>>>>>>>>", key, require("cjson.safe").encode(self)) Review comment: should use `core.log` always. ########## File path: doc/zh-cn/plugins/limit-count.md ########## @@ -26,17 +26,18 @@ ## 参数 -| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 | -| -------------- | ------- | ------------ | ------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| count | integer | 必须 | | [0,...] | 指定时间窗口内的请求数量阈值 | -| time_window | integer | 必须 | | [0,...] | 时间窗口的大小(以秒为单位),超过这个时间就会重置 | -| key | string | 必须 | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for"] | 用来做请求计数的依据 | -| rejected_code | integer | 可选 | 503 | [200,600] | 当请求超过阈值被拒绝时,返回的 HTTP 状态码 | -| policy | string | 可选 | "local" | ["local", "redis"] | 用于检索和增加限制的速率限制策略。可选的值有:`local`(计数器被以内存方式保存在节点本地,默认选项) 和 `redis`(计数器保存在 Redis 服务节点上,从而可以跨节点共享结果,通常用它来完成全局限速) | -| redis_host | string | `redis` 必须 | | | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的地址。 | -| redis_port | integer | 可选 | 6379 | [1,...] | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的端口 | -| redis_password | string | 可选 | | | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的密码。 | -| redis_timeout | integer | 可选 | 1000 | [1,...] | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点以毫秒为单位的超时时间 | +| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 | +| --------------- | -------- | ------------ | ------- | ------------------------------------------------------------ | ------------------------------------------------------------ | +| count | integer | 必须 | | [0,...] | 指定时间窗口内的请求数量阈值 | +| time_window | integer | 必须 | | [0,...] | 时间窗口的大小(以秒为单位),超过这个时间就会重置 | +| key | string | 必须 | | ["remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for"] | 用来做请求计数的依据 | +| rejected_code | integer | 可选 | 503 | [200,600] | 当请求超过阈值被拒绝时,返回的 HTTP 状态码 | +| policy | string | 可选 | "local" | ["local", "redis", "redis-cluster"] | 用于检索和增加限制的速率限制策略。可选的值有:`local`(计数器被以内存方式保存在节点本地,默认选项) 和 `redis`(计数器保存在 Redis 服务节点上,从而可以跨节点共享结果,通常用它来完成全局限速);以及`redis-cluster`,跟redis功能一样,只是使用redis集群方式。 | Review comment: we should only change the doc we need to update. small PR is easy for reviewing. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
