Luffy is a simple resilience and transient-fault handling library for .NET
PM> Install-Package Luffy
- Luffy provides circuit breaker feature
- Luffy provides retry mechanism with back-off (linear and exponentially)
- Luffy provides fallback feature
Sample usage for the circuit breaker:
async Task<double> CircuitBreakerSample(double amount, string from, string to)
{
double currentRate = await Luffy.Instance
.UseCircuitBreaker(new CircuitBreakerOptions(key: "CurrencyConverterSampleAPI",
exceptionThreshold: 5,
successThresholdWhenCircuitBreakerHalfOpenStatus: 5,
durationOfBreak: TimeSpan.FromSeconds(5)))
.ExecuteAsync<double>(async () => {
// Some API calls...
double rate = await CurrencyConverterSampleAPI(amount, from, to);
return rate;
});
return currentRate;
}
Sample usage for the retry mechanism:
async Task<double> RetryMechanismSample(double amount, string from, string to)
{
double currentRate = await Luffy.Instance
.UseRetry(new RetryMechanismOptions(RetryPolicies.Linear,
retryCount: 3,
interval: TimeSpan.FromSeconds(5)))
.ExecuteAsync<double>(async () => {
// Some API calls...
double rate = await CurrencyConverterSampleAPI(amount, from, to);
return rate;
});
return currentRate;
}
Sample usage for the retry mechanism and fallback scenario:
async Task<double> RetryMechanismWithFallbackSample(double amount, string from, string to)
{
double currentRate = await Luffy.Instance
.UseRetry(new RetryMechanismOptions(RetryPolicies.Linear,
retryCount: 3,
interval: TimeSpan.FromSeconds(5)))
.ExecuteAsync<double>(async () => {
// Some API calls...
double rate = await CurrencyConverterSampleAPI(amount, from, to);
return rate;
}, async () => {
// Some fallback scenario.
double rate = 100;
return await Task.FromResult(rate);
});
return currentRate;
}