Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concurrent-api: make context capture more generic #3183

Merged

Conversation

bryce-anderson
Copy link
Contributor

@bryce-anderson bryce-anderson commented Feb 5, 2025

Motivation:

Our context capture and restore process is centered upon the ContextMap type. The AsyncContext type is only one specific type of context that applications may want to capture and restore across async boundaries. Other examples include the OTEL context, grpc context, and really any random ThreadLocal you want to preserve across async boundaries. They don't necessarily fit the same pattern as a AsycContext.

Modifications:

Further abstract the context type to a CapturedContext . The key method is Scope attachContext() which has the job of attaching whatever context it knows about. The key benefit of this abstraction is that it is very composable. We can add extensions that are simple proxy wrappers that capture the context and restore their own, while delegating to an underlying version that is ultimately rooted in what we know as the AsyncContext.

Result:

More composable context capture and restore.

Motivation:

Why is this change being made?

Modifications:

- List the changes

Result:

What is the result of this change?
@bryce-anderson bryce-anderson force-pushed the bl_anderson/GenericCaptureContext branch from aeaf864 to 45c3eae Compare February 5, 2025 00:49
/**
* Functionality related to capturing thread-local like context for later restoration across async boundaries.
*/
interface CapturedContextProvider {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the nugget that lets users 'hook' into the context capture and restore process.

Copy link
Member

Choose a reason for hiding this comment

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

Should be @FunctionalInterface

@bryce-anderson bryce-anderson marked this pull request as ready for review February 6, 2025 19:15
/**
* Functionality related to capturing thread-local like context for later restoration across async boundaries.
*/
interface CapturedContextProvider {
Copy link
Member

Choose a reason for hiding this comment

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

Should be @FunctionalInterface

Copy link
Member

@idelpivnitskiy idelpivnitskiy left a comment

Choose a reason for hiding this comment

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

🚀 🚀 🚀

@Override
public Scope attachContext() {
return AsyncContext.provider().attachContextMap(this);
}

// Scope method
// Scope method. For the base async context implementation, the `prev` map instance is returned as the Scope if that
// is the context state that needs to be restored and restoring just means setting it to the thread-local.
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding these clarifications!

@bryce-anderson bryce-anderson merged commit f0851de into apple:main Feb 7, 2025
11 checks passed
@bryce-anderson bryce-anderson deleted the bl_anderson/GenericCaptureContext branch February 7, 2025 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants