Turtle.Retry 2.0.0

Helps to get rid of repetitive retry logic.

Install-Package Turtle.Retry -Version 2.0.0
dotnet add package Turtle.Retry --version 2.0.0
<PackageReference Include="Turtle.Retry" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Turtle.Retry --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Turtle

What does it do?

Turtle saves you from writing your logic to retry an action when it fails. It allows you to fine tune the behavior.

Why Turtle?

A turtle might be slow, but it is determined to get where it wants to be!

Examples

var result = Retry.This(() =>
{
    Console.WriteLine("Hello");
    Throws();
})
.MaximumNumberOfTries(5)
.Run();

This will repeat the Action passed in This() every 100ms for a maximum of 5 tries as long as the action throws an exception.
result contains the CompletionState. The CompletionState is an enum with the following values: Failed, Aborted, Success.

Retry.This(() =>
{
    Console.WriteLine("Hello");
    return false;
})
.MaximumNumberOfTries(5)
.Run();

This will repeat the Func<bool> pass in This() every 100ms for a maximum of tries as long as the Func return false.

Retry.This(() => Console.WriteLine("Hello"),
        () => true)
    .Run();
This uses a predicate to determine if the try was successful.


```C#
Retry.This(() =>
{
    Console.WriteLine("Hello");
    return false;
})
.Using(new LimitedExponentialBackoffRetryStrategy
{
    BaseDelay = TimeSpan.FromMilliseconds(50),
    MaxDelay = TimeSpan.FromSeconds(5)
})
.MaximumNumberOfTries(5)
.Run();

This example uses a limited exponential backoff strategy. The time wait between tries increases exponentially.

Different RetryStrategies can be easily created.

var tokenSource = new CancellationTokenSource();
var t = Retry.This(() =>
{
    Console.WriteLine("async");
    Throws();
}).RunAsync(tokenSource.Token);

It works with Tasks as well.

Control behavior based on the exception

It is possible to define how Turtle.Retry will behave in case of an exception. You can do this by passing an
implementation of IExceptionBehavior like this:

Retry.This(() => action)
.OnException(new MyExceptionBehavior()
.Run();

This interface is fairly simple. Just implement the OnException method,
and return the appropriate AfterExceptionBehavior. AfterExceptionBehavior is an enum with the following values:
Retry, Rethrow, Abort. Retry will result in another try if MaximumNumberOfTries has not been reached yet,
Abort will stop the Retry process and return CompletionState.Aborted. Retrow instructs Turtle.Retry to rethrow
the exception and therefore stop the Retry process.

Turtle

What does it do?

Turtle saves you from writing your logic to retry an action when it fails. It allows you to fine tune the behavior.

Why Turtle?

A turtle might be slow, but it is determined to get where it wants to be!

Examples

var result = Retry.This(() =>
{
    Console.WriteLine("Hello");
    Throws();
})
.MaximumNumberOfTries(5)
.Run();

This will repeat the Action passed in This() every 100ms for a maximum of 5 tries as long as the action throws an exception.
result contains the CompletionState. The CompletionState is an enum with the following values: Failed, Aborted, Success.

Retry.This(() =>
{
    Console.WriteLine("Hello");
    return false;
})
.MaximumNumberOfTries(5)
.Run();

This will repeat the Func<bool> pass in This() every 100ms for a maximum of tries as long as the Func return false.

Retry.This(() => Console.WriteLine("Hello"),
        () => true)
    .Run();
This uses a predicate to determine if the try was successful.


```C#
Retry.This(() =>
{
    Console.WriteLine("Hello");
    return false;
})
.Using(new LimitedExponentialBackoffRetryStrategy
{
    BaseDelay = TimeSpan.FromMilliseconds(50),
    MaxDelay = TimeSpan.FromSeconds(5)
})
.MaximumNumberOfTries(5)
.Run();

This example uses a limited exponential backoff strategy. The time wait between tries increases exponentially.

Different RetryStrategies can be easily created.

var tokenSource = new CancellationTokenSource();
var t = Retry.This(() =>
{
    Console.WriteLine("async");
    Throws();
}).RunAsync(tokenSource.Token);

It works with Tasks as well.

Control behavior based on the exception

It is possible to define how Turtle.Retry will behave in case of an exception. You can do this by passing an
implementation of IExceptionBehavior like this:

Retry.This(() => action)
.OnException(new MyExceptionBehavior()
.Run();

This interface is fairly simple. Just implement the OnException method,
and return the appropriate AfterExceptionBehavior. AfterExceptionBehavior is an enum with the following values:
Retry, Rethrow, Abort. Retry will result in another try if MaximumNumberOfTries has not been reached yet,
Abort will stop the Retry process and return CompletionState.Aborted. Retrow instructs Turtle.Retry to rethrow
the exception and therefore stop the Retry process.

  • .NETStandard 2.0

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.0 3,685 2/28/2018
1.1.1 837 10/26/2016
1.1.0 667 9/30/2016
1.0.1 749 7/12/2016
1.0.0 603 5/16/2016