Skip to content

Commit

Permalink
fix(lib): resty.kong.peer_conn.* can only be used in the http subsy…
Browse files Browse the repository at this point in the history
…stem (#85)

Fix FTI-5616

---------

Signed-off-by: tzssangglass <tzssangglass@gmail.com>
  • Loading branch information
tzssangglass authored May 13, 2024
1 parent a8411f7 commit 7da8c7d
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions lualib/resty/kong/peer_conn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,41 @@ local get_request = base.get_request
local errmsg = base.get_errmsg_ptr()
local C = ffi.C
local ffi_str = ffi.string
local get_phase = ngx.get_phase
local subsystem = ngx.config.subsystem
local NGX_ERROR = ngx.ERROR

local error = error


ffi.cdef[[
int
ngx_http_lua_kong_ffi_get_last_peer_connection_cached(ngx_http_request_t *r,
char **err);
]]


local function get_last_peer_connection_cached()
if get_phase() ~= "balancer" then
error("get_last_peer_connection_cached() can only be called in balancer phase")
end

local r = get_request()
if not r then
error("no request found")
local get_last_peer_connection_cached


if subsystem == "http" then
require "resty.core.phase" -- for ngx.get_phase
local ngx_phase = ngx.get_phase
ffi.cdef[[
int ngx_http_lua_kong_ffi_get_last_peer_connection_cached(ngx_http_request_t *r,
char **err);
]]

get_last_peer_connection_cached = function()
if ngx_phase() ~= "balancer" then
error("get_last_peer_connection_cached() can only be called in balancer phase")
end

local r = get_request()
if not r then
error("no request found")
end

local rc = C.ngx_http_lua_kong_ffi_get_last_peer_connection_cached(r, errmsg)

if rc == NGX_ERROR then
error(ffi_str(errmsg[0]), 2)
end

return rc == 1
end

local rc = C.ngx_http_lua_kong_ffi_get_last_peer_connection_cached(r, errmsg)

if rc == NGX_ERROR then
error(ffi_str(errmsg[0]), 2)
end

return rc == 1
end

return {
Expand Down

0 comments on commit 7da8c7d

Please sign in to comment.