Skip to content

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

Merged
k8s-ci-robot merged 3 commits intokubernetes-sigs:mainfrom
gyliu513:async
Apr 18, 2026
Merged

fix: Run response path plugin hooks asynchronously to reduce streaming latency#2717
k8s-ci-robot merged 3 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 c3c2ada
🔍 Latest deploy log https://app.netlify.com/projects/gateway-api-inference-extension/deploys/69e21a2a6d3bd80008d1c384
😎 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
Comment thread pkg/epp/requestcontrol/director.go Outdated
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!

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.

Re-reading this, how about we don't fire-and-forget the responseHeader just leave it as is calling in sync. Because intuitively responseHeader should be also called before any other responseBody.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, I was using sync to handle response header now.

@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
@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
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 11, 2026
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 12, 2026
@gyliu513
Copy link
Copy Markdown
Contributor Author

I think this is in good shape, @kfswain @zetxqx can you help review again? Thanks!

@ahg-g
Copy link
Copy Markdown
Contributor

ahg-g commented Apr 16, 2026

/assign @zetxqx

Bob, can you pls take a look, this touches request control and handling.

Copy link
Copy Markdown
Contributor

@zetxqx zetxqx left a comment

Choose a reason for hiding this comment

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

Thanks for your patience. One major concern I have is whether we should make responseHeader async. Theoretically, this could allow responseHeader to be invoked after responseBody, which change the existing behavior a little bit.

If we keep the responseHeader call synchronous, the primary benefit of this PR would be faster delivery of intermediate response chunks to the client. Because we have to wait for the final chunk to modify the dynamic data.

Comment thread pkg/epp/requestcontrol/director.go Outdated
Comment on lines +493 to +495
if len(d.requestControlPlugins.responseReceivedPlugins) == 0 {
return
}
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.

nit: can we also move this early return check to func (d *Director) HandleResponseHeader ? consistent with the style with func (d *Director) HandleResponseBody( ? we can probably inline runResponseHeaderPluginsAsync

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread pkg/epp/requestcontrol/director.go Outdated
if len(d.requestControlPlugins.responseReceivedPlugins) == 0 {
return
}
go d.runResponseHeaderPlugins(ctx, request, response, targetEndpoint)
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.

Re-reading this, how about we don't fire-and-forget the responseHeader just leave it as is calling in sync. Because intuitively responseHeader should be also called before any other responseBody.

Copy link
Copy Markdown
Contributor Author

@gyliu513 gyliu513 left a comment

Choose a reason for hiding this comment

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

Thanks @zetxqx , comments addressed, can you review?

Comment thread pkg/epp/requestcontrol/director.go Outdated
Comment on lines +493 to +495
if len(d.requestControlPlugins.responseReceivedPlugins) == 0 {
return
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread pkg/epp/requestcontrol/director.go Outdated
if len(d.requestControlPlugins.responseReceivedPlugins) == 0 {
return
}
go d.runResponseHeaderPlugins(ctx, request, response, targetEndpoint)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, I was using sync to handle response header now.

@gyliu513 gyliu513 force-pushed the async branch 2 times, most recently from fe6c314 to 07f201e Compare April 17, 2026 01:17
Comment thread pkg/epp/requestcontrol/director_test.go Outdated
Comment on lines +1204 to +1216
// HandleResponseHeader runs plugins asynchronously, so wait for completion.
require.Eventually(t, func() bool {
pr1.mu.Lock()
defer pr1.mu.Unlock()
return pr1.lastRespOnResponse != nil
}, time.Second, 10*time.Millisecond, "response header plugin should have been called")

pr1.mu.Lock()
lastResp := pr1.lastRespOnResponse
lastTargetPod := pr1.lastTargetPodOnResponse
pr1.mu.Unlock()

if diff := cmp.Diff("test-req-id-for-response", lastResp.RequestId); diff != "" {
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.

nit: since we change the responseHeader back to sync we can revert back to the previous verification way,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@zetxqx
Copy link
Copy Markdown
Contributor

zetxqx commented Apr 17, 2026

Thanks @zetxqx , comments addressed, can you review?

Thank you, just one nit comment regarding the unittest

@zetxqx
Copy link
Copy Markdown
Contributor

zetxqx commented Apr 17, 2026

/lgtm

cc: @ahg-g

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 17, 2026
@ahg-g
Copy link
Copy Markdown
Contributor

ahg-g commented Apr 18, 2026

/approve

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ahg-g, gyliu513

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

The pull request process is described 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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 18, 2026
@k8s-ci-robot k8s-ci-robot merged commit c6d3ad1 into kubernetes-sigs:main Apr 18, 2026
11 checks passed
@gyliu513 gyliu513 deleted the async branch April 18, 2026 13:57
elevran pushed a commit to llm-d/llm-d-inference-scheduler that referenced this pull request Apr 23, 2026
…g latency (kubernetes-sigs/gateway-api-inference-extension#2717)

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

* fix: async response body plugin processing with per-request channel queue for ordered execution

* sync handle request header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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