|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const logger = require('../logger'); |
| 4 | + |
| 5 | +const STATES = Object.freeze({ |
| 6 | + CLOSED: 'closed', |
| 7 | + OPEN: 'open', |
| 8 | + HALF_OPEN: 'half-open', |
| 9 | +}); |
| 10 | + |
| 11 | +class CircuitBreaker { |
| 12 | + constructor(name, options = {}) { |
| 13 | + this.name = name; |
| 14 | + this.failureThreshold = Math.max(1, options.failureThreshold ?? 3); |
| 15 | + this.successThreshold = Math.max(1, options.successThreshold ?? 1); |
| 16 | + this.timeoutMs = Math.max(1, options.timeoutMs ?? 30000); |
| 17 | + this._now = options.now || Date.now; |
| 18 | + this._logger = options.logger || logger; |
| 19 | + |
| 20 | + this.state = STATES.CLOSED; |
| 21 | + this.failureCount = 0; |
| 22 | + this.successCount = 0; |
| 23 | + this.openedAt = null; |
| 24 | + this.halfOpenInFlight = false; |
| 25 | + } |
| 26 | + |
| 27 | + getState() { |
| 28 | + this._moveToHalfOpenIfReady(); |
| 29 | + return this.state; |
| 30 | + } |
| 31 | + |
| 32 | + isOpen() { |
| 33 | + return this.getState() === STATES.OPEN; |
| 34 | + } |
| 35 | + |
| 36 | + async call(fn) { |
| 37 | + this._moveToHalfOpenIfReady(); |
| 38 | + |
| 39 | + if (this.state === STATES.OPEN) { |
| 40 | + this._logger.info('Circuit breaker open, skipping source call', { |
| 41 | + source: this.name, |
| 42 | + state: this.state, |
| 43 | + }); |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + if (this.state === STATES.HALF_OPEN && this.halfOpenInFlight) { |
| 48 | + this._logger.info('Circuit breaker half-open probe already in flight, skipping source call', { |
| 49 | + source: this.name, |
| 50 | + state: this.state, |
| 51 | + }); |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + const probing = this.state === STATES.HALF_OPEN; |
| 56 | + if (probing) { |
| 57 | + this.halfOpenInFlight = true; |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + const result = await fn(); |
| 62 | + if (result === null || result === undefined) { |
| 63 | + this.recordFailure(); |
| 64 | + } else { |
| 65 | + this.recordSuccess(); |
| 66 | + } |
| 67 | + return result ?? null; |
| 68 | + } catch (err) { |
| 69 | + this.recordFailure(); |
| 70 | + throw err; |
| 71 | + } finally { |
| 72 | + if (probing) { |
| 73 | + this.halfOpenInFlight = false; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + recordSuccess() { |
| 79 | + if (this.state === STATES.HALF_OPEN) { |
| 80 | + this.successCount += 1; |
| 81 | + if (this.successCount >= this.successThreshold) { |
| 82 | + this._transitionTo(STATES.CLOSED, { reason: 'success-threshold' }); |
| 83 | + } |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (this.state === STATES.CLOSED) { |
| 88 | + this.failureCount = 0; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + recordFailure() { |
| 93 | + if (this.state === STATES.HALF_OPEN) { |
| 94 | + this._transitionTo(STATES.OPEN, { reason: 'half-open-failure' }); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if (this.state === STATES.CLOSED) { |
| 99 | + this.failureCount += 1; |
| 100 | + if (this.failureCount >= this.failureThreshold) { |
| 101 | + this._transitionTo(STATES.OPEN, { reason: 'failure-threshold' }); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + reset() { |
| 107 | + this._transitionTo(STATES.CLOSED, { reason: 'manual-reset' }); |
| 108 | + } |
| 109 | + |
| 110 | + _moveToHalfOpenIfReady() { |
| 111 | + if (this.state !== STATES.OPEN || this.openedAt === null) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if (this._now() - this.openedAt >= this.timeoutMs) { |
| 116 | + this._transitionTo(STATES.HALF_OPEN, { reason: 'cooldown-elapsed' }); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + _transitionTo(nextState, metadata = {}) { |
| 121 | + if (this.state === nextState) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + const previousState = this.state; |
| 126 | + this.state = nextState; |
| 127 | + this.failureCount = 0; |
| 128 | + this.successCount = 0; |
| 129 | + this.openedAt = nextState === STATES.OPEN ? this._now() : null; |
| 130 | + |
| 131 | + this._logger.info('Circuit breaker state changed', { |
| 132 | + source: this.name, |
| 133 | + from: previousState, |
| 134 | + to: nextState, |
| 135 | + ...metadata, |
| 136 | + }); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +module.exports = { |
| 141 | + CircuitBreaker, |
| 142 | + STATES, |
| 143 | +}; |
0 commit comments