Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

fix(circuit): disable circuit breaker in development #307

Merged
merged 12 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions __tests__/server/utils/createCircuitBreaker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('Circuit breaker', () => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => 0);

beforeEach(() => {
process.env.NODE_ENV = 'production';
setEventLoopDelayThreshold();
mockCircuitBreaker.close();
jest.clearAllMocks();
Expand Down Expand Up @@ -64,6 +65,19 @@ describe('Circuit breaker', () => {
expect(value).toBe(true);
});

it('should not open the circuit when in development environment', async () => {
process.env.NODE_ENV = 'development';
expect.assertions(2);
setEventLoopDelayThreshold(-1);
nellyk marked this conversation as resolved.
Show resolved Hide resolved
jest.advanceTimersByTime(510);
// Need to fire the breaker once before it will open
await mockCircuitBreaker.fire('hola, mundo');
jest.clearAllMocks();
const value = await mockCircuitBreaker.fire('hola, mundo');
expect(asyncFuntionThatMightFail).toHaveBeenCalled();
expect(value).toBe(false);
});

it('should not open the circuit when the root module is not loaded', async () => {
expect.assertions(2);
getModule.mockReturnValueOnce(false);
Expand Down
2 changes: 2 additions & 0 deletions docs/api/modules/App-Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ if (!global.BROWSER) {

The `eventLoopDelayThreshold` directive accepts a number representing the threshold of the event loop delay (in milliseconds) before opening the circuit. Once the circuit is open, it will remain open for 10 seconds and close at that time pending the event loop delay. The default value is `250`. If you desire to disable the event loop delay potion of the circuit breaker, set this value to `Infinity`. The circuit will also open if the error rate exceeds 10%. In practice, `eventLoopDelayThreshold` allows for tuning server side rendering (SSR) of Modules. We may increase request throughput by temporarily disabling SSR at high load through event loop delay monitoring.

> This is disabled when NODE_ENV=development

**📘 More Information**
* [Frank Lloyd Root's `appConfig`](../../../prod-sample/sample-modules/frank-lloyd-root/0.0.0/src/config.js)
* Library: [Opossum](https://nodeshift.dev/opossum/)
Expand Down
1 change: 1 addition & 0 deletions src/server/utils/createCircuitBreaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const eventLoopDelayHistogram = monitorEventLoopDelay();
eventLoopDelayHistogram.enable();

const checkMaxEventLoopDelay = async () => {
if (process.env.NODE_ENV === 'development') return;
// Return if root module is not loaded, as that is where threshold is configured
if (!getModule(rootModuleName)) return;
// Get event loop delay in milliseconds (from nanoseconds)
Expand Down