Turtle.Retry
2.0.0
dotnet add package Turtle.Retry --version 2.0.0
NuGet\Install-Package Turtle.Retry -Version 2.0.0
<PackageReference Include="Turtle.Retry" Version="2.0.0" />
paket add Turtle.Retry --version 2.0.0
#r "nuget: Turtle.Retry, 2.0.0"
// Install Turtle.Retry as a Cake Addin #addin nuget:?package=Turtle.Retry&version=2.0.0 // Install Turtle.Retry as a Cake Tool #tool nuget:?package=Turtle.Retry&version=2.0.0
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.