Using Configuration to Setup Polly
See original GitHub issueI’ve written the following extension method to help me configure Polly from my ASP.NET Core JSON configuration file. It may help others if something more complete was included in Polly itself.
Policy policy = Policy.Handle<Exception>().FromOptions(options);
public class PolicyOptions
{
public TimeSpan? DurationOfBreak { get; set; }
public int? ExceptionsAllowedBeforeBreaking { get; set; }
public int? RetryCount { get; set; }
public bool RetryForever { get; set; }
}
public static Policy FromOptions(this PolicyBuilder policyBuilder, PolicyOptions options)
{
if (policyBuilder == null)
{
throw new ArgumentNullException(nameof(policyBuilder));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
Policy policy;
if (options.RetryCount.HasValue)
{
policy = policyBuilder.RetryAsync(options.RetryCount.Value);
}
else if (options.RetryForever)
{
policy = policyBuilder.RetryForeverAsync();
}
else if (options.DurationOfBreak.HasValue && options.ExceptionsAllowedBeforeBreaking.HasValue)
{
policy = policyBuilder.CircuitBreakerAsync(options.ExceptionsAllowedBeforeBreaking.Value, options.DurationOfBreak.Value);
}
else
{
throw new ArgumentException(
$"Invalid Policy Options.",
nameof(options));
}
return policy;
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Configuration - Polly - Read the Docs
The configuration is done with a yaml file named config.yml located in /etc/polly . Example of Polly advertising VirtualBox as swarm_virtualbox .
Read more >Adding & Using Polly in Teams Meetings
Select Polly and then click on Add; Select Save from the tab configuration page; Begin creating your pollys! HubSpot Video. Polly Add Meeting...
Read more >Installing and Configuring Amazon Polly for Windows (SAPI)
In Windows, open a command prompt. At the command prompt, run the aws configure --profile polly-windows command. When prompted for the AWS Access ......
Read more >Implement HTTP call retries with exponential backoff ...
Learn how to handle HTTP failures with Polly and IHttpClientFactory. ... Configure a client with Polly's Retry policy, in app startup.
Read more >Activate and configure the Amazon Polly integration
To access the Amazon Polly text-to-speech engine in Genesys Cloud, you must first add and configure the Amazon Polly integration and then add...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Pinging on this thread, to alert that there is a lot of related discussion going on in the ASP.NET Core 2.1
HttpClientFactory
repo around the Polly integration.Triage: Closing this issue as not something the core Polly team is likely to add.
Back when this issue was proposed, it made a lot of sense, as Polly had only two policies, configured in mostly-numeric ways.
Polly now offers a wider range of policies, and a range of more powerful ways of configuring them:
IEnumerable<TimeSpan>
of delays-before-retrying; or alternatively aFunc<outcome, TimeSpan>
to drive retry delays from 429 responses; TimeoutPolicy aFunc<TimeSpan> timeout
; circuit-breaker will soon take aFunc<int, TimeSpan>
function to calculatebreakDuration
.TL;DR A config-constant-driven
Options
-API for Polly now would inevitably end up covering only a fragmented subset of ways of configuring policies. This makes it make less sense for the Polly team to aim to provide as an API alternative. However, it is still fairly easy for users to implement for themselves (and a great approach), for a particular subset of constant-driven Polly features they use.