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

nic-6443 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 6749048b0 fix(limit-count): isolate redis-sentinel keepalive pools by 
database and credentials (#13553)
6749048b0 is described below

commit 6749048b05c57e70d376f024cfb4fb870223cec7
Author: Nic <[email protected]>
AuthorDate: Mon Jun 15 10:38:30 2026 +0800

    fix(limit-count): isolate redis-sentinel keepalive pools by database and 
credentials (#13553)
---
 apisix-master-0.rockspec              |   2 +-
 t/lib/test_redis.lua                  |   4 +-
 t/plugin/limit-count-redis-sentinel.t | 115 ++++++++++++++++++++++++++++++++++
 3 files changed, 119 insertions(+), 2 deletions(-)

diff --git a/apisix-master-0.rockspec b/apisix-master-0.rockspec
index f10887106..5cd33ca3f 100644
--- a/apisix-master-0.rockspec
+++ b/apisix-master-0.rockspec
@@ -32,7 +32,7 @@ description = {
 
 dependencies = {
     "lua-resty-ctxdump = 0.1-0",
-    "api7-lua-resty-redis-connector = 0.12.0",
+    "api7-lua-resty-redis-connector = 0.13.0",
     "lyaml = 6.2.8-1",
     "api7-lua-resty-dns-client = 7.1.1-0",
     "lua-resty-template = 2.0-1",
diff --git a/t/lib/test_redis.lua b/t/lib/test_redis.lua
index 8acc1e04e..7f097a460 100644
--- a/t/lib/test_redis.lua
+++ b/t/lib/test_redis.lua
@@ -23,7 +23,9 @@ local tonumber = tonumber
 
 local _M = {}
 
-local DEFAULT_PORTS = {6379, 5000, 5001, 5002, 5003, 5004, 5005, 5006}
+-- 6479 is the sentinel master used by the redis-sentinel policies; leftover
+-- counters there survive CI re-runs if it is not flushed
+local DEFAULT_PORTS = {6379, 6479, 5000, 5001, 5002, 5003, 5004, 5005, 5006}
 local DEFAULT_HOST = "127.0.0.1"
 
 local function log_warn(...)
diff --git a/t/plugin/limit-count-redis-sentinel.t 
b/t/plugin/limit-count-redis-sentinel.t
index 5004e514f..e8b1d001e 100644
--- a/t/plugin/limit-count-redis-sentinel.t
+++ b/t/plugin/limit-count-redis-sentinel.t
@@ -507,3 +507,118 @@ GET /hello
 --- error_code: 500
 --- error_log
 invalid username-password pair
+
+
+
+=== TEST 15: routes with different databases must not share connections
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            for i, db in ipairs({1, 2}) do
+                local code, body = t('/apisix/admin/routes/' .. i,
+                    ngx.HTTP_PUT,
+                    string.format([[{
+                        "uri": "/hello%s",
+                        "plugins": {
+                            "limit-count": {
+                                "count": 5,
+                                "time_window": 60,
+                                "rejected_code": 503,
+                                "key": "remote_addr",
+                                "policy": "redis-sentinel",
+                                "redis_sentinels": [
+                                     {"host": "127.0.0.1", "port": 26379}
+                                 ],
+                                 "redis_master_name": "mymaster",
+                                 "redis_role": "master",
+                                 "redis_database": %d
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        }
+                    }]], i == 1 and "" or "1", db)
+                    )
+                if code >= 300 then
+                    ngx.status = code
+                    ngx.say(body)
+                    return
+                end
+            end
+
+            ngx.sleep(0.5)
+
+            -- alternate requests between the two routes so that the second
+            -- route is served by a connection put into the keepalive pool
+            -- by the first one if the pool is wrongly shared
+            local http = require "resty.http"
+            local uris = {
+                "http://127.0.0.1:"; .. ngx.var.server_port .. "/hello",
+                "http://127.0.0.1:"; .. ngx.var.server_port .. "/hello1",
+            }
+            for i = 1, 2 do
+                for _, uri in ipairs(uris) do
+                    local httpc = http.new()
+                    local res, err = httpc:request_uri(uri)
+                    if not res then
+                        ngx.say("request failed: ", err)
+                        return
+                    end
+                    ngx.say(res.status, " remaining: ",
+                            res.headers["X-RateLimit-Remaining"] or "nil")
+                end
+            end
+
+            -- each database must contain only its own route's counter,
+            -- tracking exactly the 2 requests sent to that route
+            local redis = require "resty.redis"
+            for db = 1, 2 do
+                local red = redis:new()
+                red:set_timeout(1000)
+                local ok, err = red:connect("127.0.0.1", 6479)
+                if not ok then
+                    ngx.say("failed to connect: ", err)
+                    return
+                end
+                ok, err = red:select(db)
+                if not ok then
+                    ngx.say("failed to select db ", db, ": ", err)
+                    return
+                end
+                local keys, err = red:keys("plugin-limit-count*")
+                if not keys then
+                    ngx.say("failed to get keys: ", err)
+                    return
+                end
+                ngx.say("db ", db, " keys: ", #keys)
+                for _, key in ipairs(keys) do
+                    local counter, err = red:get(key)
+                    if err then
+                        ngx.say("failed to get counter of ", key, ": ", err)
+                        return
+                    end
+                    ngx.say("db ", db, " counter: ", counter)
+                    red:del(key)
+                end
+                red:close()
+            end
+
+            for i = 1, 2 do
+                t('/apisix/admin/routes/' .. i, ngx.HTTP_DELETE)
+            end
+        }
+    }
+--- timeout: 10
+--- response_body
+200 remaining: 4
+200 remaining: 4
+200 remaining: 3
+200 remaining: 3
+db 1 keys: 1
+db 1 counter: 2
+db 2 keys: 1
+db 2 counter: 2

Reply via email to