Skip to content

fix: server list duplicated in the cache #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/resty/rediscluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ end

local function try_hosts_slots(self, serv_list)
local errors = {}
local serv_dup_check = {}
local config = self.config
if #serv_list < 1 then
return nil, "failed to fetch slots, serv_list config is empty"
Expand Down Expand Up @@ -141,7 +142,11 @@ local function try_hosts_slots(self, serv_list)

-- generate new list of servers
for j = 3, #sub_info do
servers.serv_list[#servers.serv_list + 1 ] = { ip = sub_info[j][1], port = sub_info[j][2] }
local key = tostring(sub_info[j][1]) .. tostring(sub_info[j][2])
if not serv_dup_check[key] then
servers.serv_list[#servers.serv_list + 1] = { ip = sub_info[j][1], port = sub_info[j][2] }
serv_dup_check[key] = true
end
end

for slot = start_slot, end_slot do
Expand All @@ -156,6 +161,7 @@ local function try_hosts_slots(self, serv_list)
--ngx.log(ngx.NOTICE, "finished initializing slotcache...")
slot_cache[self.config.name] = slots
slot_cache[self.config.name .. "serv_list"] = servers
ngx.log(ngx.NOTICE, "servers cnt: ", #servers.serv_list)
else
table_insert(errors, err)
end
Expand Down
45 changes: 44 additions & 1 deletion t/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -929,4 +929,47 @@ GET /t
^.*failed to fetch slots: connection refused.*$
--- timeout: 3
--- no_error_log
[alert]
[alert]

=== TEST 14: test server list cnt
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua '
local config = {
name = "testCluster", --rediscluster name
serv_list = { --redis cluster node list(host and port),
{ ip = "127.0.0.1", port = 7000 },
{ ip = "127.0.0.1", port = 7001 },
{ ip = "127.0.0.1", port = 7002 },
{ ip = "127.0.0.1", port = 7003 },
{ ip = "127.0.0.1", port = 7004 },
{ ip = "127.0.0.1", port = 7005 },
{ ip = "127.0.0.1", port = 7006 }
},
keepalive_timeout = 60000, --redis connection pool idle timeout
keepalive_cons = 1000, --redis connection pool size
connect_timeout = 1000, --timeout while connecting
read_timeout = 200, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
package.loaded["resty.rediscluster"] = nil
local redis = require "resty.rediscluster"
local red, err = redis:new(config)

if err then
ngx.say("failed to create: ", err)
return
end

ngx.say("connected")
';
}
--- request
GET /t
--- response_body_like
^.*timeout.*$
--- error_log
try_hosts_slots(): serv_list size: 7