membphis commented on a change in pull request #2340: URL: https://github.com/apache/apisix/pull/2340#discussion_r502279935
########## File path: apisix/plugins/limit-count/limit-count-redis-cluster.lua ########## @@ -0,0 +1,139 @@ +-- +-- 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 require = require +local ipairs = ipairs + + +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) Review comment: I think it may fail, need to capture the error message ########## File path: apisix/plugins/limit-count/limit-count-redis-cluster.lua ########## @@ -0,0 +1,139 @@ +-- +-- 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 require = require +local ipairs = ipairs + + +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 Review comment: can we use the default value here? ########## File path: doc/plugins/limit-count.md ########## @@ -109,6 +110,41 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335 }' ``` +If using redis-cluster: + +```shell +curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' +{ + "uri": "/index.html", + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr", + "policy": "redis-cluster", + "redis_serv_list": [ + { Review comment: how about this style? it seems simpler: ``` redis_serv_list: ["127.0.0.1:6373", "127.0.0.1:6374"] ``` ########## File path: apisix/plugins/limit-count/limit-count-redis-cluster.lua ########## @@ -0,0 +1,139 @@ +-- +-- 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 require = require +local ipairs = ipairs + + +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" Review comment: local cache ########## File path: doc/plugins/limit-count.md ########## @@ -109,6 +110,41 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335 }' ``` +If using redis-cluster: + +```shell +curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' +{ + "uri": "/index.html", + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr", + "policy": "redis-cluster", + "redis_serv_list": [ + { Review comment: avoid using the `tab` key, we always use spaces. ---------------------------------------------------------------- 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]
