internal static async Task ImplementationAsync(Func<Task> action, IEnumerable<ExceptionPredicate> shouldRetryPredicates, ICircuitBreakerState breakerState, bool continueOnCapturedContext) { if (breakerState.IsBroken) { throw new BrokenCircuitException("The circuit is now open and is not allowing calls.", breakerState.LastException); } try { await action().ConfigureAwait(continueOnCapturedContext); breakerState.Reset(); } catch (Exception ex) { if (!shouldRetryPredicates.Any(predicate => predicate(ex))) { throw; } breakerState.TryBreak(ex); throw; } }
void ICircuitBreakerSwitch.CloseCircuit(ICircuitBreakerState from) { var tripped = TryToTrip(from, _closedState); if (tripped) { EventHandler?.OnCircuitClosed(this); } }
void ICircuitBreakerSwitch.AttemptToCloseCircuit(ICircuitBreakerState from) { var tripped = TryToTrip(from, _halfOpenedState); if (tripped) { EventHandler?.OnCircuitHalfOpened(this); } }
void ICircuitBreakerSwitch.OpenCircuit(ICircuitBreakerState from) { var tripped = TryToTrip(from, _openedState); if (tripped) { EventHandler?.OnCircuitOpened(this); } }
private bool TryToTrip(ICircuitBreakerState from, ICircuitBreakerState to) { if (Interlocked.CompareExchange(ref _currentState, to, from) == from) { to.Enter(); return(true); } return(false); }
// The constructor. public CircuitBreaker(int failureCountThreshold, int openTimeoutInSeconds) { state = closedState; failureCount = 0L; failureThreshold = failureCountThreshold; openTimeoutSeconds = openTimeoutInSeconds; timer = new System.Timers.Timer(1000); timer.Elapsed += TimerElapsed; timerSecondsElapsed = 0L; }
private void Reset() { lock (StateTransitionLockerObject) { // Must be from half-open. if (state == halfOpenState) { Interlocked.Exchange(ref failureCount, 0L); state = closedState; Closed?.Invoke(this, EventArgs.Empty); } } }
private void AttemptReset() { lock (StateTransitionLockerObject) { // Must be from open. if (state == openState) { StopTimer(); state = halfOpenState; HalfOpen?.Invoke(this, EventArgs.Empty); } } }
private void TripBreaker() { lock (StateTransitionLockerObject) { // Must be from closed or half-open. if (state != openState) { Interlocked.Exchange(ref failureCount, 0L); state = openState; Open?.Invoke(this, EventArgs.Empty); RestartTimer(); } } }
public async Task InvokeThroughAsync(ICircuitBreakerState state, Func <Task> func, TimeSpan timeout, CancellationToken cancellationToken = default(CancellationToken)) { try { await InvokeAsync(func, timeout, cancellationToken); } catch (Exception) { state.InvocationFails(); throw; } state.InvocationSucceeds(); }
public void InvokeThrough(ICircuitBreakerState state, Action action, TimeSpan timeout) { try { Invoke(action, timeout); } catch (System.Exception) { state.InvocationFails(); throw; } state.InvocationSucceeds(); }
public async Task InvokeThroughAsync(ICircuitBreakerState state, Func<Task> func, TimeSpan timeout) { try { await InvokeAsync(func, timeout); } catch (System.Exception) { state.InvocationFails(); throw; } state.InvocationSucceeds(); }
private Metric CreateCounter(ICircuitBreakerState circuitBreakerState) { return(new Metric { gauge = new Gauge { value = circuitBreakerState.ClosedPct, }, label = new List <LabelPair> { new LabelPair { name = "policy", value = circuitBreakerState.Policy } } }); }
public T InvokeThrough<T>(ICircuitBreakerState state, Func<T> func, TimeSpan timeout) { T result; try { result = Invoke(func, timeout); } catch (System.Exception) { state.InvocationFails(); throw; } state.InvocationSucceeds(); return result; }
internal static async Task ImplementationAsync( Func< Task > action, ExceptionHandler canHandle, ICircuitBreakerState breaker ) { if( breaker.IsBroken ) throw breaker.LastException; try { await action().ConfigureAwait( false ); breaker.Reset(); } catch( Exception ex ) { if( !canHandle( ex ) ) throw; breaker.TryBreak( ex ); throw; } }
internal static void Implementation( Action action, ExceptionHandler canHandle, ICircuitBreakerState breaker ) { if( breaker.IsBroken ) throw breaker.LastException; try { action(); breaker.Reset(); } catch( Exception ex ) { if( !canHandle( ex ) ) throw; breaker.TryBreak( ex ); throw; } }
public async Task<T> InvokeThroughAsync<T>(ICircuitBreakerState state, Func<Task<T>> func, TimeSpan timeout) { Task<T> task; try { task = InvokeAsync(func, timeout); await task; } catch (System.Exception) { state.InvocationFails(); throw; } state.InvocationSucceeds(); return await task; }
public async Task <T> InvokeThroughAsync <T>(ICircuitBreakerState state, Func <Task <T> > func, TimeSpan timeout, CancellationToken cancellationToken = default(CancellationToken)) { Task <T> task; try { task = InvokeAsync(func, timeout, cancellationToken); await task; } catch (Exception) { state.InvocationFails(); throw; } state.InvocationSucceeds(); return(await task); }
internal static async Task ImplementationAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken, IEnumerable<ExceptionPredicate> shouldHandlePredicates, ICircuitBreakerState breakerState, bool continueOnCapturedContext) { cancellationToken.ThrowIfCancellationRequested(); if (breakerState.IsBroken) { throw new BrokenCircuitException("The circuit is now open and is not allowing calls.", breakerState.LastException); } try { await action(cancellationToken).ConfigureAwait(continueOnCapturedContext); breakerState.Reset(); } catch (Exception ex) { if (cancellationToken.IsCancellationRequested) { if (ex is OperationCanceledException && ((OperationCanceledException) ex).CancellationToken == cancellationToken) { throw; } cancellationToken.ThrowIfCancellationRequested(); } if (!shouldHandlePredicates.Any(predicate => predicate(ex))) { throw; } breakerState.TryBreak(ex); throw; } }
internal static void Implementation(Action action, IEnumerable<ExceptionPredicate> shouldRetryPredicates, ICircuitBreakerState breakerState) { if (breakerState.IsBroken) { throw new BrokenCircuitException("The circuit is now open and is not allowing calls.", breakerState.LastException); } try { action(); breakerState.Reset(); } catch (Exception ex) { if (!shouldRetryPredicates.Any(predicate => predicate(ex))) { throw; } breakerState.TryBreak(ex); throw; } }
internal static async Task ImplementationAsync(Func <Task> action, IEnumerable <ExceptionPredicate> shouldRetryPredicates, ICircuitBreakerState breakerState) { if (breakerState.IsBroken) { throw new BrokenCircuitException("The circuit is now open and is not allowing calls.", breakerState.LastException); } try { await action().NotOnCapturedContext(); breakerState.Reset(); } catch (Exception ex) { if (!shouldRetryPredicates.Any(predicate => predicate(ex))) { throw; } breakerState.TryBreak(ex); throw; } }
internal static void Implementation(Action action, ExceptionHandler canHandle, ICircuitBreakerState breaker) { if (breaker.IsBroken) { throw breaker.LastException; } try { action(); breaker.Reset(); return; } catch (Exception ex) { if (!canHandle(ex)) { throw; } breaker.TryBreak(ex); throw; } }
public void HalfCloseCircuit(ICircuitBreakerState from) { SwitchState(from, _halfOpen); }
internal static void Implementation(Action action, IEnumerable <ExceptionPredicate> shouldRetryPredicates, ICircuitBreakerState breakerState) { if (breakerState.IsBroken) { throw new BrokenCircuitException("The circuit is now open and is not allowing calls.", breakerState.LastException); } try { action(); breakerState.Reset(); } catch (Exception ex) { if (!shouldRetryPredicates.Any(predicate => predicate(ex))) { throw; } breakerState.TryBreak(ex); throw; } }
public void OpenCircuit(ICircuitBreakerState from) { SwitchState(from, _open); }
public CircultBreaker(ICircuitBreakerState state) { _state = state; }
void ICircuitBreakerSwitch.CloseCircuit(ICircuitBreakerState from) { Trip(from, _closedState); }
public Entry(ICircuitBreakerState circuitBreakerState, Task task) { CircuitBreakerState = circuitBreakerState; Task = task; }
void ICircuitBreakerSwitch.AttemptToCloseCircuit(ICircuitBreakerState from) { Trip(from, _halfOpenedState); }
public CircuitBreakerStateLock(ICircuitBreakerState inner) { _inner = inner; }
public CircuitBreaker(ICircuitBreakerState circuitBreakerState) { _circuitBreakerstate = circuitBreakerState; }
internal static async Task ImplementationAsync(Func <CancellationToken, Task> action, CancellationToken cancellationToken, IEnumerable <ExceptionPredicate> shouldHandlePredicates, ICircuitBreakerState breakerState, bool continueOnCapturedContext) { cancellationToken.ThrowIfCancellationRequested(); if (breakerState.IsBroken) { throw new BrokenCircuitException("The circuit is now open and is not allowing calls.", breakerState.LastException); } try { await action(cancellationToken).ConfigureAwait(continueOnCapturedContext); breakerState.Reset(); } catch (Exception ex) { if (cancellationToken.IsCancellationRequested) { if (ex is OperationCanceledException && ((OperationCanceledException)ex).CancellationToken == cancellationToken) { throw; } cancellationToken.ThrowIfCancellationRequested(); } if (!shouldHandlePredicates.Any(predicate => predicate(ex))) { throw; } breakerState.TryBreak(ex); throw; } }
public void CloseCircuit(ICircuitBreakerState from) { SwitchState(from, _closed); }
void ICircuitBreakerSwitch.OpenCircuit(ICircuitBreakerState from) { Trip(from, _openedState); }