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.
- 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.
#main package
dotnet add package EdwardHsu.CircuitBreaker
#hook injector package
dotnet add package EdwardHsu.CircuitBreaker.HookInjector
- Create a Circuit Breaker.
var fuse = new ExecutionLimitFuse(5);
var breaker = new CircuitBreaker(fuse);
- 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
}
- Control the Circuit Breaker.
breaker.On(); // Turn on the circuit breaker
breaker.Off(); // Turn off the circuit breaker
- Inject the Circuit Breaker into an Existing Method.
breaker.Inject(() => myInstance.MyMethod(arg));
breaker.Inject(() => MyClass.MyStaticMethod(arg));
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.
This project is licensed under the MIT License.