Skip to content

Commit

Permalink
feat(circuit-breaker): extract abort controller renewal logic into se…
Browse files Browse the repository at this point in the history
…parate function

nodeshift#861
  • Loading branch information
WillianAgostini committed Oct 23, 2024
1 parent 92230a7 commit 3d3bd18
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,7 @@ class CircuitBreaker extends EventEmitter {
function _halfOpen (circuit) {
circuit[STATE] = HALF_OPEN;
circuit[PENDING_CLOSE] = true;
if (circuit.options.autoRenewAbortController) {
circuit.options.abortController = new AbortController();
}
circuit._renewAbortControllerIfNeeded();
/**
* Emitted after `options.resetTimeout` has elapsed, allowing for
* a single attempt to call the service again. If that attempt is
Expand Down Expand Up @@ -347,6 +345,21 @@ class CircuitBreaker extends EventEmitter {
}
}

/**
* Renews the abort controller if needed
* @private
* @returns {void}
*/
_renewAbortControllerIfNeeded () {
if (
this.options.autoRenewAbortController &&
this.options.abortController &&
this.options.abortController.signal.aborted
) {
this.options.abortController = new AbortController();
}
}

/**
* Closes the breaker, allowing the action to execute again
* @fires CircuitBreaker#close
Expand All @@ -359,13 +372,7 @@ class CircuitBreaker extends EventEmitter {
}
this[STATE] = CLOSED;
this[PENDING_CLOSE] = false;
if (
this.options.autoRenewAbortController &&
this.options.abortController &&
this.options.abortController.signal.aborted
) {
this.options.abortController = new AbortController();
}
this._renewAbortControllerIfNeeded();
/**
* Emitted when the breaker is reset allowing the action to execute again
* @event CircuitBreaker#close
Expand Down

0 comments on commit 3d3bd18

Please sign in to comment.