TimeoutResilienceStrategy
using Polly.Telemetry;
using Polly.Utils;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Polly.Timeout
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal sealed class TimeoutResilienceStrategy : ResilienceStrategy
{
private readonly ResilienceStrategyTelemetry _telemetry;
private readonly CancellationTokenSourcePool _cancellationTokenSourcePool;
public TimeSpan DefaultTimeout { get; }
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0
})]
[field: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0
})]
public Func<TimeoutGeneratorArguments, ValueTask<TimeSpan>> TimeoutGenerator {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0
})]
get;
}
[System.Runtime.CompilerServices.Nullable(2)]
[field: System.Runtime.CompilerServices.Nullable(2)]
public Func<OnTimeoutArguments, ValueTask> OnTimeout {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
}
public TimeoutResilienceStrategy(TimeoutStrategyOptions options, TimeProvider timeProvider, ResilienceStrategyTelemetry telemetry)
{
DefaultTimeout = options.Timeout;
TimeoutGenerator = options.TimeoutGenerator;
OnTimeout = options.OnTimeout;
_telemetry = telemetry;
_cancellationTokenSourcePool = CancellationTokenSourcePool.Create(timeProvider);
}
[AsyncStateMachine(typeof(<ExecuteCore>d__12<, >))]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
1
})]
protected internal override ValueTask<Outcome<TResult>> ExecuteCore<[System.Runtime.CompilerServices.Nullable(2)] TResult, [System.Runtime.CompilerServices.Nullable(2)] TState>([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
1,
0,
0,
1
})] Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback, ResilienceContext context, TState state)
{
<ExecuteCore>d__12<TResult, TState> stateMachine = default(<ExecuteCore>d__12<TResult, TState>);
stateMachine.<>t__builder = AsyncValueTaskMethodBuilder<Outcome<TResult>>.Create();
stateMachine.<>4__this = this;
stateMachine.callback = callback;
stateMachine.context = context;
stateMachine.state = state;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
private static CancellationTokenRegistration CreateRegistration(CancellationTokenSource cancellationSource, CancellationToken previousToken)
{
if (previousToken.CanBeCanceled)
return previousToken.Register(delegate(object state) {
((CancellationTokenSource)state).Cancel();
}, cancellationSource, false);
return default(CancellationTokenRegistration);
}
}
}