What are some exception filtering implementations in a circuit breaker pattern?

circuit breaker pattern , from the book Release It! , protects the remote service from requests while it (or recovery) and helps the client manage the repeated removal of the remote service. I like the Davy Brions circuit breaker and the Ayendes lazy timeout fix is very clean.

However, I have not seen many filtering implementations , the exceptions of which will lead to an increase in the number of circuit breaker failures.


Don't worry about showing a lock unless your implementation is dependent on smart locking. FYI, Phil Haack has the latest version of TimedLock , used in articles by Davy Brion.

+4
source share
3 answers

Predicate filter

A predicate can provide advanced criteria and filtering logic.

public void AttemptCall(Action action, Predicate<Exception> match) { try { action(); } catch(Exception e) { if(match(e)) state.ActUponException(e); throw; } } 

For example, you can only increase the circuit breaker on a WebException caused by a timeout.

 circuitBreaker.AttemptCall(() => service.DoWork(), e => { WebException local = e as WebException; if(local == null) return false; return local.Status == WebExceptionStatus.Timeout; }); 
+3
source

Filter out which types will increase the number

Your first thought may be to build a common method call with a common try... catch . However, below will not work due to .NET error , see these questions for more information.

 public void AttemptCall<TException>(Action action) where TException : Exception { try { action(); } catch(TException e) { state.ActUponExcpetion(e); throw; } } 

You need to catch all the exceptions and examine the type.

 public void AttemptCall<TException>(Action action) where TException : Exception { try { action(); } catch(TException e) { if(e is TException) state.ActUponExcpetion(e); throw; } } 
+1
source

Filter which types will not increase the number

Tim Ross wrote about this .

 private readonly List<Exception> ignored = new List<Exception>(); public void Ignore<TException>() where TException : Exception { Type type = typeof(TException); if(ignored.Contains(type)) return; ignored.Add(type); } public void AttemptCall(Action action) { try { action(); } catch(Exception e) { if(!ignore.Contains(e.GetType())) state.ActUponException(e); throw; } } 
+1
source

All Articles