CircuitBreakerManualControl
Allows manual control of the circuit-breaker.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Polly.CircuitBreaker
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class CircuitBreakerManualControl
{
[System.Runtime.CompilerServices.NullableContext(0)]
private class RegistrationDisposable : IDisposable
{
[System.Runtime.CompilerServices.Nullable(1)]
private readonly Action _disposeAction;
[System.Runtime.CompilerServices.NullableContext(1)]
public RegistrationDisposable(Action disposeAction)
{
_disposeAction = disposeAction;
}
public void Dispose()
{
_disposeAction();
}
}
private readonly object _lock = new object();
private readonly HashSet<Func<ResilienceContext, Task>> _onIsolate = new HashSet<Func<ResilienceContext, Task>>();
private readonly HashSet<Func<ResilienceContext, Task>> _onReset = new HashSet<Func<ResilienceContext, Task>>();
private bool _isolated;
internal bool IsEmpty => _onIsolate.Count == 0;
public CircuitBreakerManualControl()
{
}
public CircuitBreakerManualControl(bool isIsolated)
{
_isolated = isIsolated;
}
internal IDisposable Initialize(Func<ResilienceContext, Task> onIsolate, Func<ResilienceContext, Task> onReset)
{
lock (_lock) {
_onIsolate.Add(onIsolate);
_onReset.Add(onReset);
if (_isolated) {
ResilienceContext context = ResilienceContextPool.Shared.Get(default(CancellationToken)).Initialize<VoidResult>(true);
IsolateAsync(context).GetAwaiter().GetResult();
}
return new RegistrationDisposable(delegate {
lock (_lock) {
_onIsolate.Remove(onIsolate);
_onReset.Remove(onReset);
}
});
}
}
[AsyncStateMachine(typeof(<IsolateAsync>d__9))]
internal Task IsolateAsync(ResilienceContext context)
{
<IsolateAsync>d__9 stateMachine = default(<IsolateAsync>d__9);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.context = context;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<IsolateAsync>d__10))]
public Task IsolateAsync(CancellationToken cancellationToken = default(CancellationToken))
{
<IsolateAsync>d__10 stateMachine = default(<IsolateAsync>d__10);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<CloseAsync>d__11))]
internal Task CloseAsync(ResilienceContext context)
{
<CloseAsync>d__11 stateMachine = default(<CloseAsync>d__11);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.context = context;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<CloseAsync>d__12))]
public Task CloseAsync(CancellationToken cancellationToken = default(CancellationToken))
{
<CloseAsync>d__12 stateMachine = default(<CloseAsync>d__12);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
}
}