ngHttpCircuitBreaker is an attempt to model the circuit-breaker pattern for angular services and $http calls. For a great explanation of the benefits of the circuit-breaker pattern, see http://techblog.netflix.com/2011/12/making-netflix-api-more-resilient.html. The ng-http-circuitbreaker is created as an $http interceptor and therefore AngularJS v1.1.5+ is required.
Please note that while a client-side angular circuit breaker should help reduce load on your backend servers in the event of system failures, it is not a replacement for server side circuit breakers / fail-over designs. The point of the client side circuit breaker is to lessen the demand for doomed requests against your server, hopefully giving the server time to recover.
The ngHttpCircuitBreaker can support one or more endpoints, using regular expressions to define each circuit. For example, if your application calls two different sets of APIs, you can selectively choose which one(s) you want to protect with the circuit breaker.
For each circuit you can specify:
- endpoint regular expression - the regex to test the $http.config.url
- failure limit - the number of errors you'll accept before you want the circuit breaker to trip into the OPEN state
- response sla - the time in ms to allow the $http service to wait for a response before timing out (timeouts are an important part of the fail fast mindset)
- half open time - the time in ms until an open circuit should transition to half-open
- status codes to ignore - an array of HTTP failure status codes that should be ignored (such as 401 - Authorization Required)
Your client application must be designed to gracefully handle error responses - once tripped the circuit breaker will reject all requests immediately until the half-open time has elapsed. At that point it will allow a single request to pass through to the backend server - while that request is waiting for a response the circuit will transition back to the OPEN state and reject all requests. If the single request that went through during the half open state responds successfully, the circuit will immediately move to the CLOSED state and allow all requests to go through again.
Include the ng-http-circuitbreaker dependency in your module, configure the circuits and add it to the $httpProvider's interceptor list.
var yourModule = angular.module('someApp', ['ng-http-circuitbreaker'])
.config(['$httpProvider','ngHttpCircuitBreakerConfigProvider', function($httpProvider, ngHttpCircuitBreakerConfigProvider) {
ngHttpCircuitBreakerConfigProvider
.circuit({endPointRegEx: /^\/api\//i, failureLimit: 5, responseSLA: 500, timeUntilHalfOpen: 5000, statusCodesToIgnore: [401,403,409]})
.circuit({endPointRegEx: /^\/ext\//i, failureLimit: 8, responseSLA: 750, timeUntilHalfOpen: 5000, statusCodesToIgnore: [401]});
$httpProvider.interceptors.push('ngHttpCircuitBreaker');
}]);
The only required parameter to the circuit configuration is the endPointRegEx which is any valid regular expression that matches your service route(s). The other parameters provide the following defaults:
- failureLimit: 5
- responseSLA: 500ms
- timeUntilHalfOpen: 5000ms
- statusCodesToIgnore: [401,403,409]