Philiprehberger.CircuitBreaker
0.1.5
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Philiprehberger.CircuitBreaker --version 0.1.5
NuGet\Install-Package Philiprehberger.CircuitBreaker -Version 0.1.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Philiprehberger.CircuitBreaker" Version="0.1.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.CircuitBreaker" Version="0.1.5" />
<PackageReference Include="Philiprehberger.CircuitBreaker" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.CircuitBreaker --version 0.1.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Philiprehberger.CircuitBreaker, 0.1.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Philiprehberger.CircuitBreaker@0.1.5
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.CircuitBreaker&version=0.1.5
#tool nuget:?package=Philiprehberger.CircuitBreaker&version=0.1.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Philiprehberger.CircuitBreaker
Standalone circuit breaker with half-open probing, health tracking, and event callbacks.
Installation
dotnet add package Philiprehberger.CircuitBreaker
Usage
using Philiprehberger.CircuitBreaker;
var breaker = new CircuitBreaker(failureThreshold: 3, openDuration: TimeSpan.FromSeconds(10));
breaker.OnOpen += () => Console.WriteLine("Circuit opened!");
breaker.OnClose += () => Console.WriteLine("Circuit closed.");
breaker.OnHalfOpen += () => Console.WriteLine("Circuit half-open, probing...");
// Synchronous execution
var result = breaker.Execute(() => ComputeValue());
// Asynchronous execution
var data = await breaker.ExecuteAsync(() => httpClient.GetStringAsync("/api/health"));
Using Options
var options = new CircuitBreakerOptions(
FailureThreshold: 5,
OpenDuration: TimeSpan.FromSeconds(30)
);
var breaker = new CircuitBreaker(options);
Manual Control
breaker.Trip(); // Force the circuit open
breaker.Reset(); // Force the circuit closed
Handling Rejected Requests
try
{
var result = breaker.Execute(() => CallService());
}
catch (CircuitBrokenException ex)
{
Console.WriteLine($"State: {ex.State}, retry in {ex.RemainingDuration.TotalSeconds}s");
}
API
CircuitBreaker
| Member | Description |
|---|---|
CircuitBreaker(int failureThreshold = 5, TimeSpan? openDuration = null) |
Creates a new circuit breaker |
CircuitBreaker(CircuitBreakerOptions options) |
Creates a circuit breaker from options |
Execute<T>(Func<T>) |
Executes a synchronous operation through the breaker |
ExecuteAsync<T>(Func<Task<T>>) |
Executes an asynchronous operation through the breaker |
Trip() |
Manually opens the circuit |
Reset() |
Manually closes the circuit and resets failure count |
State |
Current CircuitState (Closed, Open, HalfOpen) |
FailureCount |
Number of consecutive failures |
LastFailure |
Timestamp of the most recent failure |
OnOpen |
Callback when circuit opens |
OnClose |
Callback when circuit closes |
OnHalfOpen |
Callback when circuit enters half-open |
OnFailure |
Callback on each failure |
OnSuccess |
Callback on each success |
CircuitState
| Value | Description |
|---|---|
Closed |
Requests flow through normally |
Open |
Requests are rejected immediately |
HalfOpen |
A single probe request is allowed to test recovery |
CircuitBreakerOptions
| Property | Default | Description |
|---|---|---|
FailureThreshold |
5 |
Failures before opening |
OpenDuration |
null (30s) |
Duration circuit stays open |
HalfOpenTimeout |
null |
Timeout for half-open probe |
CircuitBrokenException
| Property | Description |
|---|---|
State |
Circuit state when the exception was thrown |
OpenedAt |
When the circuit was opened |
RemainingDuration |
Time until the circuit transitions to half-open |
Development
dotnet build src/Philiprehberger.CircuitBreaker.csproj --configuration Release
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.