use js private
This commit is contained in:
@@ -10,7 +10,7 @@ import { ExponentialBackoffStoppedRetriesError, ExponentialBackoffMaxRetriesHitE
|
|||||||
* The growth rate is the factor by which the delay increases with each attempt.
|
* The growth rate is the factor by which the delay increases with each attempt.
|
||||||
*/
|
*/
|
||||||
export class ExponentialBackoff {
|
export class ExponentialBackoff {
|
||||||
private readonly options: ExponentialBackoffOptions;
|
readonly #options: ExponentialBackoffOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new exponential-backoff instance.
|
* Creates a new exponential-backoff instance.
|
||||||
@@ -25,7 +25,7 @@ export class ExponentialBackoff {
|
|||||||
* @param options.jitter - Maximum proportional reduction subtracted from each delay (0–1). Default: `0.1`.
|
* @param options.jitter - Maximum proportional reduction subtracted from each delay (0–1). Default: `0.1`.
|
||||||
*/
|
*/
|
||||||
constructor(options: Partial<ExponentialBackoffOptions> = {}) {
|
constructor(options: Partial<ExponentialBackoffOptions> = {}) {
|
||||||
this.options = {
|
this.#options = {
|
||||||
maxDelay: 10_000,
|
maxDelay: 10_000,
|
||||||
maxAttempts: 10,
|
maxAttempts: 10,
|
||||||
baseDelay: 1_000,
|
baseDelay: 1_000,
|
||||||
@@ -125,7 +125,7 @@ export class ExponentialBackoff {
|
|||||||
let attempt = 0;
|
let attempt = 0;
|
||||||
|
|
||||||
// If the max attempts is 0, we should continue indefinitely.
|
// If the max attempts is 0, we should continue indefinitely.
|
||||||
const unlimitedAttempts = this.options.maxAttempts === 0;
|
const unlimitedAttempts = this.#options.maxAttempts === 0;
|
||||||
|
|
||||||
// Loop until we succeed, hit the max attempts, or the abort signal is activated
|
// Loop until we succeed, hit the max attempts, or the abort signal is activated
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -146,7 +146,7 @@ export class ExponentialBackoff {
|
|||||||
|
|
||||||
// Calculate the count for next attempt. Do this now so we can exit before waiting and before running the next attempt.
|
// Calculate the count for next attempt. Do this now so we can exit before waiting and before running the next attempt.
|
||||||
const nextAttemptCount = attempt + 1;
|
const nextAttemptCount = attempt + 1;
|
||||||
const nextAttemptExceedsMaxAttempts = nextAttemptCount >= this.options.maxAttempts;
|
const nextAttemptExceedsMaxAttempts = nextAttemptCount >= this.#options.maxAttempts;
|
||||||
|
|
||||||
// If the next attempt exceeds the max attempts, break out of the loop
|
// If the next attempt exceeds the max attempts, break out of the loop
|
||||||
if (!unlimitedAttempts && nextAttemptExceedsMaxAttempts) {
|
if (!unlimitedAttempts && nextAttemptExceedsMaxAttempts) {
|
||||||
@@ -160,7 +160,7 @@ export class ExponentialBackoff {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wait before going to the next attempt
|
// Wait before going to the next attempt
|
||||||
const delay = ExponentialBackoff.calculateDelay(this.options, attempt);
|
const delay = ExponentialBackoff.calculateDelay(this.#options, attempt);
|
||||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||||
|
|
||||||
attempt++;
|
attempt++;
|
||||||
|
|||||||
Reference in New Issue
Block a user