Awesome Open Source
Awesome Open Source

Luffy


alt tag

Luffy is a simple resilience and transient-fault handling library for .NET

NuGet version

NuGet Packages

PM> Install-Package Luffy

Features:

  • Luffy provides circuit breaker feature
  • Luffy provides retry mechanism with back-off (linear and exponentially)
  • Luffy provides fallback feature

Usages:


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;
}

Samples:


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
c-sharp (11,550
retry (33
circuit-breaker (31
resiliency (16

Find Open Source By Browsing 7,000 Topics Across 59 Categories