← All tasksNode.js · Senior
Circuit breaker
When a dependency is down, every request to it waits for a timeout and ties up resources. Write createBreaker(fn, { failureThreshold, resetMs, now }) with call(...args) and state().
- closed: calls go to fn; after failureThreshold consecutive failures the breaker goes open (a success resets the counter).
- open: call rejects immediately with Circuit open, and fn isn't called.
- resetMs after opening the state is half-open: one trial call goes through (other concurrent ones get Circuit open). Success closes the breaker, failure opens it again for resetMs.
Ctrl+Enter runs the tests. Tab indents; to leave the editor with the keyboard, press Esc and then Tab. Your code is saved in this browser.
Tests (6)
let t = 0; const b = createBreaker(async (x) => x * 2, { failureThreshold: 3, resetMs: 1000, now: () => t }); expect(await b.call(21)).toBe(42); expect(b.state()).toBe('closed');
let t = 0; const f = fn(async () => { throw new Error('timeout'); }); const b = createBreaker(f, { failureThreshold: 3, resetMs: 1000, now: () => t }); for (let i = 0; i < 3; i++) await expect(b.call()).rejects.toThrow('timeout'); expect(b.state()).toBe('open'); await expect(b.call()).rejects.toThrow('Circuit open'); expect(f).toHaveBeenCalledTimes(3);
let t = 0; let fail = true; const b = createBreaker(async () => { if (fail) throw new Error('down'); return 'ok'; }, { failureThreshold: 2, resetMs: 1000, now: () => t }); await expect(b.call()).rejects.toThrow('down'); await expect(b.call()).rejects.toThrow('down'); t = 999; expect(b.state()).toBe('open'); t = 1000; expect(b.state()).toBe('half-open'); fail = false; expect(await b.call()).toBe('ok'); expect(b.state()).toBe('closed');
let t = 0; const b = createBreaker(async () => { throw new Error('down'); }, { failureThreshold: 1, resetMs: 500, now: () => t }); await expect(b.call()).rejects.toThrow('down'); t = 500; await expect(b.call()).rejects.toThrow('down'); expect(b.state()).toBe('open'); t = 900; await expect(b.call()).rejects.toThrow('Circuit open');
let t = 0; let ok = false; const b = createBreaker(async () => { if (!ok) throw new Error('down'); await wait(30); return 'ok'; }, { failureThreshold: 1, resetMs: 100, now: () => t }); await expect(b.call()).rejects.toThrow('down'); t = 100; ok = true; const first = b.call(); await expect(b.call()).rejects.toThrow('Circuit open'); expect(await first).toBe('ok');
let t = 0; let n = 0; const b = createBreaker(async () => { n++; if (n % 2) throw new Error('flaky'); return n; }, { failureThreshold: 2, resetMs: 1000, now: () => t }); await expect(b.call()).rejects.toThrow('flaky'); expect(await b.call()).toBe(2); await expect(b.call()).rejects.toThrow('flaky'); expect(b.state()).toBe('closed');