此内容没有您所选择的语言版本。

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 or half-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.

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)}`);
  }
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat