question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Configuration to Setup Polly

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
reisenbergercommented, Feb 22, 2018

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.

1reaction
reisenbergercommented, Feb 9, 2020

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:

  • some policies such as Fallback are entirely non-numeric
  • most policies which in their original versions were entirely numeric now also take more function-driven overloads: retry policy can take an IEnumerable<TimeSpan> of delays-before-retrying; or alternatively a Func<outcome, TimeSpan> to drive retry delays from 429 responses; TimeoutPolicy a Func<TimeSpan> timeout; circuit-breaker will soon take a Func<int, TimeSpan> function to calculate breakDuration.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found