Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kylos101/circuitbreaker
A circuit breaker class library
https://github.com/kylos101/circuitbreaker
Last synced: 20 days ago
JSON representation
A circuit breaker class library
- Host: GitHub
- URL: https://github.com/kylos101/circuitbreaker
- Owner: kylos101
- License: mit
- Created: 2014-10-09T02:05:46.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-11-26T02:33:23.000Z (about 8 years ago)
- Last Synced: 2024-12-09T04:36:58.837Z (26 days ago)
- Language: C#
- Size: 52.7 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
CircuitBreaker
==============
[![Build status](https://ci.appveyor.com/api/projects/status/24xnocra9wc8hl00/branch/master?svg=true)](https://ci.appveyor.com/project/kylos101/circuitbreaker/branch/master)A circuit breaker class library that uses a command pattern.
[I started here...](http://msdn.microsoft.com/en-us/library/dn589784.aspx). As you can see, it was heavily borrowed.
## How to use it
### Define a circuit
This represents something you want to protect.
```csharp
///
/// My database's circuit
///
public class MyDatabaseCircuit : AbstractCircuit
{
public MyDatabaseCircuit(TimeSpan? timespan) : base(timespan)
{
base.Description = "Some fake database I use...";
}
}
```### Setup a command
This represents something you want to do.
```csharp
///
/// A command with an action that always works...
///
public class TestCommand : AbstractCommand
{
public TestCommand(ICircuit circuit) : base(circuit)
{
this.Action = DoSomething;
}///
/// An action for this TestCommand
///
protected override Action Action { get; set; }///
/// This should "succeed" assuming the breaker's circuit is not "tripped"
///
private void DoSomething()
{
Debug.WriteLine("We did something!");
}
}
```### Try using your command
It'll throw CircuitBreakerOpenException if there's trouble.
Refer to the inner exception for the actual exception.
```csharp
var testCircuit = new MyDatabaseCircuit(null);
var testCommand = new TestCommand(testCiruit);try
{
var result = testCommand.ExecuteAction().Result;
}
catch (AggregateException ex)
{
ex.Handle((x) =>
{
if (x is CircuitBreakerOpenException)
{
// do something because the circuit is open / tripped / down
return true; // don't throw, we're handling the exception
}
return false; // throw, we didn't encounter the expected exception
});
}
```