edward-hsu-1994 / EdwardHsu.CircuitBreaker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EdwardHsu.CircuitBreaker

Unit Test codecov NuGet Version NuGet Download Github license

CircuitBreaker is a .NET library that provides a circuit breaker pattern implementation for methods. It is designed to prevent a method from being executed too frequently or too intensively, which can lead to resource exhaustion or other issues.

Features

  • Circuit Breaker: The CircuitBreaker class represents the circuit breaker and provides methods to control its state (on, tripped off, and off).
  • Fuse: The IFuse interface defines the triggering conditions of the circuit breaker. The library provides two implementations:
    • ExecutionLimitFuse: Trips the circuit breaker when the number of executions exceeds a specified limit.
    • TimeSlidingWindowLimitFuse: Trips the circuit breaker when the number of executions within a specified time window exceeds a limit.
  • Injection: The CircuitBreakerHookInjector class allows you to inject the circuit breaker into existing methods without modifying the code.

Installation

#main package
dotnet add package EdwardHsu.CircuitBreaker

#hook injector package
dotnet add package EdwardHsu.CircuitBreaker.HookInjector

Usage

  1. Create a Circuit Breaker.
var fuse = new ExecutionLimitFuse(5);
var breaker = new CircuitBreaker(fuse);
  1. Execute a Method through the Circuit Breaker.
try
{
    breaker.Execute(new object[] { arg1, arg2 }); // Arguments can be passed to Fuse, which can determine whether to trigger based on these parameters.
}
catch (InvalidOperationException)
{
    // Circuit breaker is tripped off
}
  1. Control the Circuit Breaker.
breaker.On(); // Turn on the circuit breaker
breaker.Off(); // Turn off the circuit breaker
  1. Inject the Circuit Breaker into an Existing Method.
breaker.Inject(() => myInstance.MyMethod(arg));
breaker.Inject(() => MyClass.MyStaticMethod(arg));

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License.

About

License:MIT License


Languages

Language:C# 100.0%