]> git.ardis.lu - csvio/commitdiff
Add `interval` option to `retry` strategy
authorArdis Lu <ardisglu@gmail.com>
Wed, 26 Feb 2025 05:56:37 +0000 (21:56 -0800)
committerArdis Lu <ardisglu@gmail.com>
Wed, 26 Feb 2025 05:56:37 +0000 (21:56 -0800)
src/errorStrategies.js
test/errorStrategies.test.js

index 508fa4d70bbbc91fed9d474b803919edb83a965f..b9a24a2f18a2317285fd1a8cbe916776bafb8a4e 100644 (file)
@@ -33,6 +33,8 @@ function placeholder(value) {
 
 /**
  * @typedef {Object} RetryOptions
+ * @property {number} [interval] The duration (in milliseconds) to wait in between each retry. The default value is `0`
+ * (retry immediately).
  * @property {string} [placeholder] A placeholder value to set if the function still errors after the final iteration.
  * If unset, the final error thrown by the function is re-thrown.
  */
@@ -47,8 +49,10 @@ function placeholder(value) {
  * @throws If `value` is unset and the function throws an error after the final iteration, the error is re-thrown.
  */
 function retry(iterations, options = {}) {
+  options.interval ??= 0;
   return async (row, e, fn) => {
     while (iterations--) {
+      await utils.sleep(options.interval);
       try { return await fn(row); }
       catch { }
     }
index 54eda6e8e0914069c4b1404bead662e532947872..2ea3b9895b303527dd3bc9d2f9a8964b13c4e488 100644 (file)
@@ -133,6 +133,19 @@ suite('ErrorStrategies.retry', { concurrency: true }, () => {
         [10]
       ]));
   });
+  test('retries with interval', async (t) => {
+    assertSleep(t, { min: 10 * 1000, max: 10 * 1000 + 1000 });
+    const fn = retryFactory(11); // 1st try + 10 retries = pass on 11th with extra retries available
+    await createCSVMockStream([
+      ['header'],
+      ['0']
+    ])
+      .pipeThrough(new CSVTransformer(fn, { onError: retry(10, { interval: 1000 }) }))
+      .pipeTo(csvStreamEqualWritable([
+        ['header'],
+        [11]
+      ]));
+  });
   test('sets placeholder if failed after last iteration and placeholder is given', async () => {
     const fn = retryFactory(100);
     await createCSVMockStream([
This page took 0.036864 seconds and 4 git commands to generate.