Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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
});
}
```