39 lines
1.3 KiB
C#
39 lines
1.3 KiB
C#
using EonaCat.SmartRetry;
|
|
|
|
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
|
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
|
|
|
class Program
|
|
{
|
|
static async Task Main()
|
|
{
|
|
var policy = SmartRetryPolicy.Policy()
|
|
.Handle<TimeoutException>()
|
|
.Retry(5)
|
|
.WithExponentialBackoff(1, 2)
|
|
.WithJitter(0.2)
|
|
.WithCircuitBreaker(failureThreshold: 3, openDuration: TimeSpan.FromSeconds(10))
|
|
.WithFallbackAsync(async () =>
|
|
{
|
|
Console.WriteLine("Executing fallback...");
|
|
await Task.Delay(50);
|
|
|
|
// fallback value
|
|
return 42;
|
|
})
|
|
.OnRetry((attempt, ex) => Console.WriteLine($"Retry {attempt}: {ex.Message}"))
|
|
.OnCircuitOpen(() => Console.WriteLine("Circuit breaker opened"))
|
|
.OnCircuitClose(() => Console.WriteLine("Circuit breaker closed"));
|
|
|
|
int result = await policy.ExecuteAsync<int>(async (ct) =>
|
|
{
|
|
Console.WriteLine("Attempting operation...");
|
|
await Task.Delay(50, ct);
|
|
throw new TimeoutException("Simulated failure");
|
|
});
|
|
|
|
Console.WriteLine($"Result: {result}");
|
|
|
|
}
|
|
}
|