Using Polly with Any Type of Request

Full source code here.

Want to learn more about Polly? Check out my Pluralsight course on it.

I recently presented a talk on Polly at the DevUp conference in St. Louis. In the presentation I showed examples using Polly with HttpClient requests only because this is my most common use case. I showed how to use retries, circuit breakers, fallbacks, caching, timeouts and bulkheads isolation.

At the end of the presentation a few people asked me if Polly could be used with other types of requests, of course it can and I should have said so during the talk. I’ve added a new slide for the next time I present on Polly - (as of October 2018 that will be Granite State Code Camp, Boston Code Camp, and NDC London).

Polly can be used with any type of request you make, whether it returns a value or not. You can use Polly on code that throws exceptions from time to time (though I strongly recommend fixing your code), for database calls, any method call for that matter. Below are some examples of using the retry policy in a variety of scenarios.

Exceptions

This policy retries if an exception is thrown, you can be more specific about exception types.

1ErrorProneCode errorProneCode = new ErrorProneCode();
2
3RetryPolicy retryIfException = Policy.Handle<Exception>()
4    .Retry(4, (exception, retryCount) =>
5    {
6        Console.WriteLine($"Got a response of {exception.Message} (expected 0), retrying {retryCount}");
7    });
8
9retryIfException.Execute(errorProneCode.DoSomethingThatMightThrowException);

Int

Here I check if the int returned is anything other than 0, or if there has been a DivideByZeroException. If so, I retry up to four times and also print some text to the console.

1RetryPolicy<int> retryPolicyNeedsAResponseOfOne = Policy.HandleResult<int>(i => i != 0)
2    .Or<DivideByZeroException>()
3    .Retry(4, (response, retryCount) =>
4    {
5        Console.WriteLine($"Got a response of {response.Result} (expected 0), retrying {retryCount}");
6    });
7
8int number = retryPolicyNeedsAResponseOfOne.Execute(() => errorProneCode.GetSomeNumber());
9Console.WriteLine($"Got expected response = {number}\n\n");

IEnumerable

In this one I check that the IEnumerable has three items in it, if not, I retry and print some info to the console.

1RetryPolicy<IEnumerable<int>> retryPolicyNeedsResponseWithTwoNumbers = Policy.HandleResult<IEnumerable<int>>(l => l.Count() != 3)
2   .Retry(4, (response, retryCount) =>
3   {
4       Console.WriteLine($"Got a response with {response.Result.Count()} entries (expected 3), retrying {retryCount}");
5   });
6
7var numbers = retryPolicyNeedsResponseWithTwoNumbers.Execute(() => errorProneCode.GetListOfNumbers());
8Console.WriteLine($"Got expected response of {numbers.Count()} entries\n\n");

Bool

In this policy I check the bool returned, if it is false, I retry.

1RetryPolicy<bool> retryPolicyNeedsTrueResponse = Policy.HandleResult<bool>(b => b != true)
2   .Retry(4, (response, retryCount) =>
3   {
4       Console.WriteLine($"Got a response of {response.Result} entries (expected true), retrying {retryCount}");
5   });
6
7bool result = retryPolicyNeedsTrueResponse.Execute(() => errorProneCode.GetBool());
8Console.WriteLine($"Got expected response of {result}\n\n");

Polly can check the value of any return type or exception making it possible to use Polly for any call you can think of.

Full source code here.

comments powered by Disqus

Related