Skip to content

Commit

Permalink
fix(worker) check sock file existence (#12)
Browse files Browse the repository at this point in the history
* use access to check sock file

* bump to 0.1.1
  • Loading branch information
chronolaw authored Jun 29, 2022
1 parent 011e629 commit 6ceaba5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lualib/resty/events/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, }

Expand Down
31 changes: 28 additions & 3 deletions lualib/resty/events/worker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, }

Expand All @@ -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(),
Expand All @@ -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)

Expand Down

0 comments on commit 6ceaba5

Please sign in to comment.