8000 GitHub - Niahm/Polly at v1.0.0 · GitHub
[go: up one dir, main page]

Skip to content
NiahmPublic
forked from App-vNext/Polly

About

Polly is a .NET 3.5 / 4.0 / 4.5 / PCL library that allows developers to express transient exception handling policies such as Retry, Retry Forever, Wait and Retry or Circuit Breaker in a fluent manner.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Polly

Polly is a .NET 3.5 / 4.0 / 4.5 library that allows developers to express transient exception handling policies such as Retry, Retry Forever, Wait and Retry or Circuit Breaker in a fluent manner.

Installing via NuGet

Install-Package Polly

Usage

Step 1 : Specify the type of exceptions you want the policy to handle

Single exception type

Policy
  .Handle<DivideByZeroException>()

Single exception type with condition

Policy
  .Handle<SqlException>(ex => ex.Number == 1205)

Multiple exception types

Policy
  .Handle<DivideByZeroException>()
  .Or<ArgumentException>()

Multiple exception types with condition

Policy
  .Handle<SqlException>(ex => ex.Number == 1205)
  .Or<ArgumentException>(ex => ex => x.ParamName == "example")

Step 2 : Specifiy how the policy should handle those exceptions

Retry once

Policy
  .Handle<DivideByZeroException>()
  .Retry()

Retry multiple times

Policy
  .Handle<DivideByZeroException>()
  .Retry(3)

Retry multiple times, calling an action on each retry with the current exception and retry count

Policy
    .Handle<DivideByZeroException>()
    .Retry(3, (exception, retyCount) =>
    {
        // do something 
    });

Retry forever

Policy
  .Handle<DivideByZeroException>()
  .RetryForever()

Retry forever, calling an action on each retry with the current exception

Policy
  .Handle<DivideByZeroException>()
  .RetryForever(exception =>
  {
        // do something       
  });

Retry, waiting a specified duration between each retry

Policy
  .Handle<DivideByZeroException>()
  .WaitAndRetry(new[]
  {
    TimeSpan.FromSeconds(1),
    TimeSpan.FromSeconds(2),
    TimeSpan.FromSeconds(3)
  });

Retry, waiting a specified duration between each retry, calling an action on each retry with the current exception and duration

Policy
  .Handle<DivideByZeroException>()
  .WaitAndRetry(new[]
  {
    1.Seconds(),
    2.Seconds(),
    3.Seconds()
  }, (exception, timeSpan) => {
    // do something    
  }); 

Retry a specified number of times, using a function to calculate the duration to wait between retries based on the current retry attempt (allows for exponential backoff)

// in this case will wait for
//  1 ^ 2 = 2 seconds then
//  2 ^ 2 = 4 seconds then
//  3 ^ 2 = 8 seconds then
//  4 ^ 2 = 16 seconds then
//  5 ^ 2 = 32 seconds
Policy
  .Handle<DivideByZeroException>()
  .WaitAndRetry(5, retryAttempt => 
	TimeSpan.FromSeconds(Math.Pow(2, retryAttempt) 
  );

Retry a specified number of times, using a function to calculate the duration to wait between retries based on the current retry attempt, calling an action on each retry with the current exception and duration

Policy
  .Handle<DivideByZeroException>()
  .WaitAndRetry(
    5, 
    retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), 
    (exception, timeSpan) => {
      // do something
    }
  );

Circuit Breaker, break circuit after the specified number of exceptions and keep circuit broken for the specified duration

Policy
  .Handle<DivideByZeroException>()
  .CircuitBreaker(2, TimeSpan.FromMinutes(1));

For more information on the Circuit Breaker pattern see:

Step 3 : Execute the policy

Execute an action

var policy = Policy
              .Handle<DivideByZeroException>()
              .Retry();

policy.Execute(() => DoSomething());

Execute a function returning a result

var policy = Policy
              .Handle<DivideByZeroException>()
              .Retry();

var result = policy.Execute(() => DoSomething());

You can of course chain it all together

Policy
  .Handle<SqlException>(ex => ex.Number == 1205)
  .Or<ArgumentException>(ex => ex.ParamName == "example")
  .Retry()
  .Execute(() => DoSomething());

3rd Party Libraries

License

Licensed under the terms of the New BSD License

About

Polly is a .NET 3.5 / 4.0 / 4.5 / PCL library that allows developers to express transient exception handling policies such as Retry, Retry Forever, Wait and Retry or Circuit Breaker in a fluent manner.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

0