做一个主动推送的服务,使用redis的pub/sub功能,用到了第三方库https://github.com/pintsized/lua-resty-redis-connector来帮助连接redis sentinel,但是在订阅频道后,循环中read_reply总是会报错closed。
代码类似于:
while true do
if wb.fatal then
red:punsubscribe(pusher.config["channels"])
ngx.log(ngx.ERR, "websocket has disconnected: ", err)
return ngx.exit(444)
end
if ngx.worker.exiting() then
return ngx.exit(444)
end
local res, err = red:read_reply()
if not res then
if err ~= "timeout" then
ngx.log(ngx.ERR, "Redis read error: ", err)
return ngx.exit(444)
end
else
if g_list[res[3]] then
local ok, err = pusher.ws_send(wb, "msg", res[4])
if not ok then
red:punsubscribe(pusher.config["channels"])
ngx.log(ngx.ERR, "failed to send text: ", err)
return ngx.exit(444)
end
end
end
end