Skip to content

(feat) add support to cmds with subcmd such as 'object' and 'pubsub' #90

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 1 commit into
base: master
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
24 changes: 23 additions & 1 deletion lib/resty/rediscluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ local cluster_invalid_cmds = {
["shutdown"] = true
}

local cmds_with_sub_cmd = {
["object"] = 2, -- offset to key (which means for command "object", slot key is at offset 2)
["pubsub"] = 2
}

local function redis_slot(str)
return redis_crc(parse_key(str))
end
Expand Down Expand Up @@ -469,7 +474,7 @@ local function handle_command_with_retry(self, target_ip, target_port, asking, c

local need_to_retry = false
local res
if cmd == "eval" or cmd == "evalsha" then
if cmd == "eval" or cmd == "evalsha" or cmds_with_sub_cmd[cmd] then
res, err = redis_client[cmd](redis_client, ...)
else
res, err = redis_client[cmd](redis_client, key, ...)
Expand Down Expand Up @@ -776,13 +781,30 @@ eval(script, 0, arg1, arg2 ...)
local key = args[3] or "no_key"
return _do_cmd(self, cmd, key, ...)
end

local function _do_sub_cmd(self, cmd, ...)
--[[
command usage:
object subcommand [arguments]
e.g object idletime "mylist"
pubsub subcommand [arguments]
e.g pubsub channels
]]
local args = {...}
local key_index = cmds_with_sub_cmd[cmd]
local key = args[key_index] or "no_key"
return _do_cmd(self, cmd, key, ...)
end

-- dynamic cmd
setmetatable(_M, {
__index = function(_, cmd)
local method =
function(self, ...)
if cmd == "eval" or cmd == "evalsha" then
return _do_eval_cmd(self, cmd, ...)
elseif cmds_with_sub_cmd[cmd] then
return _do_sub_cmd(self, cmd, ...)
else
return _do_cmd(self, cmd, ...)
end
Expand Down
106 changes: 83 additions & 23 deletions t/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ __DATA__
read_timeout = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -115,7 +115,7 @@ dog: an animal
read_timeout = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -160,7 +160,7 @@ flushall: true
keepalive_cons = 1000, --redis connection pool size
connect_timeout = 1000, --timeout while connecting
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -193,7 +193,7 @@ flushall: true
ngx.say("get not_found: ", res)
end


';
}
--- request
Expand Down Expand Up @@ -226,7 +226,7 @@ not_found not found.
read_timeout = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -259,7 +259,7 @@ not_found not found.
ngx.say("get nokey: ", #res, " (", type(res), ")")
end


';
}
--- request
Expand Down Expand Up @@ -293,7 +293,7 @@ get nokey: 0 (table)
read_timeout = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -373,7 +373,7 @@ get nokey: 0 (table)

ngx.say("connections: ", res)


';
}
--- request
Expand Down Expand Up @@ -412,7 +412,7 @@ connections: 1
read_timeout = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -460,7 +460,7 @@ failed to set connections: nil: ERR wrong number of arguments for 'incr' command
read_timeout = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -499,7 +499,7 @@ failed to set connections: nil: ERR wrong number of arguments for 'incr' command
local cjson = require "cjson"
ngx.say("lrange result: ", cjson.encode(res))


';
}
--- request
Expand Down Expand Up @@ -534,7 +534,7 @@ lrange result: ["hello","world"]
read_timeout = 2500, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -565,7 +565,7 @@ lrange result: ["hello","world"]
local cjson = require "cjson"
ngx.say("blpop result: ", cjson.encode(res))


';
}
--- request
Expand Down Expand Up @@ -599,7 +599,7 @@ no element popped.
read_timeout = 200, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -630,7 +630,7 @@ no element popped.
local cjson = require "cjson"
ngx.say("blpop result: ", cjson.encode(res))


';
}
--- request
Expand Down Expand Up @@ -663,7 +663,7 @@ lua tcp socket read timed out
read_timeout = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand All @@ -686,7 +686,7 @@ lua tcp socket read timed out
end

ngx.say("set dog: ", res)


local res, err = red:mget("dog{111}", "cat{111}", "dog{111}")
if err then
Expand All @@ -697,7 +697,7 @@ lua tcp socket read timed out
local cjson = require "cjson"
ngx.say("mget result: ", cjson.encode(res))


';
}
--- request
Expand Down Expand Up @@ -730,7 +730,7 @@ mget result: ["an animal",null,"an animal"]
read_timeout = 200, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -789,7 +789,7 @@ mget result: ["an animal",null,"an animal"]
ngx.say("cat: ", h.cat)
ngx.say("cow: ", h.cow)


';
}
--- request
Expand Down Expand Up @@ -825,7 +825,7 @@ cow: moo
read_timeout = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection

}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
Expand Down Expand Up @@ -909,7 +909,7 @@ foo: false, type: string
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"
Expand All @@ -929,4 +929,64 @@ GET /t
^.*failed to fetch slots: connection refused.*$
--- timeout: 3
--- no_error_log
[alert]
[alert]


=== TEST 14: object idletime
--- 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 = "192.168.255.3", port = 7000 },
{ ip = "192.168.255.3", port = 7001 },
{ ip = "192.168.255.3", port = 7002 },
{ ip = "192.168.255.3", port = 7003 },
{ ip = "192.168.255.3", port = 7004 },
{ ip = "192.168.255.3", port = 7005 },
{ ip = "192.168.255.3", 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 = 1000, --timeout while reading
send_timeout = 1000, --timeout while sending
max_redirection = 5 --maximum retry attempts for redirection
}
local redis = require "resty.rediscluster"
local red, err = redis:new(config)
if err then
ngx.say("failed to create: ", err)
return
end
local res, err = red:set("dog", "an animal")
if not res then
ngx.say("failed to set dog: ", err)
return
end
ngx.say("set dog: ", res)
for i = 1, 2 do
local res, err = red:object("idletime", "dog")
if err then
ngx.say("failed to get idletime of dog: ", err)
return
end
if not res then
ngx.say("dog not found.")
return
end
if type(res) == "number" then
ngx.say("idletime received")
end
end
';
}
--- request
GET /t
--- response_body
set dog: OK
idletime received
idletime received
--- no_error_log