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 Copy linkLink copied to clipboard!
The circuit breaker add-on emits the following types of events:
- fire
-
This event is emitted when the circuit breaker
firemethod is executed to call a remote service. - reject
-
This event is emitted when the circuit breaker has an
openorhalf-openstate. - 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
openstate. - close
-
This event is emitted when the circuit breaker moves to a
closedstate. - halfOpen
-
This event is emitted when the circuit breaker moves to a
half-openstate. - 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 Copy linkLink copied to clipboard!
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.