Where is the Nancy CancellationToken for async request handlers coming from and when is it canceled?

This is more a question of how and when Nancy can cancel the asynchronous request handler through the provided cancelationToken instance.

Basically, what interests me is, in what conditions is the token .IsCancellationRequested set to true - is this documented or explained somewhere?

How does Nancy handle async handlers that "never" return / at "time"? And with respect to "in time": is there a timeout / limit after which the handlers will be canceled?

+7
source share
3 answers

I know this is an old question, but I was in your situation, and I think I found the answer even if you use Owin to host your application (using Nancy.Owin ).

CancellationToken comes directly from Owin through the IOwinRequest.CallCancelled property ( Nancy's source code is used here ). This token can be set by Owin if the request is canceled (for example, by force closing the HTTP connection).

+3
source

Each CancellationToken comes from somewhere, and that somewhere there is its CancellationTokenSource.

When you call CancellationTokenSource.Cancel, each token created from it is marked.


Fun fact: CancellationToken is a structure that means that every time you pass it to a function or assign it to a variable, it creates a new copy. Since the source cannot track all of these copies, we cannot have the CancellationToken.IWasCancelled event. Instead, when you call IsCancellationRequested, the token should request its source.

ref: https://msdn.microsoft.com/en-us/library/system.threading.cancellationtokensource(v=vs.110).aspx


So, back to Nancy, looking for their source code for the CancellationTokenSource, and you will find the answer. Here is just the one I've seen.

https://github.com/NancyFx/Nancy/blob/8a29b0495bfac4806536327c4d78de1ee59bd513/src/Nancy/NancyEngine.cs

+1
source

With the beauty of undo markers, you do not need to know or care about how they are set. It all depends on the person providing you the token. You just look at the token and see if it is installed.

If you call a method that accepts a CancellationToken , and you want to know how to create it, which you can set when you want, then you should use the CancellationTokenSource to create the token; you can use CTS to cancel the token that it generates, or set it to cancel after a certain period of time.

0
source

Source: https://habr.com/ru/post/988597/


All Articles