Пример #1
0
        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);
            }
        }
Пример #4
0
        void ICircuitBreakerSwitch.OpenCircuit(ICircuitBreakerState from)
        {
            var tripped = TryToTrip(from, _openedState);

            if (tripped)
            {
                EventHandler?.OnCircuitOpened(this);
            }
        }
Пример #5
0
 private bool TryToTrip(ICircuitBreakerState from, ICircuitBreakerState to)
 {
     if (Interlocked.CompareExchange(ref _currentState, to, from) == from)
     {
         to.Enter();
         return(true);
     }
     return(false);
 }
Пример #6
0
 // 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;
 }
Пример #7
0
 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);
         }
     }
 }
Пример #8
0
 private void AttemptReset()
 {
     lock (StateTransitionLockerObject)
     {
         // Must be from open.
         if (state == openState)
         {
             StopTimer();
             state = halfOpenState;
             HalfOpen?.Invoke(this, EventArgs.Empty);
         }
     }
 }
Пример #9
0
 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();
         }
     }
 }
Пример #10
0
        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();
        }
Пример #11
0
        public void InvokeThrough(ICircuitBreakerState state, Action action, TimeSpan timeout)
        {
            try
            {
                Invoke(action, timeout);
            }
            catch (System.Exception)
            {
                state.InvocationFails();
                throw;
            }

            state.InvocationSucceeds();
        }
Пример #12
0
        public async Task InvokeThroughAsync(ICircuitBreakerState state, Func<Task> func, TimeSpan timeout)
        {
            try
            {
                await InvokeAsync(func, timeout);
            }
            catch (System.Exception)
            {
                state.InvocationFails();
                throw;
            }

            state.InvocationSucceeds();
        }
Пример #13
0
 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
             }
         }
     });
 }
Пример #14
0
        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;
        }
Пример #15
0
		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;
			}
		}
Пример #16
0
		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;
			}
		}
Пример #17
0
        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;
        }
Пример #18
0
        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;
            }
        }
Пример #20
0
        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;
            }
        }
Пример #21
0
        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;
            }
        }
Пример #22
0
        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;
            }
        }
Пример #23
0
 public void HalfCloseCircuit(ICircuitBreakerState from)
 {
     SwitchState(from, _halfOpen);
 }
Пример #24
0
        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;
            }
        }
Пример #25
0
 public void OpenCircuit(ICircuitBreakerState from)
 {
     SwitchState(from, _open);
 }
 public CircultBreaker(ICircuitBreakerState state)
 {
     _state = state;
 }
Пример #27
0
 void ICircuitBreakerSwitch.CloseCircuit(ICircuitBreakerState from)
 {
     Trip(from, _closedState);
 }
Пример #28
0
 public Entry(ICircuitBreakerState circuitBreakerState, Task task)
 {
     CircuitBreakerState = circuitBreakerState;
     Task = task;
 }
Пример #29
0
 void ICircuitBreakerSwitch.AttemptToCloseCircuit(ICircuitBreakerState from)
 {
     Trip(from, _halfOpenedState);
 }
 public CircuitBreakerStateLock(ICircuitBreakerState inner)
 {
     _inner = inner;
 }
Пример #31
0
 public CircuitBreaker(ICircuitBreakerState circuitBreakerState)
 {
     _circuitBreakerstate = circuitBreakerState;
 }
Пример #32
0
        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;
            }
        }
Пример #33
0
 public void CloseCircuit(ICircuitBreakerState from)
 {
     SwitchState(from, _closed);
 }
 public CircuitBreakerStateLock(ICircuitBreakerState inner)
 {
     _inner = inner;
 }
Пример #35
0
 void ICircuitBreakerSwitch.OpenCircuit(ICircuitBreakerState from)
 {
     Trip(from, _openedState);
 }