You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/02/09 15:03:59 UTC

[apisix] branch master updated: feat: add select database for Redis related functions (#3559)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 93a0d77  feat: add select database for Redis related functions (#3559)
93a0d77 is described below

commit 93a0d77ef1b014a13f7288033d8a55d5785804cc
Author: wettper <we...@web-lovers.com>
AuthorDate: Tue Feb 9 23:03:50 2021 +0800

    feat: add select database for Redis related functions (#3559)
---
 apisix/plugins/limit-count.lua                   |   3 +
 apisix/plugins/limit-count/limit-count-redis.lua |   8 +
 doc/plugins/limit-count.md                       |   2 +
 doc/zh-cn/plugins/limit-count.md                 |   2 +
 t/plugin/limit-count-redis2.t                    | 190 +++++++++++++++++++++++
 5 files changed, 205 insertions(+)

diff --git a/apisix/plugins/limit-count.lua b/apisix/plugins/limit-count.lua
index 57c3049..b730c5b 100644
--- a/apisix/plugins/limit-count.lua
+++ b/apisix/plugins/limit-count.lua
@@ -76,6 +76,9 @@ local schema = {
                         redis_password = {
                             type = "string", minLength = 0,
                         },
+                        redis_database = {
+                            type = "integer", minimum = 0, default = 0,
+                        },
                         redis_timeout = {
                             type = "integer", minimum = 1, default = 1000,
                         },
diff --git a/apisix/plugins/limit-count/limit-count-redis.lua b/apisix/plugins/limit-count/limit-count-redis.lua
index 4dcee71..2462143 100644
--- a/apisix/plugins/limit-count/limit-count-redis.lua
+++ b/apisix/plugins/limit-count/limit-count-redis.lua
@@ -61,6 +61,14 @@ function _M.incoming(self, key)
                 return nil, err
             end
         end
+
+        -- select db
+        if conf.redis_database ~= 0 then
+            local ok, err = red:select(conf.redis_database)
+            if not ok then
+                return false, "failed to change redis db, err: " .. err
+            end
+        end
     elseif err then
         -- core.log.info(" err: ", err)
         return nil, err
diff --git a/doc/plugins/limit-count.md b/doc/plugins/limit-count.md
index 9c2614f..19a3e62 100644
--- a/doc/plugins/limit-count.md
+++ b/doc/plugins/limit-count.md
@@ -43,6 +43,7 @@ Limit request rate by a fixed number of requests in a given time window.
 | redis_host     | string  | required for `redis` |         |                                                                          | When using the `redis` policy, this property specifies the address of the Redis server.                                                                                                                                                                                                                     |
 | redis_port     | integer | optional             | 6379    | [1,...]                                                                  | When using the `redis` policy, this property specifies the port of the Redis server.                                                                                                                                                                                                                        |
 | redis_password | string  | optional             |         |                                                                          | When using the `redis` policy, this property specifies the password of the Redis server.                                                                                                                                                                                                                    |
+| redis_database | integer | optional             | 0       | redis_database >= 0                                                      | When using the `redis` policy, this property specifies the datatabase you selected of the Redis server, and only for non Redis cluster mode (single instance mode or Redis public cloud service that provides single entry).                                                                                |
 | redis_timeout  | integer | optional             | 1000    | [1,...]                                                                  | When using the `redis` policy, this property specifies the timeout in milliseconds of any command submitted to the Redis server.                                                                                                                                                                            |
 | redis_cluster_nodes | array | optional |         |                                                              | When using `redis-cluster` policy,This property is a list of addresses of Redis cluster service nodes. |
 
@@ -97,6 +98,7 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335
             "redis_host": "127.0.0.1",
             "redis_port": 6379,
             "redis_password": "password",
+            "redis_database": 1,
             "redis_timeout": 1001
         }
     },
diff --git a/doc/zh-cn/plugins/limit-count.md b/doc/zh-cn/plugins/limit-count.md
index dd772fe..7428b32 100644
--- a/doc/zh-cn/plugins/limit-count.md
+++ b/doc/zh-cn/plugins/limit-count.md
@@ -44,6 +44,7 @@
 | redis_host      | string   | `redis` 必须 |         |                                                              | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的地址。   |
 | redis_port      | integer  | 可选         | 6379    | [1,...]                                                      | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的端口     |
 | redis_password  | string   | 可选         |         |                                                              | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的密码。   |
+| redis_database  | integer  | 可选         | 0       | redis_database >= 0                                          | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点中使用的 database,并且只针对非 Redis 集群模式(单实例模式或者提供单入口的Redis公有云服务)生效。    |
 | redis_timeout   | integer  | 可选         | 1000    | [1,...]                                                      | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点以毫秒为单位的超时时间 |
 | redis_cluster_nodes | array | 可选         |         |                                                              | 当使用 `redis-cluster` 限速策略时,该属性是 Redis 集群服务节点的地址列表。 |
 
@@ -100,6 +101,7 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335
             "redis_host": "127.0.0.1",
             "redis_port": 6379,
             "redis_password": "password",
+            "redis_database": 1,
             "redis_timeout": 1001
         }
     },
diff --git a/t/plugin/limit-count-redis2.t b/t/plugin/limit-count-redis2.t
new file mode 100644
index 0000000..96d4915
--- /dev/null
+++ b/t/plugin/limit-count-redis2.t
@@ -0,0 +1,190 @@
+#
+# 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.
+#
+BEGIN {
+    if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+        $SkipReason = "unavailable for the hup tests";
+
+    } else {
+        $ENV{TEST_NGINX_USE_HUP} = 1;
+        undef $ENV{TEST_NGINX_USE_STAP};
+    }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route, with redis host and port and default database
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 2,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_timeout": 1001
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: set route, with redis host and port but wrong database
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 2,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 999999,
+                            "redis_timeout": 1001
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: use wrong database
+--- request
+GET /hello
+--- error_code eval
+500
+--- response_body
+{"error_msg":"failed to limit count: failed to change redis db, err: ERR invalid DB index"}
+
+
+
+=== TEST 4: set route, with redis host and port and right database
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 2,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 1,
+                            "redis_timeout": 1001
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 5: up the limit
+--- pipelined_requests eval
+["GET /hello", "GET /hello", "GET /hello", "GET /hello"]
+--- error_code eval
+[200, 200, 503, 503]
+--- no_error_log
+[error]