Stability.Net 1.0.4

C# net core library to bring Stability in your project.
Resiliency Pattern to provide stability of your application execution path.
Current Implementation Supports Circuit-breaker and Retry Commands.
Subscribed the live feed.
Best for Async Calls.

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

Stability

Stability dotnet core library to provide Resiliency pattern for .NET application.

Features!

  • Circuit Breaker
  • Retry Pattern

Installation

It's available via a nuget package
PM> Install-Package Stability.Net

Example : Simple Circuit Breaker

    public async Task Success_with_Close_State() {
        var builder = new CircuitBreakerBuilder();
        var userEntity = new UserEntity();
        var command = builder.AsKey(nameof(userEntity.GetUsers)).WithMaxFailure(3).Build();
        var users = await command.ExecuteAsync(() => userEntity.GetUsers(0), CancellationToken.None);
    }

Example : Circuit Breaker with Fallback

    public async Task Given_Failed_Request_With_Fallback_Should_Execute_Fallback_with_Expected_Result() {
            var builder = CreateBuilder();
            var userEntity = new UserEntity();

            CircuitBreakerCommand command = builder.AsKey(nameof(userEntity.GetUsers)).WithMaxFailure(1).WithResetTimeout(TimeSpan.FromSeconds(1)).Build();
            var users = await command.ExecuteAsync(() => userEntity.GetUsers(10), CancellationToken.None, () => userEntity.GetSuccessResult());
        }

Example : Circuit Breaker with Metrics Collector

// Initialize the MetricsCollector Advance uses with Metrics Collector

      public static class MetricsCollector {
        public static IList<CircuitBreakerMetrics> MetricsStore = new List<CircuitBreakerMetrics>();

        public static void SubscribeMetrics() {
            CircuitBreakerBuilder.Feed.Subscribe(metrics => MetricsStore.Add(metrics));
        }
        public static ICircuitBreakerBuilder CreateCommand(string commandName) {
            var builder = new CircuitBreakerBuilder()
                .AsKey(commandName)
                .WithMaxFailure(3)
                .WithResetTimeout(TimeSpan.FromSeconds(10))
                .WithMaxRetry(3)
                .RetryInterval(new List<TimeSpan>()
                {
                    TimeSpan.FromMilliseconds(100),
                    TimeSpan.FromMilliseconds(500),
                    TimeSpan.FromMilliseconds(1000)
                });

            return builder;
        }
    }

// wrap methods calls
      public async Task<IActionResult> GetCircuitBreaker(string simulate, CancellationToken cancellationToken) {
            var person = new Person();
            var builder = MetricsCollector.CreateCommand("GetEmployee");
            CircuitBreakerCommand command = builder.Build();
            IEnumerable<EmployeeModel> result = null;
            try {
                result = await command.ExecuteAsync(() => person.EmployeeModels(simulate, person), cancellationToken);
            }
            catch (CircuitBreakerOpen e) {
                return NotFound(new { CircuitBreakerBuilder.Metrics, Exception = e });
            }
            catch (Exception) {

            }
            return Ok(new { CommandMetrics = command.Metrics, Data = result });
        }
    // Access Metrics
     public IActionResult GetMetrics() {
            return Ok(MetricsCollector.MetricsStore);
        }
 

Stability

Stability dotnet core library to provide Resiliency pattern for .NET application.

Features!

  • Circuit Breaker
  • Retry Pattern

Installation

It's available via a nuget package
PM> Install-Package Stability.Net

Example : Simple Circuit Breaker

    public async Task Success_with_Close_State() {
        var builder = new CircuitBreakerBuilder();
        var userEntity = new UserEntity();
        var command = builder.AsKey(nameof(userEntity.GetUsers)).WithMaxFailure(3).Build();
        var users = await command.ExecuteAsync(() => userEntity.GetUsers(0), CancellationToken.None);
    }

Example : Circuit Breaker with Fallback

    public async Task Given_Failed_Request_With_Fallback_Should_Execute_Fallback_with_Expected_Result() {
            var builder = CreateBuilder();
            var userEntity = new UserEntity();

            CircuitBreakerCommand command = builder.AsKey(nameof(userEntity.GetUsers)).WithMaxFailure(1).WithResetTimeout(TimeSpan.FromSeconds(1)).Build();
            var users = await command.ExecuteAsync(() => userEntity.GetUsers(10), CancellationToken.None, () => userEntity.GetSuccessResult());
        }

Example : Circuit Breaker with Metrics Collector

// Initialize the MetricsCollector Advance uses with Metrics Collector

      public static class MetricsCollector {
        public static IList<CircuitBreakerMetrics> MetricsStore = new List<CircuitBreakerMetrics>();

        public static void SubscribeMetrics() {
            CircuitBreakerBuilder.Feed.Subscribe(metrics => MetricsStore.Add(metrics));
        }
        public static ICircuitBreakerBuilder CreateCommand(string commandName) {
            var builder = new CircuitBreakerBuilder()
                .AsKey(commandName)
                .WithMaxFailure(3)
                .WithResetTimeout(TimeSpan.FromSeconds(10))
                .WithMaxRetry(3)
                .RetryInterval(new List<TimeSpan>()
                {
                    TimeSpan.FromMilliseconds(100),
                    TimeSpan.FromMilliseconds(500),
                    TimeSpan.FromMilliseconds(1000)
                });

            return builder;
        }
    }

// wrap methods calls
      public async Task<IActionResult> GetCircuitBreaker(string simulate, CancellationToken cancellationToken) {
            var person = new Person();
            var builder = MetricsCollector.CreateCommand("GetEmployee");
            CircuitBreakerCommand command = builder.Build();
            IEnumerable<EmployeeModel> result = null;
            try {
                result = await command.ExecuteAsync(() => person.EmployeeModels(simulate, person), cancellationToken);
            }
            catch (CircuitBreakerOpen e) {
                return NotFound(new { CircuitBreakerBuilder.Metrics, Exception = e });
            }
            catch (Exception) {

            }
            return Ok(new { CommandMetrics = command.Metrics, Data = result });
        }
    // Access Metrics
     public IActionResult GetMetrics() {
            return Ok(MetricsCollector.MetricsStore);
        }
 

Release Notes

added Metrics provider
added thread-safe for metrics

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.4 353 12/6/2017
1.0.3 328 11/25/2017
1.0.2 336 11/20/2017
1.0.1 302 11/19/2017
1.0.0 287 11/18/2017