@lodestar/utils 1.16.0-dev.533db97f18 → 1.16.0-dev.53f8f99c64

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.
package/lib/retry.d.ts CHANGED
@@ -4,11 +4,17 @@ export type RetryOptions = {
4
4
  */
5
5
  retries?: number;
6
6
  /**
7
- * An optional Function that is invoked after the provided callback throws
8
- * It expects a boolean to know if it should retry or not
9
- * Useful to make retrying conditional on the type of error thrown
7
+ * An optional Function that is invoked after the provided callback throws.
8
+ * It expects a boolean to know if it should retry or not.
9
+ * Useful to make retrying conditional on the type of error thrown.
10
10
  */
11
11
  shouldRetry?: (lastError: Error) => boolean;
12
+ /**
13
+ * An optional Function that is invoked right before a retry is performed.
14
+ * It's passed the Error that triggered it and a number identifying the attempt.
15
+ * Useful to track number of retries and errors in logs or metrics.
16
+ */
17
+ onRetry?: (lastError: Error, attempt: number) => unknown;
12
18
  /**
13
19
  * Milliseconds to wait before retrying again
14
20
  */
package/lib/retry.js CHANGED
@@ -7,15 +7,25 @@ import { sleep } from "./sleep.js";
7
7
  */
8
8
  export async function retry(fn, opts) {
9
9
  const maxRetries = opts?.retries ?? 5;
10
+ // Number of retries + the initial attempt
11
+ const maxAttempts = maxRetries + 1;
10
12
  const shouldRetry = opts?.shouldRetry;
13
+ const onRetry = opts?.onRetry;
11
14
  let lastError = Error("RetryError");
12
- for (let i = 1; i <= maxRetries; i++) {
15
+ for (let i = 1; i <= maxAttempts; i++) {
13
16
  try {
17
+ // If not the first attempt, invoke right before retrying
18
+ if (i > 1)
19
+ onRetry?.(lastError, i);
14
20
  return await fn(i);
15
21
  }
16
22
  catch (e) {
17
23
  lastError = e;
18
- if (shouldRetry && !shouldRetry(lastError)) {
24
+ if (i === maxAttempts) {
25
+ // Reached maximum number of attempts, there's no need to check if we should retry
26
+ break;
27
+ }
28
+ else if (shouldRetry && !shouldRetry(lastError)) {
19
29
  break;
20
30
  }
21
31
  else if (opts?.retryDelay !== undefined) {
package/lib/retry.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"retry.js","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AAoBjC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAI,EAAuC,EAAE,IAAmB;IACzF,MAAM,UAAU,GAAG,IAAI,EAAE,OAAO,IAAI,CAAC,CAAC;IACtC,MAAM,WAAW,GAAG,IAAI,EAAE,WAAW,CAAC;IAEtC,IAAI,SAAS,GAAU,KAAK,CAAC,YAAY,CAAC,CAAC;IAC3C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,UAAU,EAAE,CAAC,EAAE,EAAE;QACpC,IAAI;YACF,OAAO,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;SACpB;QAAC,OAAO,CAAC,EAAE;YACV,SAAS,GAAG,CAAU,CAAC;YACvB,IAAI,WAAW,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE;gBAC1C,MAAM;aACP;iBAAM,IAAI,IAAI,EAAE,UAAU,KAAK,SAAS,EAAE;gBACzC,MAAM,KAAK,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;aAC7C;SACF;KACF;IACD,MAAM,SAAS,CAAC;AAClB,CAAC"}
1
+ {"version":3,"file":"retry.js","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AA0BjC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAI,EAAuC,EAAE,IAAmB;IACzF,MAAM,UAAU,GAAG,IAAI,EAAE,OAAO,IAAI,CAAC,CAAC;IACtC,0CAA0C;IAC1C,MAAM,WAAW,GAAG,UAAU,GAAG,CAAC,CAAC;IACnC,MAAM,WAAW,GAAG,IAAI,EAAE,WAAW,CAAC;IACtC,MAAM,OAAO,GAAG,IAAI,EAAE,OAAO,CAAC;IAE9B,IAAI,SAAS,GAAU,KAAK,CAAC,YAAY,CAAC,CAAC;IAC3C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,WAAW,EAAE,CAAC,EAAE,EAAE;QACrC,IAAI;YACF,yDAAyD;YACzD,IAAI,CAAC,GAAG,CAAC;gBAAE,OAAO,EAAE,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;YAEnC,OAAO,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;SACpB;QAAC,OAAO,CAAC,EAAE;YACV,SAAS,GAAG,CAAU,CAAC;YAEvB,IAAI,CAAC,KAAK,WAAW,EAAE;gBACrB,kFAAkF;gBAClF,MAAM;aACP;iBAAM,IAAI,WAAW,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE;gBACjD,MAAM;aACP;iBAAM,IAAI,IAAI,EAAE,UAAU,KAAK,SAAS,EAAE;gBACzC,MAAM,KAAK,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;aAC7C;SACF;KACF;IACD,MAAM,SAAS,CAAC;AAClB,CAAC"}
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/ChainSafe/lodestar/issues"
13
13
  },
14
- "version": "1.16.0-dev.533db97f18",
14
+ "version": "1.16.0-dev.53f8f99c64",
15
15
  "type": "module",
16
16
  "exports": "./lib/index.js",
17
17
  "files": [
@@ -58,5 +58,5 @@
58
58
  "beacon",
59
59
  "blockchain"
60
60
  ],
61
- "gitHead": "990f388e4f2bb4fee539415e2e361d68aff5f661"
61
+ "gitHead": "2536a9754d55fac8d093d83e8ee9c8428bfddf4d"
62
62
  }