Skip to content

Commit

Permalink
chore(worker): actively close connection to broker on error (#63)
Browse files Browse the repository at this point in the history
### Summary

If worker errors for any reason, it is good to actively close connection to broker rather than waiting garbage collector to kick in and finalize it.
  • Loading branch information
bungle authored Jun 19, 2024
1 parent c34192e commit a7f3d93
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lualib/resty/events/protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ function _Client:connect(addr)
return true
end

function _Client:close()
local sock = self.sock
if not sock then
return nil, "not initialized"
end

local ok, err = sock:close()
if not ok then
return nil, err
end

return true
end

return {
server = _Server,
client = _Client,
Expand Down
8 changes: 8 additions & 0 deletions lualib/resty/events/worker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ function _M:communicate()
local ok, err = broker_connection:connect(listening)

if exiting() then
if ok then
broker_connection:close()
end
return
end

Expand All @@ -271,6 +274,9 @@ function _M:communicate()
if exiting() then
kill(read_thread_co)
kill(write_thread_co)

broker_connection:close()

return
end

Expand All @@ -285,6 +291,8 @@ function _M:communicate()
wait(read_thread_co)
wait(write_thread_co)

broker_connection:close()

start_communicate_timer(self, random_delay())
end

Expand Down

0 comments on commit a7f3d93

Please sign in to comment.