-
Notifications
You must be signed in to change notification settings - Fork 3.9k
context: Make CancellableContext implement Closeable #3607
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
Conversation
|
CC: @adriancole |
|
Tldr I like this because errorprone can warn on lack of close. Wrt how to
handle exceptions, the example is honest.
Asides:
I know @pauldraper attempted to layer capabilities to handle exceptions
neatly (in a different lib), cleverly with multiple "throws arity"
https://github.com/lucidsoftware/java-thread-context/tree/master/thread-context/src/main/java/com/github/threadcontext/function
cc also @nicmunroe
Ack grpc context isnt java 8 but the approach could be layered with non
java 8 types here or elsewhere. I feel we are getting closer to something
ideal, but try/resources idiom is limited.
One idea is a compiler or codegen which "fixes" the catch vs autoclose
problem. Ex possibly similar to autovalue memoize or retrolambda. In this
way we could eliminate the boilerplate by hiding it somehow. Cc @orfjackal
@raphw @eamonnmcmanus
|
zhangkun83
left a comment
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.
LGTM
| * cancelled. | ||
| * | ||
| * <p>This class must be cleaned up by either calling {@link #close} or {@link #cancel}. | ||
| * {@link #close} is equivalent to calling {@code cancel(null)}. It is safe to call the methods |
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.
Maybe also in cancel()'s javadoc refer back to close().
| * more than once, but only the first call will have any effect. | ||
| * | ||
| * <p>Blocking code can use the try-with-resources idiom: | ||
| * <pre> |
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.
Get rid of the extra newlines. Take a look at the generated documentation to see how it looks.
|
Concerning catching the exception: I think I may encourage people not to (and just Basically there's two different operations at play here: cancellation (cancel with error) and cleanup (cancel with null). My hypothesis is that generally when using try-with-resources (aka, you're using blocking-style APIs), you have no need to cancel, even if an exception was thrown. The only time "you're cancelling" is when the Deadline is reached, which provides an exception. Now, that's not at all true when doing async work. But if you're doing async work you're much less likely to use try-with-resources. |
|
One note is that at least with a trace context (usually not using this form
but as an example) we often use try with because you can end up with an
exception creating or starting a deferred task (ex latter could be a
rejection cause). Not sure if such a pattern applies elsewhere or if there
is a risk in not knowing the cause.
…On 25 Oct 2017 8:29 am, "Eric Anderson" ***@***.***> wrote:
Concerning catching the exception: I think I may encourage people not to
(and just cancel(null)/close()).
Basically there's two different operations at play here: cancellation
(cancel with error) and cleanup (cancel with null). My hypothesis is that
generally when using try-with-resources (aka, you're using blocking-style
APIs), you have no need to cancel, even if an exception was thrown. The
only time "you're cancelling" is when the Deadline is reached, which
provides an exception.
Now, that's not at all true when doing async work. But if you're doing
async work you're much less likely to use try-with-resources.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3607 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD619K1QVp2B_dgyMTZoIXc4ez6KeQDks5svoDTgaJpZM4QDqyZ>
.
|
This achieves two things:
CancellableContextis a resource that must be freedrunAndCancelandcallAndCancel.@ejona86 and I considered adding a try-with-resources API that attaches the CancellableContext before the
try, and both cancels as well as detaches at theclose. But the problem is that the close runs before any catch statements, so users will not be able to use the exception as the cancellation cause unless they use a nested try/catch.We also considered adding a try-with-resources API to attach/detach a context without cancelling, so that the two
trystatements in the example will look more symmetrical. But the syntactic sugar it provides is very minor.Internal use cases that kick off work asynchronously and use
ListenableFuturewill use an internal helper utility. We can not easily open source that code becausegrpc-contextmust have minimal dependencies and can not use guava.