Skip to content

Commit 31c74ce

Browse files
committed
refactor: sync code style with gateway — use ngx_flush(wait == true) and improve test robustness
1 parent 7f735b1 commit 31c74ce

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

apisix/plugin.lua

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,11 +1409,7 @@ function _M.lua_response_filter(api_ctx, headers, body, wait)
14091409
if not ok then
14101410
return false, err
14111411
end
1412-
if wait then
1413-
ok, err = ngx_flush(true)
1414-
else
1415-
ok, err = ngx_flush()
1416-
end
1412+
ok, err = ngx_flush(wait == true)
14171413
if not ok then
14181414
return false, err
14191415
end
@@ -1448,11 +1444,7 @@ function _M.lua_response_filter(api_ctx, headers, body, wait)
14481444
if not ok then
14491445
return false, err
14501446
end
1451-
if wait then
1452-
ok, err = ngx_flush(true)
1453-
else
1454-
ok, err = ngx_flush()
1455-
end
1447+
ok, err = ngx_flush(wait == true)
14561448
if not ok then
14571449
return false, err
14581450
end

t/plugin/ai-proxy-client-disconnect.t

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ add_block_preprocessor(sub {
4444
ngx.header["Content-Type"] = "text/event-stream"
4545
local dict = ngx.shared["test"]
4646
dict:set("upstream_chunks", 0)
47+
-- Stream up to 2000 chunks with 30ms sleep between each.
48+
-- The proxy should abort well before this completes when
49+
-- the client disconnects.
4750
for i = 1, 2000 do
4851
local ok, err = ngx.print(
4952
'data: {"id":"chatcmpl-1","object":'
@@ -63,6 +66,7 @@ add_block_preprocessor(sub {
6366
}
6467
}
6568
69+
# Probe endpoint to read the current chunk count.
6670
location /chunks {
6771
content_by_lua_block {
6872
local dict = ngx.shared["test"]
@@ -164,7 +168,9 @@ passed
164168
165169
-- Allow time for the proxy to detect the disconnect and stop
166170
-- feeding the upstream connection, then capture the chunk count.
167-
ngx.sleep(0.3)
171+
-- 1s window: unfixed path produces ~33 chunks (1000ms / 30ms per
172+
-- chunk); fixed path stops within a few chunks of the disconnect.
173+
ngx.sleep(1.0)
168174
169175
-- Read chunk count from the mock upstream's probe endpoint.
170176
local probe = http.new()
@@ -187,11 +193,24 @@ passed
187193
local count_str = probe_res:read_body()
188194
probe:close()
189195
190-
local count = tonumber(count_str) or 0
191-
-- Without the fix, the upstream would have produced hundreds of
192-
-- chunks by now. With the fix it stops shortly after disconnect.
193-
-- We expect well under 50 chunks after only 0.3s budget.
194-
if count > 50 then
196+
if probe_res.status ~= 200 then
197+
ngx.status = 500
198+
ngx.say("probe status unexpected: ", probe_res.status)
199+
return
200+
end
201+
202+
local count = tonumber(count_str)
203+
if not count then
204+
ngx.status = 500
205+
ngx.say("invalid probe response: ", count_str or "nil")
206+
return
207+
end
208+
209+
-- With the fix the upstream stops shortly after client disconnect
210+
-- (well under 15 chunks). Without the fix it reaches ~33 chunks in
211+
-- the 1s observation window, so this threshold reliably catches the
212+
-- regression while leaving ample headroom for timing variation.
213+
if count > 15 then
195214
ngx.status = 500
196215
ngx.say("upstream was not aborted promptly, chunks: ", count)
197216
return

0 commit comments

Comments
 (0)