Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - Polly Policy.TimeoutAsync not giving TimeoutRejected exception

Here is my sample code:

var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(_configOptions.ResponseTimeout), TimeoutStrategy.Pessimistic);
            
try
{
    return await timeoutPolicy.ExecuteAsync(async ct => await Somemethod(request, ct), CancellationToken.None);
}
catch (TimeoutRejectedException ex)
{
    _//Some logging to go here/
}

configOptions.ResponseTimeout is configured in config file, I am expecting the method await Somemethod(request, ct), CancellationToken.None should throw TimeoutRejectedException based on the value given in config.

But it is not throwing any exception. Can anyone please help me to identify where I am going wrong?

Help will be much appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Optimistic timeout

The delegate does support cancellation.

The to be executed delegate

private static async Task SomeMethodAsync(CancellationToken ct = default)
{
    Console.WriteLine($"{nameof(SomeMethodAsync)} has been called.");
    await Task.Delay(15000, ct); //It is aware of the CancellationToken
}

The timeout policy

 private static AsyncTimeoutPolicy GetTimeoutPolicy
    => Policy
        .TimeoutAsync(
            TimeSpan.FromMilliseconds(1000),
            TimeoutStrategy.Optimistic,
            (context, timeout, _, exception) =>
            {
                Console.WriteLine($"{"Timeout",-10}{timeout,-10:ss\.fff}: {exception.GetType().Name}");
                return Task.CompletedTask;
            });

The usage

var strategy = GetTimeoutPolicy;
var result = await strategy.ExecuteAsync(async (ct) => await SomeMethodAsync(ct), CancellationToken.None);

The output

SomeMethodAsync has been called.
Timeout   01.000    : TaskCanceledException
Unhandled exception. Polly.Timeout.TimeoutRejectedException: The delegate executed asynchronously through TimeoutPolicy did not complete within the timeout.
 ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
...

Pessimistic timeout

The delegate does not support cancellation.

The to be executed delegate

private static async Task SomeMethodAsync(CancellationToken ct = default)
{
    Console.WriteLine($"{nameof(SomeMethodAsync)} has been called.");
    await Task.Delay(15000); //It is NOT aware of the CancellationToken
}

The timeout policy

 private static AsyncTimeoutPolicy GetTimeoutPolicy
    => Policy
        .TimeoutAsync(
            TimeSpan.FromMilliseconds(1000),
            TimeoutStrategy.Pessimistic,
            (context, timeout, _, exception) =>
            {
                Console.WriteLine($"{"Timeout",-10}{timeout,-10:ss\.fff}: {exception.GetType().Name}");
                return Task.CompletedTask;
            });

The usage

var strategy = GetTimeoutPolicy;
var result = await strategy.ExecuteAsync(async () => await SomeMethodAsync());

The output

SomeMethodAsync has been called.
Timeout   01.000    : TaskCanceledException
Unhandled exception. Polly.Timeout.TimeoutRejectedException: The delegate executed asynchronously through TimeoutPolicy did not complete within the timeout.
 ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...