Add static wrappers for Executor / ExecutorService using current cont… - #2988
Conversation
…ext at invocation time.
Codecov Report
@@ Coverage Diff @@
## main #2988 +/- ##
===========================================
+ Coverage 0 90.29% +90.29%
- Complexity 0 2791 +2791
===========================================
Files 0 323 +323
Lines 0 8677 +8677
Branches 0 875 +875
===========================================
+ Hits 0 7835 +7835
- Misses 0 579 +579
- Partials 0 263 +263
Continue to review full report at Codecov.
|
jkwatson
left a comment
There was a problem hiding this comment.
This seems like a reasonable addition to the API to me. @bogdandrutu any thoughts?
|
Looked over this without any knowledge of prior discussions, apologies if I'm missing context. My initial thought is there's a lot of nearly identical code and whether there's another way to approach it. For instance, could An alternative to |
I'm not sure I follow the suggestion but I think this may not work mostly because it's not the methods that need context, it's the callbacks that need to be wrapped. But if there are ways to reduce duplication in these internal classes it'd be great to follow up on it :-)
I think the problem I had with method names with current context on the right side is it sounded even more so that the context used is what's current when calling this method itself, not what's current when the b executor is invoked. Another idea that came to mind which I like now is |
I can certainly put together a PR with the idea once this is in place.
I think I'm misunderstanding something, as I thought what you describe is what it does. The current context is passed in such a way that it's active during the callback on the Executor. Any help in understanding where I went wrong would be appreciated.
|
The context that should be used isn't the one that is current when |
|
Thanks @anuraaga, I'd missed that. I agree that the names I've suggested don't really fit the delayed execution meaning. I think one of your original ideas works, |
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| class CurrentContextExecutorService extends ForwardingExecutorService { |
| * Returns an {@link Executor} which delegates to the provided {@code executor}, wrapping all | ||
| * invocations with the {@linkplain Context#current() current context} at the time of invocation. | ||
| */ | ||
| static Executor currentContextWrapping(Executor executor) { |
There was a problem hiding this comment.
Naming is the hardest problem in eng, maybe consider wrapCurrent just in case we will add equivalent for wrapping with current Runnable/Callable and would be also consistent with current wrap helpers?
There was a problem hiding this comment.
maybe consider wrapCurrent just in case we will add equivalent for wrapping with current Runnable/Callable and would be also consistent with current wrap helpers?
I don't think we'll be adding those since it's very easy to call Context.current().wrap but even if we did, I precisely don't want consistency with it :) This method uses the current context when calling ExecutorService.execute, not when calling this method so it's quite different.
|
@kenfinnigan Any thoughts on this other idea I just came up with, |
|
I'm good with
|
|
@trask Thanks, liked |
|
Ooh, I really like |
|
@bogdandrutu This look ok? Will go ahead and merge soon |
|
Throwing more ideas: I am also ok with |
|
Sounds like |
…ext at invocation time.
Some asynchronous instrumentation requires registering an
ExecutorServicestatically which propagates context from invocations to executions. For example, to properly traceOkHttpClientto supportenqueue, we need to do something like thisI thought of names like
currentContextWrapping,wrapWithCurrentContextbut don't know if I like either name so suggestions appreciated.