chenqiaorui commented on issue #7550:
URL: https://github.com/apache/apisix/issues/7550#issuecomment-1208854599
> I want to make sure of one question: do you deploy private code code, such
as custom plugins?
@tzssangglass yes, i have a custom plugin. The plugin use redis
```subscribe``` and ```ngx.share```. The code can be see below:
```
local redis_new = require("resty.redis").new
local core = require("apisix.core")
local setmetatable = setmetatable
local tostring = tostring
local _M = {version = 0.1}
local mt = {
__index = _M
}
function _M.new(plugin_name, conf)
local self = {conf = conf, plugin_name = plugin_name}
return setmetatable(self, mt)
end -- redis conf
local function red_conn(conf)
local red = redis_new()
local timeout = conf.redis_timeout or 1000
red:set_timeouts(timeout, timeout, timeout)
local ok, err = red:connect(conf.redis_host, conf.redis_port or 6379) --
redis conn
if not ok then
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 = red:auth(conf.redis_password) -- redis auth
if not ok then
return nil, err
end
end
elseif err then
return nil, err
end
return red, false
end
function _M.token(self, key)
local auth_token = ngx.shared["auth_token"]
if auth_token == ngx.null then
return false, 'index auth_token error.'
end
if auth_token:get(key) then
return true, nil -- token vaild, return true
end
local conf = self.conf
local red, err = red_conn(conf) -- redis conn
if err then
return false, err
end
core.log.info("***connect to redis ***")
local ok, err = red:select(conf.redis_db)
if not ok then
return false, err
end
local ret, err = red:exists(key)
if not ret then
return false, err
end
local ttl, err = red:ttl(key)
if ttl == -1 then
ttl = 604800
end
ok, err = red:set_keepalive(10000, 100) -- keep redis conn
if not ok then
return false, err
end
if ret == 0 then
return false, "rejected" -- token not exists ,return reject
end
auth_token:set(key,'',ttl) -- set shared memory
return true, ret -- token vaild, return true
end
function _M.subscribe(self, func)
local co = coroutine.create(function()
local conf = self.conf
local red, err = red_conn(conf)
if err then
core.log.error("failed to conn redis: ", err)
end
local ok, err =
red:subscribe('__keyevent@'..conf.redis_db..'__:del') -- subscribe delete
command
if not ok then
core.log.error("failed to create subscribe: ", err)
end
red:subscribe('__keyevent@'..conf.redis_db..'__:expired') --
subscribe expired command
while true do
local ret,err = red:read_reply()
if err then
core.log.info("failed to read_reply at subscribe: ", err)
else
func(ret) -- update shared memeory
end
red:set_keepalive(100, 100) -- keep redis conn
end
end)
coroutine.resume(co)
end
return _M
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]