@dcrosstech/dct-ts-utils 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,8 @@
1
1
  export declare class RetryError<Args = unknown> extends Error {
2
2
  readonly retryArgs: Args;
3
3
  readonly finalError?: Error;
4
- constructor(message: string, retryArgs: Args, finalError?: Error);
4
+ readonly retryAfter?: number;
5
+ constructor(message: string, retryArgs: Args, finalError?: Error, retryAfter?: number);
5
6
  }
6
7
  export declare class TerminalError extends Error {
7
8
  constructor(message: string);
package/dist/lib/retry.js CHANGED
@@ -1,10 +1,11 @@
1
1
  import { sleep } from "./sleep";
2
2
  export class RetryError extends Error {
3
- constructor(message, retryArgs, finalError) {
3
+ constructor(message, retryArgs, finalError, retryAfter) {
4
4
  super(message);
5
5
  this.name = "RetryError";
6
6
  this.retryArgs = retryArgs;
7
7
  this.finalError = finalError;
8
+ this.retryAfter = retryAfter;
8
9
  }
9
10
  }
10
11
  export class TerminalError extends Error {
@@ -73,7 +74,8 @@ function computeDelayMs(q, minD, maxD, factor, maxE, jitter, retryAttempt) {
73
74
  export async function executeWithExponentialBackoff(cfg, fn, initialArgs) {
74
75
  const { q, minD, maxD, maxR, f, maxE, j } = normalizeConfig(cfg);
75
76
  let args = initialArgs;
76
- for (let attemptIndex = 0;; attemptIndex++) {
77
+ let retryAfter;
78
+ for (let attempt = 0;; attempt++) {
77
79
  try {
78
80
  return await fn(args);
79
81
  }
@@ -84,18 +86,21 @@ export async function executeWithExponentialBackoff(cfg, fn, initialArgs) {
84
86
  if (err instanceof RetryError) {
85
87
  args = err.retryArgs;
86
88
  finalOnExhaustion = err.finalError;
89
+ retryAfter = err.retryAfter;
87
90
  }
88
91
  // Out of retries?
89
- if (maxR !== undefined && attemptIndex >= maxR) {
92
+ if (maxR !== undefined && attempt >= maxR) {
90
93
  if (finalOnExhaustion) {
91
94
  throw finalOnExhaustion;
92
95
  }
93
96
  throw err;
94
97
  }
95
- const retryAttempt = attemptIndex + 1; // 1-based retry count
96
- const delayMs = computeDelayMs(q, minD, maxD, f, maxE, j, retryAttempt);
97
- if (delayMs > 0) {
98
- await sleep(delayMs);
98
+ const retryAttempt = attempt + 1; // 1-based retry count
99
+ if (!retryAfter) {
100
+ retryAfter = computeDelayMs(q, minD, maxD, f, maxE, j, retryAttempt);
101
+ }
102
+ if (retryAfter > 0) {
103
+ await sleep(retryAfter);
99
104
  }
100
105
  }
101
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dcrosstech/dct-ts-utils",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Common TypeScript utilities (sleep, semaphore, queues, heaps, exponential backoff)",
5
5
  "license": "ISC",
6
6
  "author": "David Cross <david@crossfamilyweb.com>",