Travix-International / Hystrix.Dotnet

A combination of circuit breaker and timeout. The .net version of the open source Hystrix library built by Netflix.
https://travix-international.github.io/Hystrix.Dotnet/
MIT License
95 stars 16 forks source link

[Question] Use of CancellationTokenSource #15

Closed HigorCesar closed 6 years ago

HigorCesar commented 7 years ago

Considering that Hystrix handles time out is necessary to call ExecuteAsync with CancellationTokenSource properly configured?

markvincze commented 7 years ago

I think passing in a CancellationToken is only needed if you ever want to cancel the operation from the outside, if you don't want to do that, you can omit the argument. @JorritSalverda is there any situation when it's needed?

JorritSalverda commented 7 years ago

It's to make sure that if the circuit breaker times out before the code it executes does it cancels that code. You use it as follows:

var cancellationTokenSource = new CancellationTokenSource();

var client = new System.Net.Http.HttpClient();

var response = await hystrixCommandFactory.GetHystrixCommand("group", "command").ExecuteAsync(
    async () => await client.GetAsync(url, cancellationTokenSource.Token).ConfigureAwait(false)
    , cancellationTokenSource).ConfigureAwait(false);

So if your circuit breaker has a 1 second timeout and the HttpClient 30 seconds without it it would continue to run up to 30 seconds, whereas with the cancellationtoken it get's cancelled after 1 second.