Skip to content

Commit

Permalink
feat(rpc): connection lost event
Browse files Browse the repository at this point in the history
Precondition for KAG-6178
  • Loading branch information
StarlightIbuki committed Feb 11, 2025
1 parent 04f8094 commit 2f9c52b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions kong/clustering/rpc/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ function _M:connect(premature, node_id, host, path, cert, key)

local ok, err = c:connect(uri, opts)
if not ok then
ngx_log(ngx_ERR, _log_prefix, "unable to connect to peer: ", err)
err = "unable to connect to peer: " .. (err or "unknown error")
goto err
end

Expand All @@ -596,7 +596,7 @@ function _M:connect(premature, node_id, host, path, cert, key)
-- FIXME: resp_headers should not be case sensitive

if not resp_headers or not resp_headers["sec_websocket_protocol"] then
ngx_log(ngx_ERR, _log_prefix, "peer did not provide sec_websocket_protocol, node_id: ", node_id)
err = "peer did not provide sec_websocket_protocol, node_id: " .. (node_id or "unknown")
c:send_close() -- can't do much if this fails
goto err
end
Expand All @@ -605,16 +605,16 @@ function _M:connect(premature, node_id, host, path, cert, key)
local meta_cap = resp_headers["sec_websocket_protocol"]

if meta_cap ~= RPC_META_V1 then
ngx_log(ngx_ERR, _log_prefix, "did not support protocol : ", meta_cap)
err = "did not support protocol : " .. (type(meta_cap) == "string" and meta_cap or "unknown")
c:send_close() -- can't do much if this fails
goto err
end

-- if timeout (default is 5s) we will close the connection
local ok, err = self:_meta_call(c, meta_cap, node_id)
if not ok then
ngx_log(ngx_ERR, _log_prefix, "unable to handshake with server, node_id: ", node_id,
" err: ", err)
err = "unable to handshake with server, node_id: " .. (node_id or "unknown")
.. ", err: " .. (err or "unknown error")
c:send_close() -- can't do much if this fails
goto err
end
Expand All @@ -634,6 +634,13 @@ function _M:connect(premature, node_id, host, path, cert, key)
end

::err::
local worker_events = assert(kong.worker_events)

-- notify this worker
local ok, err = worker_events.post_local("clustering:jsonrpc", "connection_lost")
if not ok then
ngx_log(ngx_ERR, _log_prefix, "unable to post rpc connection_lost event: ", err)
end

if not exiting() then
c:close()
Expand Down

0 comments on commit 2f9c52b

Please sign in to comment.