diff --git a/lualib/resty/events/init.lua b/lualib/resty/events/init.lua index e5fe6b4f..6467b9b3 100644 --- a/lualib/resty/events/init.lua +++ b/lualib/resty/events/init.lua @@ -13,7 +13,7 @@ local str_sub = string.sub local worker_count = ngx.worker.count() local _M = { - _VERSION = '0.1.0', + _VERSION = '0.1.1', } local _MT = { __index = _M, } diff --git a/lualib/resty/events/worker.lua b/lualib/resty/events/worker.lua index 4874e124..8816aca5 100644 --- a/lualib/resty/events/worker.lua +++ b/lualib/resty/events/worker.lua @@ -14,7 +14,7 @@ local ngx = ngx local log = ngx.log local exiting = ngx.worker.exiting local ERR = ngx.ERR ---local DEBUG = ngx.DEBUG +local DEBUG = ngx.DEBUG local spawn = ngx.thread.spawn local kill = ngx.thread.kill @@ -47,7 +47,7 @@ local PAYLOAD_T = { local _worker_id = ngx.worker.id() local _M = { - _VERSION = '0.1.0', + _VERSION = '0.1.1', } local _MT = { __index = _M, } @@ -66,6 +66,21 @@ local function start_timer(self, delay) end)) end +local check_sock_exist +do + local ffi = require "ffi" + local C = ffi.C + ffi.cdef [[ + int access(const char *pathname, int mode); + ]] + + -- remove prefix 'unix:' + check_sock_exist = function(fpath) + local rc = C.access(fpath:sub(6), 0) + return rc == 0 + end +end + function _M.new(opts) local self = { _queue = que.new(), @@ -84,9 +99,19 @@ function _M:communicate(premature) return end + local listening = self._opts.listening + + if not check_sock_exist(listening) then + log(DEBUG, "unix domain sock(", listening, ") is not ready") + + -- try to reconnect broker, avoid crit error log + start_timer(self, 0.002) + return + end + local conn = assert(client:new()) - local ok, err = conn:connect(self._opts.listening) + local ok, err = conn:connect(listening) if not ok then log(ERR, "failed to connect: ", err)