Chapter 5. Event handling for events emitted by the circuit breaker add-on
The circuit breaker add-on emits events for the different types of actions that can occur based on the changeable states of the circuit breaker pattern. You can implement event handling in your Node.js applications to work with these different types of events and to perform some action when they occur. By using event handlers, you can control how your applications respond to the current behavior of the circuit breaker.
5.1. Types of events emitted by the circuit breaker add-on
The circuit breaker add-on emits the following types of events:
- fire
-
This event is emitted when the circuit breaker
fire
method is executed to call a remote service. - reject
-
This event is emitted when the circuit breaker has an
open
orhalf-open
state. - timeout
- This event is emitted when the timeout period for the circuit breaker action expires.
- success
- This event is emitted when the circuit breaker action completes successfully.
- failure
- This event is emitted when the circuit breaker action fails and the circuit breaker returns an error response.
- open
-
This event is emitted when the circuit breaker moves to an
open
state. - close
-
This event is emitted when the circuit breaker moves to a
closed
state. - halfOpen
-
This event is emitted when the circuit breaker moves to a
half-open
state. - fallback
- This event is emitted when the circuit breaker has a fallback function that is executed after a call to the remote service fails.
- semaphoreLocked
- This event is emitted when the circuit breaker is at full capacity and it cannot execute the request.
- healthCheckFailed
- This event is emitted when a user-supplied health check function returns a rejected promise.
5.2. Example application that uses event handlers for circuit breaker events
This example shows how to listen for different types of events that the circuit breaker emits. In this example, a fetch
function is used to request a microservice URL. Depending on the event type, the relevant message is printed to the web console.
... // Import the circuit breaker add-on const Opossum = require('@redhat/opossum'); ... // Create a new circuit breaker instance and pass the // protected function call as its first parameter const circuit = new Opossum(() => $.get(route), circuitOptions); ... // Listen for success events emitted when the // circuit breaker action completes successfully circuit.on('success', (result) => { console.log(`SUCCESS: ${JSON.stringify(result)}`); } // Listen for timeout events emitted when the timeout // period for the circuit breaker action expires circuit.on('timeout', (result) => { console.log(`TIMEOUT: ${url} is taking too long to respond.`); } // Listen for reject events emitted when the // circuit breaker has an open or half-open state circuit.on('reject', (result) => { console.log(`REJECTED: The breaker for ${url} is open. Failing fast.`); } // Listen for open events emitted when the // circuit breaker moves to an open state circuit.on('open', (result) => { console.log(`OPEN: The breaker for ${url} just opened.`); } // Listen for halfOpen events emitted when the // circuit breaker moves to a half-open state circuit.on('halfOpen', (result) => { console.log(`HALF_OPEN: The breaker for ${url} is half open.`); } // Listen for close events emitted when the // circuit breaker moves to a closed state circuit.on('close', (result) => { console.log(`CLOSE: The breaker for ${url} has closed. Service OK.`); } // Listen for fallback events emitted when // a fallback function is executed circuit.on('fallback', (result) => { console.log(`FALLBACK: ${JSON.stringify(data)}`); }
Additional resources
- For more information about the member types that you can use with the circuit breaker add-on, see the Circuit Breaker Add-on 5.0.0 API Documentation.