feat(grpc): Add task-local context propagation #2453
+504
−0
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.
This commit introduces a new task-local context propagation mechanism to gRPC, enabling context to be preserved across await points in asynchronous tasks.
The implementation is based on a combination of:
.with_context(ctx).tokio::task_local!, ensuring context is correctly scoped and propagated.Changes:
task_local_contextmodule for managing context scope and restoring context to and from thread local.extensions.rs.Contextto useArc<dyn Context>for efficient sharing.I could've chosen
Contextto be a struct instead of a dyn compatible trait, but chose to do so for the odd case where we may need another google internal implemenation of Context in future. Happy to revert this and move to a struct (like tokio or OTEL) instead.The context itself currently simply contains deadline for all practical purposes for enabling deadline propagation. We can later add more fields as needed.
This change uses the modern file hierarchy recommended by https://doc.rust-lang.org/rust-by-example/mod/split.html which is a departure from the current crate which uses the legacy
mod.rsbased structure. I am happy to move to the legacy method in favor of consistency with rest of the code if needed.