Skip to content

Commit

Permalink
feat(pdk): add service.set_target_retry_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion committed Aug 2, 2024
1 parent 494d0f0 commit 36b8794
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
22 changes: 22 additions & 0 deletions kong/pdk/service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ local function new()
ctx.balancer_data.port = port
end


-- Sets the retry callback function when the target set by `service.set_target`
-- failed to connect. The callback function will be called with no argument and
-- must return `host`, `port` and `err` if any.
--
--
-- @function kong.service.set_target_retry_callback
-- @phases access
-- @tparam function retry_callback
-- @usage
-- kong.service.set_target_retry_callback(function() return "service.local", 443 end)
function service.set_target_retry_callback(retry_callback)
check_phase(PHASES.access)

if type(retry_callback) ~= "function" then
error("retry_callback must be a function", 2)
end

ngx.ctx.balancer_data.retry_callback = retry_callback
end


---
-- Sets the retries count for the current request. This will override the
-- default retries count set in the Upstream entity.
Expand Down
10 changes: 10 additions & 0 deletions kong/runloop/balancer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ local function execute(balancer_data, ctx)
balancer_data.balancer_handle = handle

else
-- Note: balancer_data.retry_callback is only set by PDK once in access phase
-- if kong.service.set_target_retry_callback is called
if balancer_data.try_count ~= 0 and balancer_data.retry_callback then
local pok, perr, err = pcall(balancer_data.retry_callback)
if not pok or not perr then
log(ERR, "retry handler failed: ", err or perr)
return nil, "failure to get a peer from retry handler", 503
end
end

-- have to do a regular DNS lookup
local try_list
local hstate = run_hook("balancer:to_ip:pre", balancer_data.host)
Expand Down
4 changes: 2 additions & 2 deletions t/01-pdk/06-service-request/00-phase_checks.t
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ qq{
args = { "http" },
init_worker = false,
certificate = "pending",
rewrite = "forced false",
rewrite = true,
access = true,
response = "forced false",
header_filter = "forced false",
Expand All @@ -71,7 +71,7 @@ qq{
args = { "/" },
init_worker = false,
certificate = "pending",
rewrite = "forced false",
rewrite = true,
access = true,
response = "forced false",
header_filter = "forced false",
Expand Down
31 changes: 27 additions & 4 deletions t/01-pdk/09-service/00-phase_checks.t
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ qq{
body_filter = "forced false",
log = "forced false",
admin_api = "forced false",
}, {
method = "set_target_retry_callback",
args = { function() end },
init_worker = "forced false",
certificate = "pending",
rewrite = "forced false",
access = true,
response = "forced false",
header_filter = "forced false",
body_filter = "forced false",
log = "forced false",
admin_api = "forced false",
}, {
method = "set_timeouts",
args = { 1, 2, 3},
Expand Down Expand Up @@ -251,8 +263,7 @@ qq{
log = "pending",
admin_api = "forced false",
preread = "pending",
},
{
}, {
method = "set_retries",
args = { 3, },
init_worker = "forced false",
Expand All @@ -265,6 +276,19 @@ qq{
log = "pending",
admin_api = "forced false",
preread = "pending",
}, {
method = "set_target_retry_callback",
args = { function() end },
init_worker = "forced false",
certificate = "pending",
rewrite = "forced false",
access = true,
response = "forced false",
header_filter = "forced false",
body_filter = "forced false",
log = "pending",
admin_api = "forced false",
preread = "pending",
}, {
method = "set_timeouts",
args = { 1, 2, 3},
Expand All @@ -278,8 +302,7 @@ qq{
log = "pending",
admin_api = "forced false",
preread = "pending",
},
{
}, {
method = "set_tls_cert_key",
args = { chain, key, },
init_worker = false,
Expand Down

0 comments on commit 36b8794

Please sign in to comment.