Class CircuitBreakerFilter

java.lang.Object
org.forgerock.openig.filter.circuitbreaker.CircuitBreakerFilter
All Implemented Interfaces:
Filter

public class CircuitBreakerFilter extends Object implements Filter
Filter implementing the Circuit Breaker pattern to avoid cascading failures.

When a maxFailures number of errors is reached, the "circuit" will open up : the downstream handler will not be called anymore and requests will be forwarded to the openHandler.

After openDuration duration, the "circuit" will close again and let the requests go through the downstream handler again.

Note : Only RuntimeException are considered as failures.

Configuration options:

 
 {
     "type": "CircuitBreakerFilter",
     "config": {
         "maxFailures":      integer                            [REQUIRED - The maximum number of failures before
                                                                            opening the circuit]
         "openDuration":     duration                           [REQUIRED - The duration that the circuit will remain
                                                                            open for, once opened]
         "slidingCounter": {
             "size":         integer                            [REQUIRED - The number of last requests to consider.
                                                                            Must be greater than 'maxFailures']
         }
         "openHandler":      Handler reference                  [OPTIONAL - The handler that will handle requests
                                                                            while the circuit is open..
                                                                            Defaults to a static RuntimeException.
         "executor":         ScheduledExecutorService reference [OPTIONAL - The scheduler to use to schedule the
                                                                            re-closure of the circuit when opened.
                                                                            Defaults to the one present in the heap]
     }
 }
 
 

Example using a SlidingCounterResultRecorder: the circuit will open up for 1 minute, after 10 failures in the previous 100 requests using the ScheduledExecutorService already present in the heap.

 
 {
     "type": "CircuitBreakerFilter",
     "config": {
         "maxFailures": 10,
         "openDuration": "1 minute",
         "slidingCounter": {
             "size": 100
         }
     }
 }
 
 
  • Method Details

    • filter

      public Promise<Response,NeverThrowsException> filter(Context context, Request request, Handler next)
      Description copied from interface: Filter
      Filters the request and/or response of an exchange. To pass the request to the next filter or handler in the chain, the filter calls next.handle(context, request).

      This method may elect not to pass the request to the next filter or handler, and instead handle the request itself. It can achieve this by merely avoiding a call to next.handle(context, request) and creating its own response object. The filter is also at liberty to replace a response with another of its own by intercepting the response returned by the next handler.

      Specified by:
      filter in interface Filter
      Parameters:
      context - The request context.
      request - The request.
      next - The next filter or handler in the chain to handle the request.
      Returns:
      A Promise representing the response to be returned to the client.