public static void LettingItFail()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region lettingItFail
            // An exception is expected in this demo.
            int rowsWritten = errorProneCode.QueryTheDatabase();
            Console.WriteLine($"Received a response of {rowsWritten}.");

            #endregion
        }
        public static void Timeout()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region timeout
            // A TimeoutRejectedException is expected in this demo.

            var timeoutPolicy = Policy.Timeout(1, TimeoutStrategy.Pessimistic, OnTimeout);

            int result = timeoutPolicy.Execute(() => errorProneCode.SomeSlowComplexProcess());
            Console.WriteLine($"{result}");
            #endregion
        }
        public static void FallingBack()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region fallingBack

            FallbackPolicy fallbackPolicy = Policy.Handle <Exception>()
                                            .Fallback(() => PageAnAdmin());

            fallbackPolicy.Execute(() => errorProneCode.MakeRequestToADeadService());

            #endregion
        }
        public static void FallingBackAndReturningADefault()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region fallingBackAndReturningADefault

            FallbackPolicy <int> fallbackPolicy = Policy <int>
                                                  .Handle <Exception>()
                                                  .Fallback <int>(0);

            int quantity = fallbackPolicy.Execute(() => errorProneCode.GetQuantityAvailable());
            Console.WriteLine($"{quantity} items available.");
            #endregion
        }
        public static void RetryIfException()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region retryIfException

            RetryPolicy retryPolicy = Policy.Handle <Exception>()
                                      .Retry(3, (exception, retryCount) =>
            {
                Console.WriteLine($"{exception.GetType()} thrown, retrying {retryCount}.");
            });

            int result = retryPolicy.Execute(() => errorProneCode.QueryTheDatabase());

            Console.WriteLine($"Received a response of {result}.");
            #endregion
        }
        public static void RetryIfIncorrectStatus()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region retryIfIncorrectStatus

            RetryPolicy <Status> retryPolicy = Policy.HandleResult <Status>(s => s != Status.Success)
                                               .Retry(3, (response, retryCount) =>
            {
                Console.WriteLine($"Received a response of {response.Result}, retrying {retryCount}.");
            });

            Status result = retryPolicy.Execute(() => errorProneCode.GetStatus());

            Console.WriteLine($"Received a response of {result}");
            #endregion
        }
        public static void RetryIfIncorrectStatusOrException()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region retryIfIncorrectStatusOrException

            RetryPolicy <Status> retryPolicy = Policy.HandleResult <Status>(s => s != Status.Success)
                                               .Or <Exception>()
                                               .Retry(3, (responseOrException, retryCount, ctx) =>
            {
                Console.WriteLine($"Request failed, retrying {retryCount}.");
            });

            Status result = retryPolicy.Execute(() => errorProneCode.CallRemoteService());

            Console.WriteLine($"Received a response of {result}.");
            #endregion
        }
        public static void Caching()
        {
            ErrorProneCode errorProneCode      = new ErrorProneCode();
            var            memoryCache         = new MemoryCache(new MemoryCacheOptions());
            var            memoryCacheProvider = new MemoryCacheProvider(memoryCache);

            #region caching
            CachePolicy <int> cachePolicy =
                Policy.Cache <int>(memoryCacheProvider, TimeSpan.FromSeconds(1.5));

            for (int loop = 1; loop <= 10; loop++)
            {
                int result = cachePolicy.Execute(context => errorProneCode.GetSomeNumberThatMightBeCacheable(), new Context("ContextKey"));

                Console.WriteLine($"result={result}. cachePolicy executed {loop} time(s). GetSomeNumberThatMightBeCacheable method really called {result} time(s).");
                Thread.Sleep(500);
            }

            #endregion
        }
        public static void WaitAndRetry()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region waitAndRetry

            RetryPolicy <Status> retryPolicy = Policy.HandleResult <Status>(s => s != Status.Success)
                                               .WaitAndRetry(3,
                                                             sleepDurationProvider: (retryCount) => TimeSpan.FromSeconds(retryCount), // sleep a second longer after each retry
                                                             onRetry: (response, delay, retryCount, ctx) =>
            {
                Console.WriteLine($"Received a response of {response.Result}.");
                Console.WriteLine($"Slept for {delay.Seconds} second(s) before retrying.");
            });

            Status result = retryPolicy.Execute(() => errorProneCode.GetStatus());

            Console.WriteLine($"Received a response of {result}.");
            #endregion
        }
Exemple #10
0
        public static void BasicCircuitBreaker()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region basicCircuitBreaker
            // A BrokenCircuitException is expected in this demo.

            var circuitBreakerPolicy = Policy
                                       .HandleResult <Status>(r => r == Status.Fail)
                                       .CircuitBreaker(2, TimeSpan.FromSeconds(3), OnBreak, OnReset, OnHalfOpen);

            for (int loop = 1; loop <= 3; loop++)
            {
                Status statusAResult = circuitBreakerPolicy.Execute(() => errorProneCode.TargetA());
                Console.WriteLine($"Target A (call {loop}) status: {statusAResult}.");
                Status statusBResult = circuitBreakerPolicy.Execute(() => errorProneCode.TargetB());
                Console.WriteLine($"Target B (call {loop}) status: {statusBResult}.");
            }

            #endregion
        }
Exemple #11
0
        public static void Wrap()
        {
            ErrorProneCode errorProneCode = new ErrorProneCode();

            #region wrap

            RetryPolicy <Status> retryPolicy = Policy.HandleResult <Status>(s => s != Status.Success)
                                               .Retry(3, (response, retryCount) =>
            {
                Console.WriteLine($"Received a response of {response.Result}, retrying {retryCount}.");
            });

            var fallbackPolicy = Policy.HandleResult <Status>(s => s == Status.Fail)
                                 .Fallback(() => Status.Unknown);

            var wrapPolicy = Policy.Wrap(fallbackPolicy, retryPolicy);

            Status result = wrapPolicy.Execute(() => errorProneCode.GetOtherStatus());

            Console.WriteLine($"Status: {result}.");

            #endregion
        }
Exemple #12
0
        public static async Task Bulkhead()
        {
            ErrorProneCode complexCode = new ErrorProneCode();
            List <Task>    tasks       = new List <Task>();

            #region bulkhead

            AsyncBulkheadPolicy bulkheadPolicyAsync = Policy.BulkheadAsync(1, 2, OnBulkheadRejectedAsync);

            for (int i = 1; i <= 10; i++)
            {
                JobProcessor(i);   // simulate sending requests
                Thread.Sleep(500); // with delays between each request
            }

            void JobProcessor(int num) //
            {
                Console.WriteLine($"Execution slots: {bulkheadPolicyAsync.BulkheadAvailableCount}, Queue Slots: {bulkheadPolicyAsync.QueueAvailableCount}");
                tasks.Add(bulkheadPolicyAsync.ExecuteAsync(async() => await complexCode.SomeSlowComplexProcessAsync(num)));
            }

            #endregion

            try
            {
                await Task.WhenAll(tasks);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\nSome bulkhead tasks failed with exception: {ex.Message}\n");
            }

            for (int loop = 0; loop < 10; loop++)
            {
                Console.WriteLine($"Task {loop}: {tasks[loop].Status}");
            }
        }