Repository navigation
Passing the cancellation token to the ReadBuffer - #3158
Conversation
|
@vonzshik the plan sounds good to me - no problem to split the PRs.
Socket cancellation support was added to .NET Core 3.0, so previous versions of .NET Core are also affected. Also, ThrowIfCancellationRequested is considered good practice regardless of whether socket cancellation is supported or not, to abort earlier etc. In general, we do already do this at the upper layer API surface. In methods without the if (cancellationToken.IsCancellationRequested)
return Task.FromCanceled<T>(cancellationToken);But we can definitely go over the code to make sure it's done everywhere. |
roji
left a comment
There was a problem hiding this comment.
Before giving this a full review, here are some general comments:
- Any reason not to put the new CancellationToken parameter at the end, with a default of default which is the same as CancellationToken.None? Both seem to be the standard for this kind of thing. If so, this needs to be done across the whole codebase.
- If we do the above, the cancellation token becomes optional, and methods which don't pass it (e.g. sync ones) can omit it.
(BTW I hope you're using a good IDE which allows for advanced refactoring, Jetbrains can change parameter order across the entire type hierarchy in one op)
YohDeadfall
left a comment
There was a problem hiding this comment.
While @roji required changes, I'm happy with the current design. The common way doesn't mean the right one, because the devil is in details, so let's list them:
- Since it's internal, the
cancellationTokencannot be optional. Otherwise, the code will be error prone and we don't need so (Shay too, but he did a very quick review (: ). - Because the parameter isn't optional somewhere it won't be the last. Mixed style is what we would like avoid too, it makes code not fluent and increases threshold for new contributors because we require from them keep the current style, but at the same time there is no current, but many styles.
- There is the
asyncparameter. HavingcancellationTokenfollowing it groups them by meaning. It's a plus.
If I would make the change, it would be exactly like yours.
Why not?
I agree that if the parameter isn't optional, it can't be the last everywhere. But:
That's a pretty weak argument IMHO for not following standard C# practices... To summarize, there's not only a C# standard here - there's also compiler analysis warnings for making sure it is that way, and preventing the common pitfalls of forgetting to pass it along. I just don't see a reason not to follow it. |
Easy to forget somewhere and get a bug. Strongly disagreed with you. See @Brar's comment.
Yep, but it's a plus. |
Have you seen my comment above? #3158 (comment) |
|
I have seen, but let's not make things optional when they are not intended to be optional. It's an internal API and the parameter must be set mostly always. Therefore, I don't see how it could be optional. |
I'm still not understanding the reason not to have it optional, given that the compiler can catch errors for us. As you say, the parameter is mostly set, but not always - there are sync methods which don't need to pass it, for example. The methods do not require the cancellation token - they can work fine without it - so that means it's optional no? This is also about respecting common C# coding guidelines, so that when new people come in, everything works the way they're used to. I'm fine with not following standards/guidelines, but we should do that when there's a good reason to do that, and I'm not sure I see that here... |
|
FYI opened #3162 to track setting up analyzers as appropriate. |
Brar
left a comment
There was a problem hiding this comment.
I think the discussion about whether the cancellation token should have a default value or not on our internal interfaces, making them a bit more verbose either in the signature or at the call site isn't really worth our time.
Since we're talking about an internal interface we are completely free to change it whenever we feel like it and if there's a new standard that just evolved, we can choose to follow it immediately (opt into the optional warnings now) or do it once the dust around it has settled (opt into the optional warnings when we think the time has come to do it).
Mind you this isn't about correctness of implementation but rather about intent/taste and I guess there are not much more than three people at all, who care about intent/style of internal Npgsql-interfaces.
Personally I'm willing to accept it this way or that way.
roji
left a comment
There was a problem hiding this comment.
Starting to look close to done, but please look the additional comments below. BTW it's fine to introduce new overloads to minimize the need for named parameters if those are used frequently.
roji
left a comment
There was a problem hiding this comment.
Almost there :) Just a few nits remaining.
|
@roji yep, my mistake - fixed. And thank you again for reviews. Now, unto the cancellation for the writing part... |
|
Thank you @vonzshik! |
|
@Brar you kind of done the most of it - my part was not that big 😊 |
Related to #2437.
As this pr is already pretty big, I think it should be split in 3 parts:
Also, might be worth to add some
CancellationToken.ThrowIfCancellationRequestedcalls for the sake of .net 4.6.1. (which doesn't support the cancellation on the socket layer).