Ejemplo n.º 1
0
        /// <summary>
        /// Execute the CircuitBreaker function
        /// </summary>
        /// <typeparam name="R">the return type</typeparam>
        /// <param name="input">the input to execute on</param>
        /// <param name="executeDelegate">the delegate to use in Circuit break</param>
        /// <returns>result from execution of delegate (could be null)</returns>
        public async Task <R> Execute <R>(T input, Func <T, R> executeDelegate)
        {
            while (true)
            {
                var result = executeDelegate(input);
                if (!result.Equals(default(R)))
                {
                    Status = CircuitBreakerStatuses.Closed;
                    return(result);
                }
                else
                {
                    ThresholdCount++;
                    if (ThresholdCount >= Threshold)
                    {
                        Status = CircuitBreakerStatuses.Open;
                        break;
                    }

                    await Task.Delay(RetryTimeSpan);
                }
            }

            LastAttempt = DateTimeOffset.Now;

            return(default(R));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Reset the CircuitBreaker (sets status to HalfOpen)
 /// </summary>
 /// <returns>void</returns>
 public async Task Reset()
 {
     await Task.Run(() =>
     {
         Status         = CircuitBreakerStatuses.HalfOpen;
         ThresholdCount = 0;
     });
 }