Skip to content

Commit

Permalink
fix(core): fix response status code is not real upstream status when …
Browse files Browse the repository at this point in the history
…using kong.response function (#11437)

When kong uses kong_buffered_http and upstream timeout, the return status code needs to return 504 (which return by ngx.location.capture), not 502.

FTI-5320
  • Loading branch information
oowl authored and windmgc committed Mar 8, 2024
1 parent 9e950c9 commit 7095210
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/response_status_code.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Fix an issue that response status code is not real upstream status when using kong.response function.
type: bugfix
scope: Core
2 changes: 1 addition & 1 deletion kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ do
local res = ngx.location.capture("/kong_buffered_http", options)
if res.truncated and options.method ~= ngx.HTTP_HEAD then
ctx.KONG_PHASE = PHASES.error
ngx.status = 502
ngx.status = res.status or 502

if has_timing then
req_dyn_hook_run_hooks(ctx, "timing", "after:response")
Expand Down
45 changes: 44 additions & 1 deletion spec/02-integration/05-proxy/07-upstream_timeouts_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ for _, strategy in helpers.each_strategy() do

route.service = bp.services:insert(service)

if route.enable_buffering then
route.enable_buffering = nil
bp.plugins:insert({
name = "pre-function",
service = { id = route.service.id },
config = {
access = {
[[
kong.service.request.enable_buffering()
]],
},
}
})
end

if not route.protocols then
route.protocols = { "http" }
end
Expand Down Expand Up @@ -73,6 +88,17 @@ for _, strategy in helpers.each_strategy() do
read_timeout = 1, -- ms
},
},
{
methods = { "PUT" },
service = {
name = "api-4",
protocol = "http",
host = "konghq.com",
port = 81,
connect_timeout = 1, -- ms
},
enable_buffering = true,
},
}

bp.plugins:insert {
Expand All @@ -83,7 +109,7 @@ for _, strategy in helpers.each_strategy() do
}

assert(helpers.start_kong({
plugins = "ctx-checker-last",
plugins = "bundled, ctx-checker-last",
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
}))
Expand Down Expand Up @@ -161,5 +187,22 @@ for _, strategy in helpers.each_strategy() do
assert.equal(504, res.status)
end)
end)

describe("upstream_connect_timeout with enable_buffering", function()
it("sets upstream send timeout value", function()
local res = assert(proxy_client:send {
method = "PUT",
path = "/put",
body = {
huge = string.rep("a", 2^25)
},
headers = { ["Content-Type"] = "application/json" },
})

assert.equal(504, res.status)
end)
end)


end)
end

0 comments on commit 7095210

Please sign in to comment.