-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5909c86
commit 91e8754
Showing
10 changed files
with
158 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
local redis_new = require("resty.redis").new | ||
local core = require("apisix.core") | ||
|
||
|
||
local _M = {version = 0.1} | ||
|
||
local function redis_cli(conf) | ||
local red = redis_new() | ||
local timeout = conf.redis_timeout or 1000 -- default 1sec | ||
|
||
red:set_timeouts(timeout, timeout, timeout) | ||
|
||
local sock_opts = { | ||
ssl = conf.redis_ssl, | ||
ssl_verify = conf.redis_ssl_verify | ||
} | ||
|
||
local ok, err = red:connect(conf.redis_host, conf.redis_port or 6379, sock_opts) | ||
if not ok then | ||
core.log.error(" redis connect error, error: ", err) | ||
return false, err | ||
end | ||
|
||
local count | ||
count, err = red:get_reused_times() | ||
if 0 == count then | ||
if conf.redis_password and conf.redis_password ~= '' then | ||
local ok, err | ||
if conf.redis_username then | ||
ok, err = red:auth(conf.redis_username, conf.redis_password) | ||
else | ||
ok, err = red:auth(conf.redis_password) | ||
end | ||
if not ok then | ||
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 | ||
end | ||
return red, nil | ||
end | ||
|
||
|
||
|
||
function _M.new(conf) | ||
return redis_cli(conf) | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
local rediscluster = require("resty.rediscluster") | ||
local core = require("apisix.core") | ||
local ipairs = ipairs | ||
|
||
local _M = {version = 0.1} | ||
|
||
local function new_redis_cluster(conf) | ||
local config = { | ||
name = conf.redis_cluster_name, | ||
serv_list = {}, | ||
read_timeout = conf.redis_timeout, | ||
auth = conf.redis_password, | ||
dict_name = "plugin-limit-conn-redis-cluster-slot-lock", | ||
connect_opts = { | ||
ssl = conf.redis_cluster_ssl, | ||
ssl_verify = conf.redis_cluster_ssl_verify, | ||
} | ||
} | ||
|
||
for i, conf_item in ipairs(conf.redis_cluster_nodes) do | ||
local host, port, err = core.utils.parse_addr(conf_item) | ||
if err then | ||
return nil, "failed to parse address: " .. conf_item | ||
.. " err: " .. err | ||
end | ||
|
||
config.serv_list[i] = {ip = host, port = port} | ||
end | ||
|
||
local red_cli, err = rediscluster:new(config) | ||
if not red_cli then | ||
return nil, "failed to new redis cluster: " .. err | ||
end | ||
|
||
return red_cli | ||
end | ||
|
||
|
||
function _M.new(conf) | ||
return new_redis_cluster(conf) | ||
end | ||
|
||
|
||
return _M |
Oops, something went wrong.