예제 #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);
 }