diff --git a/lualib/resty/kong/peer_conn.lua b/lualib/resty/kong/peer_conn.lua index 0ff6feb8..fa528bdc 100644 --- a/lualib/resty/kong/peer_conn.lua +++ b/lualib/resty/kong/peer_conn.lua @@ -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 {