fix(streaming): resolve ctx diverge in server-side streaming #1471
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
fix
Check the PR title.
(Optional) Translate the PR title into Chinese.
fix: 修复 server 侧 streaming 接口 ctx 分叉的问题
(Optional) More detailed description for this PR(en: English/zh: Chinese).
en:
Currently in the server-side streaming scenario, the middleware func(ctx context.Context, request, response interface{}) after processing the ctx (e.g. WithValue) is not perceived by the Stream.Context() in the handler.
Use contextStream to wrap the Stream provided to the user handler, inject the middleware-processed ctx, and override the Stream.Context() method exposed to the user to solve this problem.
zh(optional):
当前在 server 侧 streaming 场景,middleware func(ctx context.Context, request, response interface{}) 对 ctx 进行处理后(例如 WithValue)并不能通过 handler 中的 Stream.Context() 感知到。
利用 contextStream wrap 一下提供给用户 handler 的 Stream,注入经过 middleware 处理后的 ctx,并重写暴露给用户的 Stream.Context() 方法来解决这个问题。
(Optional) Which issue(s) this PR fixes:
(optional) The PR that updates user documentation: