Skip to content

bug: The custom plugin modifies the request body of the streaming response returned by the server. APISIX does not respond in one chunk at a time, but accumulates several chunks to respond together #11977

@GreatLazyMan

Description

@GreatLazyMan

Current Behavior

开发了一个插件,修改了server返回的流式响应的请求体,apisix一次响应不是一个chunk,而是累积几个chunk一起响应
已经尝试在route和nginx关闭cache,没有效果。示例代码

Expected Behavior

期望在修改响应体后,可以流式的返回响应体

Error Logs

No response

Steps to Reproduce

1.启动apisix和etcd
2.配置文件中配置自定义插件
3.启动测试服务器,可以返回流式响应
4.配置route和upstream
插件代码示例

local plugin_name = "extract-reasoning"
local core = require("apisix.core")
local cjson = require("cjson.safe")
local ngx = ngx

local schema = {
    type = "object",
    properties = {},
}

local _M = {
    version = 1.0,
    priority = 1000,
    name = plugin_name,
    schema = schema,
}


function _M.header_filter(conf, ctx)
    -- 设置SSE响应头
    ngx.header["Content-Type"] = "text/event-stream"
    -- ngx.header["Cache-Control"] = "no-cache"
    ngx.header["Connection"] = "keep-alive"
    ngx.header["Cache-Control"] = "no-cache, no-store, must-revalidate"
    ngx.header["Pragma"] = "no-cache"
    ngx.header["Expires"] = "0"
end

function _M.body_filter(conf, ctx)
    local chunk = ngx.arg[1]
    local is_last = ngx.arg[2]

    ngx.arg[1] = chunk
end

return _M

Environment

  • APISIX version (run apisix version):
  • Operating system (run uname -a):
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions