Skip to content

Instantly share code, notes, and snippets.

@dvhthomas
Created November 14, 2009 03:12
Show Gist options
  • Save dvhthomas/234356 to your computer and use it in GitHub Desktop.
Save dvhthomas/234356 to your computer and use it in GitHub Desktop.
Circuit breaker usage
using System;
using System.Collections.Generic;
using CityIQ.HealthMonitor.Notifiers;
using CityIQ.HealthMonitor.Testers;
namespace CityIQ.HealthMonitor
{
/// <summary>
/// Implementation that can make requests of services and optionally notify
/// interested parties of the results.
/// </summary>
public class HeartbeatMonitor : IHeartbeatMonitor
{
private readonly IEnumerable<INotifier> _notifiers;
public HeartbeatMonitor()
{
_notifiers = new List<INotifier>();
}
/// <summary>
/// Ctor that can take some notification providers
/// </summary>
/// <param name="notifiers"></param>
public HeartbeatMonitor(IEnumerable<INotifier> notifiers)
{
_notifiers = notifiers;
}
#region IHeartbeatMonitor Members
public void CheckResource(IResourceTester resource, ICircuitBreaker circuitBreaker)
{
try
{
circuitBreaker.ExecuteCall(resource, r => r.IsAlive());
bool alive = resource.IsAlive();
if (!alive)
{
Notify(Importance.Minor, resource.Name, string.Format("Failed to call the resource {0}", resource.Name));
}
}
catch (OpenCircuitException oce)
{
// This indicates that the service has been repeatedly unavailable
// and we should let someone know about it. Until the timeout period
// has passed we'll keep throwing these until the circuit is reset to half open
Notify(Importance.Critical, resource.Name, oce.ToString());
}
catch (Exception e)
{
// The operation failed but not didn't happen enough to trip the circuit breaker.
// So this is the exception that caused the problem.
Notify(Importance.Important, resource.Name, e.ToString());
}
}
#endregion
private void Notify(Importance severity, string resourceName, string message)
{
foreach (INotifier notifier in _notifiers)
{
notifier.Notify(severity, resourceName, message);
}
}
}
}
using System;
using CityIQ.HealthMonitor;
using CityIQ.HealthMonitor.Testers;
using NUnit.Framework;
using StructureMap;
namespace CityIQ.MonitorTests
{
[TestFixture]
public class When_testing_web_sites
{
[SetUp]
public void Before_any_test()
{
IoC.Bootstrap();
}
[Test]
public void Valid_website_causes_no_failures()
{
var tester = new WebSite("Google","http://www.google.com",TimeSpan.FromSeconds(5d));
var hb = new HeartbeatMonitor();
var cb = ObjectFactory.GetInstance<ICircuitBreaker>();
Assert.AreEqual(cb.Failures, 0);
hb.CheckResource(tester, cb);
Assert.AreEqual(cb.Failures, 0);
}
[Test]
public void Inaccessible_website_causes_1_failure()
{
var tester = new WebSite("Boom", "http://www.google.com/c", TimeSpan.FromSeconds(5));
var hb = new HeartbeatMonitor();
var cb = ObjectFactory.GetInstance<ICircuitBreaker>();
Assert.AreEqual(cb.Failures, 0);
hb.CheckResource(tester, cb);
Assert.AreEqual(cb.Failures, 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment