-
Notifications
You must be signed in to change notification settings - Fork 184
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
concurrent-api: make context capture more generic #3183
Conversation
Motivation: Why is this change being made? Modifications: - List the changes Result: What is the result of this change?
aeaf864
to
45c3eae
Compare
servicetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/CapturedContext.java
Show resolved
Hide resolved
...lk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/ContextPreservingRunnable.java
Outdated
Show resolved
Hide resolved
...rrent-api/src/main/java/io/servicetalk/concurrent/api/CustomCaptureAsyncContextProvider.java
Outdated
Show resolved
Hide resolved
servicetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/CapturedContext.java
Show resolved
Hide resolved
/** | ||
* Functionality related to capturing thread-local like context for later restoration across async boundaries. | ||
*/ | ||
interface CapturedContextProvider { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be @FunctionalInterface
servicetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/CapturedContext.java
Outdated
Show resolved
Hide resolved
servicetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/CapturedContext.java
Outdated
Show resolved
Hide resolved
servicetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/CapturedContext.java
Outdated
Show resolved
Hide resolved
...talk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/CapturedContextProvider.java
Outdated
Show resolved
Hide resolved
...talk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/CapturedContextProvider.java
Outdated
Show resolved
Hide resolved
...-concurrent-api/src/main/java/io/servicetalk/concurrent/api/DefaultAsyncContextProvider.java
Outdated
Show resolved
Hide resolved
...alk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/NoopAsyncContextProvider.java
Outdated
Show resolved
Hide resolved
...-concurrent-api/src/main/java/io/servicetalk/concurrent/api/DefaultAsyncContextProvider.java
Outdated
Show resolved
Hide resolved
...-concurrent-api/src/main/java/io/servicetalk/concurrent/api/SingleSetContextOnSubscribe.java
Outdated
Show resolved
Hide resolved
/** | ||
* Functionality related to capturing thread-local like context for later restoration across async boundaries. | ||
*/ | ||
interface CapturedContextProvider { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be @FunctionalInterface
There was a problem hiding this 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. |
There was a problem hiding this comment.
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!
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 isScope 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.