Skip to content

fix: Run response path plugin hooks asynchronously to reduce streaming latency#2717

Open
gyliu513 wants to merge 2 commits intokubernetes-sigs:mainfrom
gyliu513:async
Open

fix: Run response path plugin hooks asynchronously to reduce streaming latency#2717
gyliu513 wants to merge 2 commits intokubernetes-sigs:mainfrom
gyliu513:async

Conversation

@gyliu513
Copy link
Copy Markdown
Contributor

Fixed #1935

Response header and response body plugins were previously executed synchronously, blocking the ext_proc stream on every chunk, this is unnecessary because these plugins perform side-effect work (metrics reporting, cache updates, metadata collection) that does not modify the chunk content being forwarded to the client.

This PR runs response header plugins and intermediate streaming chunk body plugins asynchronously in goroutines so they no longer add latency to the response path. The final chunk (endOfStream=true) remains synchronous because plugins like requestattributereporter write DynamicMetadata at that point, which must be available when generating the Envoy response.

What type of PR is this?

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #

Does this PR introduce a user-facing change?:

NONE

/kind bug

@k8s-ci-robot k8s-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Mar 29, 2026
@netlify
Copy link
Copy Markdown

netlify bot commented Mar 29, 2026

Deploy Preview for gateway-api-inference-extension ready!

Name Link
🔨 Latest commit 31c524f
🔍 Latest deploy log https://app.netlify.com/projects/gateway-api-inference-extension/deploys/69ca8404963a4b0008d86d52
😎 Deploy Preview https://deploy-preview-2717--gateway-api-inference-extension.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

Hi @gyliu513. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Mar 29, 2026
@gyliu513
Copy link
Copy Markdown
Contributor Author

/cc @kfswain

@k8s-ci-robot k8s-ci-robot requested a review from kfswain March 29, 2026 00:05
if len(d.requestControlPlugins.responseReceivedPlugins) == 0 {
return
}
go d.runResponseHeaderPlugins(ctx, request, response, targetEndpoint)
Copy link
Copy Markdown
Contributor

@LukeAVanDrie LukeAVanDrie Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if we don't block, don't we want to ensure that the handling for chunks A-->B-->C for request, R, are still processed in that order?

If we fire these off as routines and chunks arrive quickly, couldn't this result in out of order execution?

Copy link
Copy Markdown
Contributor Author

@gyliu513 gyliu513 Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch, thanks @LukeAVanDrie. You're right. The PredictedLatency plugin's ResponseBody is stateful across chunks (it tracks TTFT on the first chunk, accumulates TPOT across subsequent chunks via predictedLatencyCtx). Firing goroutines per chunk could result in out-of-order execution and corrupt that state.

How about this approach to keep the order: instead of firing a goroutine per chunk, each request gets a dedicated channel + goroutine queue. The flow would be:

  • Intermediate chunks (endOfStream=false): sent to a per-request buffered channel. A single goroutine reads from the channel and runs plugins sequentially — so chunks A→B→C are always processed in that order.
  • Final chunk (endOfStream=true): the channel is closed, we wait for the goroutine to drain all queued chunks, then run plugins synchronously on the main goroutine. This ensures DynamicMetadata written by plugins (e.g., requestattributereporter) is available before the Envoy response is generated.
  • Response header plugins: remain fire-and-forget async since they are stateless and only called once per request.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds good to me!

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 29, 2026
@ahg-g
Copy link
Copy Markdown
Contributor

ahg-g commented Mar 30, 2026

/ok-to-test
/assign @kfswain

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 30, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: gyliu513
Once this PR has been reviewed and has the lgtm label, please ask for approval from kfswain. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@zetxqx
Copy link
Copy Markdown
Contributor

zetxqx commented Mar 31, 2026

I think this looks good, @kfswain do we want to move forward with this?

@danehans danehans mentioned this pull request Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

All Response path hooks should be async/in a goroutine

6 participants