CircuitStateController<TResult>
using Polly.Utilities;
using System;
using System.Threading;
namespace Polly.CircuitBreaker
{
internal abstract class CircuitStateController<TResult> : ICircuitController<TResult>
{
protected readonly TimeSpan _durationOfBreak;
protected readonly Action<DelegateResult<TResult>, CircuitState, TimeSpan, Context> _onBreak;
protected readonly Action<Context> _onReset;
protected readonly Action _onHalfOpen;
protected readonly object _lock = new object();
protected long _blockedTill;
protected CircuitState _circuitState;
protected DelegateResult<TResult> _lastOutcome;
public CircuitState CircuitState {
get {
if ((int)_circuitState != 1)
return _circuitState;
using (TimedLock.Lock(_lock)) {
if ((int)_circuitState == 1 && !IsInAutomatedBreak_NeedsLock) {
_circuitState = 2;
_onHalfOpen();
}
return _circuitState;
}
}
}
public Exception LastException {
get {
using (TimedLock.Lock(_lock)) {
DelegateResult<TResult> lastOutcome = _lastOutcome;
return (lastOutcome != null) ? lastOutcome.Exception : null;
}
}
}
public TResult LastHandledResult {
get {
using (TimedLock.Lock(_lock))
return (_lastOutcome != null) ? _lastOutcome.Result : default(TResult);
}
}
protected bool IsInAutomatedBreak_NeedsLock => SystemClock.UtcNow().Ticks < _blockedTill;
protected CircuitStateController(TimeSpan durationOfBreak, Action<DelegateResult<TResult>, CircuitState, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)
{
_durationOfBreak = durationOfBreak;
_onBreak = onBreak;
_onReset = onReset;
_onHalfOpen = onHalfOpen;
_circuitState = 0;
Reset();
}
public void Isolate()
{
using (TimedLock.Lock(_lock)) {
_lastOutcome = new DelegateResult<TResult>((Exception)new IsolatedCircuitException("The circuit is manually held open and is not allowing calls."));
BreakFor_NeedsLock(TimeSpan.MaxValue, Context.None());
_circuitState = 3;
}
}
protected void Break_NeedsLock(Context context)
{
BreakFor_NeedsLock(_durationOfBreak, context);
}
private void BreakFor_NeedsLock(TimeSpan durationOfBreak, Context context)
{
_blockedTill = ((durationOfBreak > DateTime.MaxValue - SystemClock.UtcNow()) ? DateTime.MaxValue.Ticks : (SystemClock.UtcNow() + durationOfBreak).Ticks);
CircuitState circuitState = _circuitState;
_circuitState = 1;
_onBreak(_lastOutcome, circuitState, durationOfBreak, context);
}
public void Reset()
{
OnCircuitReset(Context.None());
}
protected void ResetInternal_NeedsLock(Context context)
{
_blockedTill = DateTime.MinValue.Ticks;
_lastOutcome = null;
CircuitState circuitState = _circuitState;
_circuitState = 0;
if ((int)circuitState != 0)
_onReset(context);
}
protected bool PermitHalfOpenCircuitTest()
{
long blockedTill = _blockedTill;
DateTime dateTime = SystemClock.UtcNow();
if (dateTime.Ticks < blockedTill)
return false;
ref long blockedTill2 = ref _blockedTill;
dateTime = SystemClock.UtcNow();
return Interlocked.CompareExchange(ref blockedTill2, dateTime.Ticks + _durationOfBreak.Ticks, blockedTill) == blockedTill;
}
private BrokenCircuitException GetBreakingException()
{
DelegateResult<TResult> lastOutcome = _lastOutcome;
if (lastOutcome == null)
return new BrokenCircuitException("The circuit is now open and is not allowing calls.");
if (lastOutcome.Exception != null)
return new BrokenCircuitException("The circuit is now open and is not allowing calls.", lastOutcome.Exception);
return new BrokenCircuitException<TResult>("The circuit is now open and is not allowing calls.", lastOutcome.Result);
}
public void OnActionPreExecute()
{
CircuitState circuitState = CircuitState;
switch ((int)circuitState) {
case 0:
break;
case 2:
if (!PermitHalfOpenCircuitTest())
throw GetBreakingException();
break;
case 1:
throw GetBreakingException();
case 3:
throw new IsolatedCircuitException("The circuit is manually held open and is not allowing calls.");
default:
throw new InvalidOperationException("Unhandled CircuitState.");
}
}
public abstract void OnActionSuccess(Context context);
public abstract void OnActionFailure(DelegateResult<TResult> outcome, Context context);
public abstract void OnCircuitReset(Context context);
}
}