From 23514a5b04eda33d45c969cd59523252dc7079ef Mon Sep 17 00:00:00 2001 From: tim-kos Date: Mon, 19 Oct 2026 17:23:16 +0200 Subject: [PATCH 1/9] Pass the attempt to every operation timeout and repeat the last forever timeout as a number Co-Authored-By: Claude Opus 5.5 --- README.md | 4 ++-- lib/retry_operation.js | 8 +++++-- test/integration/test-forever.js | 19 ++++++++++++++++ test/integration/test-retry-operation.js | 28 ++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 976f688..a68a6e5 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ Available options: If `forever` is true, the following changes happen: * `RetryOperation.errors()` will only output an array of one item: the last error. -* `RetryOperation` will repeatedly use the `timeouts` array. Once all of its timeouts have been used up, it restarts with the first timeout, then uses the second and so on. +* `RetryOperation` will keep retrying with the last timeout once all of the `timeouts` have been used up. #### retryOperation.errors() @@ -211,7 +211,7 @@ Defines the function `fn` that is to be retried and executes it for the first time right away. The `fn` function can receive an optional `currentAttempt` callback that represents the number of attempts to execute `fn` so far. Optionally defines `timeoutOps` which is an object having a property `timeout` in miliseconds and a property `cb` callback function. -Whenever your retry operation takes longer than `timeout` to execute, the timeout callback function `cb` is called. +Whenever your retry operation takes longer than `timeout` to execute, the timeout callback function `cb` is called with the current attempt number. #### retryOperation.try(fn) diff --git a/lib/retry_operation.js b/lib/retry_operation.js index 105ce72..f8c7a0f 100644 --- a/lib/retry_operation.js +++ b/lib/retry_operation.js @@ -62,7 +62,7 @@ RetryOperation.prototype.retry = function(err) { if (this._cachedTimeouts) { // retry forever, only keep last error this._errors.splice(0, this._errors.length - 1); - timeout = this._cachedTimeouts.slice(-1); + timeout = this._cachedTimeouts[this._cachedTimeouts.length - 1]; } else { return false; } @@ -107,8 +107,12 @@ RetryOperation.prototype.attempt = function(fn, timeoutOps) { var self = this; if (this._operationTimeoutCb) { this._timeout = setTimeout(function() { - self._operationTimeoutCb(); + self._operationTimeoutCb(self._attempts); }, self._operationTimeout); + + if (this._options.unref) { + this._timeout.unref(); + } } this._operationStart = new Date().getTime(); diff --git a/test/integration/test-forever.js b/test/integration/test-forever.js index b41307c..752776a 100644 --- a/test/integration/test-forever.js +++ b/test/integration/test-forever.js @@ -1,6 +1,7 @@ var common = require('../common'); var assert = common.assert; var retry = require(common.dir.lib + '/retry'); +var RetryOperation = require(common.dir.lib + '/retry_operation'); (function testForeverUsesFirstTimeout() { var operation = retry.operation({ @@ -22,3 +23,21 @@ var retry = require(common.dir.lib + '/retry'); } }); })(); + +(function testForeverRepeatsLastTimeout() { + var operation = new RetryOperation([1, 2], {forever: true}); + var originalSetTimeout = global.setTimeout; + var delays = []; + global.setTimeout = function(fn, delay) { + delays.push(delay); + return originalSetTimeout(function() {}, 0); + }; + + operation.attempt(function() {}); + for (var i = 0; i < 4; i++) { + operation.retry(new Error('foo')); + } + global.setTimeout = originalSetTimeout; + + assert.deepStrictEqual(delays, [1, 2, 2, 2]); +})(); diff --git a/test/integration/test-retry-operation.js b/test/integration/test-retry-operation.js index 1176d3e..f2f6cc3 100644 --- a/test/integration/test-retry-operation.js +++ b/test/integration/test-retry-operation.js @@ -291,3 +291,31 @@ var retry = require(common.dir.lib + '/retry'); fn(); })(); + +(function testOperationTimeoutReceivesAttempts() { + var operation = retry.operation([1]); + var timedOutAttempts = []; + + operation.attempt(function() {}, { + timeout: 1, + cb: function(attempts) { + timedOutAttempts.push(attempts); + if (operation.retry(new Error('timeout'))) { + return; + } + assert.deepStrictEqual(timedOutAttempts, [1, 2]); + } + }); +})(); + +(function testOperationTimeoutIsUnrefed() { + var operation = retry.operation({unref: true}); + + operation.attempt(function() {}, { + timeout: 60 * 1000, + cb: function() {} + }); + + assert.strictEqual(operation._timeout.hasRef(), false); + operation.stop(); +})(); From 58570fa5c09de96aad4f1c05c0d9f110806bbd3e Mon Sep 17 00:00:00 2001 From: tim-kos Date: Mon, 19 Oct 2026 17:37:26 +0200 Subject: [PATCH 2/9] Move the timeout tests into their own files and document timeouts()' forever option Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + test/integration/test-forever-timeout.js | 21 ++++++++++++++ test/integration/test-forever.js | 19 ------------- test/integration/test-operation-timeout.js | 32 ++++++++++++++++++++++ test/integration/test-retry-operation.js | 28 ------------------- 5 files changed, 54 insertions(+), 47 deletions(-) create mode 100644 test/integration/test-forever-timeout.js create mode 100644 test/integration/test-operation-timeout.js diff --git a/README.md b/README.md index a68a6e5..4146d65 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ milliseconds. If `options` is an array, a copy of that array is returned. * `minTimeout`: The number of milliseconds before starting the first retry. Default is `1000`. * `maxTimeout`: The maximum number of milliseconds between two retries. Default is `Infinity`. * `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `false`. +* `forever`: Whether the timeouts are for an operation that retries forever. If so and `retries` is `0`, one timeout is still returned, so there is a delay to repeat. Default is `false`. The formula used to calculate the individual timeouts is: diff --git a/test/integration/test-forever-timeout.js b/test/integration/test-forever-timeout.js new file mode 100644 index 0000000..3af083f --- /dev/null +++ b/test/integration/test-forever-timeout.js @@ -0,0 +1,21 @@ +var common = require('../common'); +var assert = common.assert; +var RetryOperation = require(common.dir.lib + '/retry_operation'); + +(function testForeverRepeatsLastTimeout() { + var operation = new RetryOperation([1, 2], {forever: true}); + var originalSetTimeout = global.setTimeout; + var delays = []; + global.setTimeout = function(fn, delay) { + delays.push(delay); + return originalSetTimeout(function() {}, 0); + }; + + operation.attempt(function() {}); + for (var i = 0; i < 4; i++) { + operation.retry(new Error('foo')); + } + global.setTimeout = originalSetTimeout; + + assert.deepStrictEqual(delays, [1, 2, 2, 2]); +})(); diff --git a/test/integration/test-forever.js b/test/integration/test-forever.js index 752776a..b41307c 100644 --- a/test/integration/test-forever.js +++ b/test/integration/test-forever.js @@ -1,7 +1,6 @@ var common = require('../common'); var assert = common.assert; var retry = require(common.dir.lib + '/retry'); -var RetryOperation = require(common.dir.lib + '/retry_operation'); (function testForeverUsesFirstTimeout() { var operation = retry.operation({ @@ -23,21 +22,3 @@ var RetryOperation = require(common.dir.lib + '/retry_operation'); } }); })(); - -(function testForeverRepeatsLastTimeout() { - var operation = new RetryOperation([1, 2], {forever: true}); - var originalSetTimeout = global.setTimeout; - var delays = []; - global.setTimeout = function(fn, delay) { - delays.push(delay); - return originalSetTimeout(function() {}, 0); - }; - - operation.attempt(function() {}); - for (var i = 0; i < 4; i++) { - operation.retry(new Error('foo')); - } - global.setTimeout = originalSetTimeout; - - assert.deepStrictEqual(delays, [1, 2, 2, 2]); -})(); diff --git a/test/integration/test-operation-timeout.js b/test/integration/test-operation-timeout.js new file mode 100644 index 0000000..0978d98 --- /dev/null +++ b/test/integration/test-operation-timeout.js @@ -0,0 +1,32 @@ +var common = require('../common'); +var assert = common.assert; +var retry = require(common.dir.lib + '/retry'); + +(function testOperationTimeoutReceivesAttempts() { + var operation = retry.operation([1]); + var timedOutAttempts = []; + + operation.attempt(function() {}, { + timeout: 1, + cb: function(attempts) { + timedOutAttempts.push(attempts); + operation.retry(new Error('timeout')); + } + }); + + process.on('exit', function() { + assert.deepStrictEqual(timedOutAttempts, [1, 2]); + }); +})(); + +(function testOperationTimeoutIsUnrefed() { + var operation = retry.operation({unref: true}); + + operation.attempt(function() {}, { + timeout: 60 * 1000, + cb: function() {} + }); + + assert.strictEqual(operation._timeout.hasRef(), false); + operation.stop(); +})(); diff --git a/test/integration/test-retry-operation.js b/test/integration/test-retry-operation.js index f2f6cc3..1176d3e 100644 --- a/test/integration/test-retry-operation.js +++ b/test/integration/test-retry-operation.js @@ -291,31 +291,3 @@ var retry = require(common.dir.lib + '/retry'); fn(); })(); - -(function testOperationTimeoutReceivesAttempts() { - var operation = retry.operation([1]); - var timedOutAttempts = []; - - operation.attempt(function() {}, { - timeout: 1, - cb: function(attempts) { - timedOutAttempts.push(attempts); - if (operation.retry(new Error('timeout'))) { - return; - } - assert.deepStrictEqual(timedOutAttempts, [1, 2]); - } - }); -})(); - -(function testOperationTimeoutIsUnrefed() { - var operation = retry.operation({unref: true}); - - operation.attempt(function() {}, { - timeout: 60 * 1000, - cb: function() {} - }); - - assert.strictEqual(operation._timeout.hasRef(), false); - operation.stop(); -})(); From 20cc2cc6c96c8423626fe33fde1bddcb9239c8e5 Mon Sep 17 00:00:00 2001 From: tim-kos Date: Thu, 26 Nov 2026 08:52:43 +0100 Subject: [PATCH 3/9] Address timeout review findings and preserve older Node test compatibility --- README.md | 24 ++++++++++++------- test/integration/test-forever-timeout.js | 11 +++++---- test/integration/test-operation-timeout.js | 28 +++++++++++++++++----- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 4146d65..dc135d7 100644 --- a/README.md +++ b/README.md @@ -103,10 +103,11 @@ main() ### retry.operation([options]) -Creates a new `RetryOperation` object. `options` is the same as `retry.timeouts()`'s `options`, with three additions: +Creates a new `RetryOperation` object. `options` accepts all of `retry.timeouts()`'s options. Setting `forever` to `true` also makes the operation keep retrying after those timeouts run out. -* `forever`: Whether to retry forever, defaults to `false`. -* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) the setTimeout's, defaults to `false`. +Two additional options apply to the operation: + +* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) all retry-delay and per-attempt timeout timers, including the first attempt's timeout. Defaults to `false`. * `maxRetryTime`: The maximum time (in milliseconds) that the retried operation is allowed to run. Default is `Infinity`. ### retry.timeouts([options]) @@ -121,7 +122,7 @@ milliseconds. If `options` is an array, a copy of that array is returned. * `minTimeout`: The number of milliseconds before starting the first retry. Default is `1000`. * `maxTimeout`: The maximum number of milliseconds between two retries. Default is `Infinity`. * `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `false`. -* `forever`: Whether the timeouts are for an operation that retries forever. If so and `retries` is `0`, one timeout is still returned, so there is a delay to repeat. Default is `false`. +* `forever`: Whether the timeouts are for an operation that retries forever. If `true` and `retries` produces no timeouts (for example `0` or `null`), returns an array containing the first computed timeout, based on `minTimeout` and subject to `randomize` and `maxTimeout`. A forever `RetryOperation` repeats this delay between attempts. Default is `false`. The formula used to calculate the individual timeouts is: @@ -186,8 +187,8 @@ Available options: * `unref`: Wether to [unref](https://nodejs.org/api/timers.html#timers_unref) the setTimeout's, defaults to `false`. If `forever` is true, the following changes happen: -* `RetryOperation.errors()` will only output an array of one item: the last error. -* `RetryOperation` will keep retrying with the last timeout once all of the `timeouts` have been used up. +* Once the `timeouts` array has been used up, `RetryOperation.errors()` keeps only the last error. +* `RetryOperation` will keep retrying with the last element of the `timeouts` array once all of the `timeouts` have been used up. `maxRetryTime` and `stop()` still end the retries. #### retryOperation.errors() @@ -209,10 +210,10 @@ If no errors occured so far, the value is `null`. #### retryOperation.attempt(fn, timeoutOps) Defines the function `fn` that is to be retried and executes it for the first -time right away. The `fn` function can receive an optional `currentAttempt` callback that represents the number of attempts to execute `fn` so far. +time right away. The `fn` function receives the current attempt number as its first argument, starting at `1`. -Optionally defines `timeoutOps` which is an object having a property `timeout` in miliseconds and a property `cb` callback function. -Whenever your retry operation takes longer than `timeout` to execute, the timeout callback function `cb` is called with the current attempt number. +Optionally defines `timeoutOps`, an object with a `timeout` in milliseconds and a callback function `cb`. +For each attempt, if `timeout` milliseconds elapse before `retry()` or `stop()` clears the timer, `cb` is called with the same 1-based attempt number passed to `fn`. This is a notification only; your code is responsible for cancelling the work or calling `retry(error)` to retry. #### retryOperation.try(fn) @@ -250,6 +251,11 @@ retry is licensed under the MIT license. # Changelog +Unreleased: + +* The operation-timeout callback now receives `1` on the first attempt, where it previously received no argument. Later attempts already received their attempt number. +* `unref: true` now also applies to the first attempt's operation-timeout timer. If nothing else keeps the event loop alive, the process may exit before this callback runs. + 0.10.0 Adding `stop` functionality, thanks to @maxnachlinger. 0.9.0 Adding `unref` functionality, thanks to @satazor. diff --git a/test/integration/test-forever-timeout.js b/test/integration/test-forever-timeout.js index 3af083f..0593662 100644 --- a/test/integration/test-forever-timeout.js +++ b/test/integration/test-forever-timeout.js @@ -11,11 +11,14 @@ var RetryOperation = require(common.dir.lib + '/retry_operation'); return originalSetTimeout(function() {}, 0); }; - operation.attempt(function() {}); - for (var i = 0; i < 4; i++) { - operation.retry(new Error('foo')); + try { + operation.attempt(function() {}); + for (var i = 0; i < 4; i++) { + operation.retry(new Error('foo')); + } + } finally { + global.setTimeout = originalSetTimeout; } - global.setTimeout = originalSetTimeout; assert.deepStrictEqual(delays, [1, 2, 2, 2]); })(); diff --git a/test/integration/test-operation-timeout.js b/test/integration/test-operation-timeout.js index 0978d98..8723bb8 100644 --- a/test/integration/test-operation-timeout.js +++ b/test/integration/test-operation-timeout.js @@ -21,12 +21,28 @@ var retry = require(common.dir.lib + '/retry'); (function testOperationTimeoutIsUnrefed() { var operation = retry.operation({unref: true}); + var originalSetTimeout = global.setTimeout; + var unrefCalls = 0; - operation.attempt(function() {}, { - timeout: 60 * 1000, - cb: function() {} - }); + global.setTimeout = function(fn, delay) { + var timer = originalSetTimeout(fn, delay); + var originalUnref = timer.unref; + timer.unref = function() { + unrefCalls++; + return originalUnref.call(timer); + }; + return timer; + }; + + try { + operation.attempt(function() {}, { + timeout: 60 * 1000, + cb: function() {} + }); + } finally { + global.setTimeout = originalSetTimeout; + operation.stop(); + } - assert.strictEqual(operation._timeout.hasRef(), false); - operation.stop(); + assert.strictEqual(unrefCalls, 1); })(); From 70dd01e7ec98335583cee43e07a4bb5b74d633e4 Mon Sep 17 00:00:00 2001 From: tim-kos Date: Thu, 26 Nov 2026 09:00:27 +0100 Subject: [PATCH 4/9] Clarify timeout retries and forever error retention --- README.md | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index dc135d7..7b8d6a7 100644 --- a/README.md +++ b/README.md @@ -103,12 +103,12 @@ main() ### retry.operation([options]) -Creates a new `RetryOperation` object. `options` accepts all of `retry.timeouts()`'s options. Setting `forever` to `true` also makes the operation keep retrying after those timeouts run out. +Creates a new `RetryOperation` object. `options` accepts all of `retry.timeouts()`'s options. Setting `forever` to `true` also makes the operation keep retrying after those timeouts run out, waiting the last computed timeout before each further attempt. Two additional options apply to the operation: -* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) all retry-delay and per-attempt timeout timers, including the first attempt's timeout. Defaults to `false`. -* `maxRetryTime`: The maximum time (in milliseconds) that the retried operation is allowed to run. Default is `Infinity`. +* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) all retry-delay and per-attempt timeout timers, including the first attempt's operation timeout (`timeoutOps.timeout`). Defaults to `false`. +* `maxRetryTime`: A time limit in milliseconds, checked when `retry(error)` is called. Once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`. ### retry.timeouts([options]) @@ -122,7 +122,7 @@ milliseconds. If `options` is an array, a copy of that array is returned. * `minTimeout`: The number of milliseconds before starting the first retry. Default is `1000`. * `maxTimeout`: The maximum number of milliseconds between two retries. Default is `Infinity`. * `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `false`. -* `forever`: Whether the timeouts are for an operation that retries forever. If `true` and `retries` produces no timeouts (for example `0` or `null`), returns an array containing the first computed timeout, based on `minTimeout` and subject to `randomize` and `maxTimeout`. A forever `RetryOperation` repeats this delay between attempts. Default is `false`. +* `forever`: When `true`, adds the first computed timeout if `retries` would otherwise produce none (for example `0` or `null`). This timeout is based on `minTimeout` and subject to `randomize` and `maxTimeout`, giving a forever `RetryOperation` a delay to repeat. Default is `false`. The formula used to calculate the individual timeouts is: @@ -184,18 +184,20 @@ a timeout given in milliseconds. Available options: * `forever`: Whether to retry forever, defaults to `false`. -* `unref`: Wether to [unref](https://nodejs.org/api/timers.html#timers_unref) the setTimeout's, defaults to `false`. +* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) all retry-delay and per-attempt timeout timers, including the first attempt's operation timeout (`timeoutOps.timeout`). Defaults to `false`. +* `maxRetryTime`: A time limit in milliseconds, checked when `retry(error)` is called. Once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`. If `forever` is true, the following changes happen: -* Once the `timeouts` array has been used up, `RetryOperation.errors()` keeps only the last error. +* Each retry that reuses the last timeout trims the stored errors to the most recent error. A `retry(error)` call that reaches `maxRetryTime` or occurs after `stop()` skips this trimming. * `RetryOperation` will keep retrying with the last element of the `timeouts` array once all of the `timeouts` have been used up. `maxRetryTime` and `stop()` still end the retries. #### retryOperation.errors() -Returns an array of all errors that have been passed to `retryOperation.retry()` so far. The -returning array has the errors ordered chronologically based on when they were passed to -`retryOperation.retry()`, which means the first passed error is at index zero and the last is -at the last index. +Returns an array of the errors currently stored by the operation. Errors passed to +`retryOperation.retry()` are ordered chronologically. In forever mode, each retry that +reuses the last timeout discards earlier errors. If `maxRetryTime` is reached, the final +error is appended and a `RetryOperation timeout occurred` error is prepended without +trimming existing errors. A `retry(error)` call after `stop()` also appends without trimming. #### retryOperation.mainError() @@ -213,7 +215,9 @@ Defines the function `fn` that is to be retried and executes it for the first time right away. The `fn` function receives the current attempt number as its first argument, starting at `1`. Optionally defines `timeoutOps`, an object with a `timeout` in milliseconds and a callback function `cb`. -For each attempt, if `timeout` milliseconds elapse before `retry()` or `stop()` clears the timer, `cb` is called with the same 1-based attempt number passed to `fn`. This is a notification only; your code is responsible for cancelling the work or calling `retry(error)` to retry. +For each attempt, if `timeout` milliseconds elapse before `retry()` or `stop()` clears the timer, `cb` is called with the current 1-based attempt number. The callback only sends a notification; it does not cancel work or start a retry. + +Call `retry()` at most once per attempt. To retry on timeout, call `retry(error)` from `cb` and cancel the timed-out work or ignore its late result, so its completion handler cannot also call `retry()` or report a result. Otherwise, `fn` can run concurrently or run again after success has been reported. #### retryOperation.try(fn) @@ -254,7 +258,8 @@ retry is licensed under the MIT license. Unreleased: * The operation-timeout callback now receives `1` on the first attempt, where it previously received no argument. Later attempts already received their attempt number. -* `unref: true` now also applies to the first attempt's operation-timeout timer. If nothing else keeps the event loop alive, the process may exit before this callback runs. +* `unref: true` now also applies to the first attempt's operation-timeout timer. If nothing else keeps the event loop alive, the process may exit before the operation-timeout callback runs. +* Forever retries now pass the repeated delay to `setTimeout` as a number instead of a one-element array. Node's retry timing is unchanged: the last timeout was already reused. The README's description of cycling through the timeouts was incorrect. 0.10.0 Adding `stop` functionality, thanks to @maxnachlinger. From 98ddfce93d36bc252bed77f7025f0394d3412e78 Mon Sep 17 00:00:00 2001 From: tim-kos Date: Thu, 26 Nov 2026 09:06:01 +0100 Subject: [PATCH 5/9] Resolve final timeout documentation review findings --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7b8d6a7..a4c755c 100644 --- a/README.md +++ b/README.md @@ -103,12 +103,12 @@ main() ### retry.operation([options]) -Creates a new `RetryOperation` object. `options` accepts all of `retry.timeouts()`'s options. Setting `forever` to `true` also makes the operation keep retrying after those timeouts run out, waiting the last computed timeout before each further attempt. +Creates a new `RetryOperation` object. `options` accepts all of `retry.timeouts()`'s options. Setting `forever` to `true` also makes the operation keep retrying after those timeouts run out, waiting the last (largest) entry in the generated `timeouts` array before each further attempt. Two additional options apply to the operation: * `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) all retry-delay and per-attempt timeout timers, including the first attempt's operation timeout (`timeoutOps.timeout`). Defaults to `false`. -* `maxRetryTime`: A time limit in milliseconds, checked when `retry(error)` is called. Once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`. +* `maxRetryTime`: The maximum total elapsed time in milliseconds since `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. ### retry.timeouts([options]) @@ -185,7 +185,7 @@ a timeout given in milliseconds. Available options: * `forever`: Whether to retry forever, defaults to `false`. * `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) all retry-delay and per-attempt timeout timers, including the first attempt's operation timeout (`timeoutOps.timeout`). Defaults to `false`. -* `maxRetryTime`: A time limit in milliseconds, checked when `retry(error)` is called. Once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`. +* `maxRetryTime`: The maximum total elapsed time in milliseconds since `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. If `forever` is true, the following changes happen: * Each retry that reuses the last timeout trims the stored errors to the most recent error. A `retry(error)` call that reaches `maxRetryTime` or occurs after `stop()` skips this trimming. @@ -215,9 +215,9 @@ Defines the function `fn` that is to be retried and executes it for the first time right away. The `fn` function receives the current attempt number as its first argument, starting at `1`. Optionally defines `timeoutOps`, an object with a `timeout` in milliseconds and a callback function `cb`. -For each attempt, if `timeout` milliseconds elapse before `retry()` or `stop()` clears the timer, `cb` is called with the current 1-based attempt number. The callback only sends a notification; it does not cancel work or start a retry. +For each attempt, if `timeout` milliseconds elapse before `retry()` or `stop()` clears the timer, `cb` is called with the current 1-based attempt number. Calling `cb` is only a notification: the library does not cancel the running work or start a retry on its own. -Call `retry()` at most once per attempt. To retry on timeout, call `retry(error)` from `cb` and cancel the timed-out work or ignore its late result, so its completion handler cannot also call `retry()` or report a result. Otherwise, `fn` can run concurrently or run again after success has been reported. +Call `retry()` at most once per attempt. To retry on timeout, call `retry(error)` from `cb` and ensure the timed-out work's completion handler cannot also call `retry()` or report a result, either by cancelling the work or by ignoring its late result. Ignoring the late result leaves the work running, so cancel it if attempts must not overlap. If the late handler can still call `retry()`, duplicate attempts can run or work can restart after success has been reported. If the `retry(error)` call in `cb` returns `false`, report the final failure from `cb`. #### retryOperation.try(fn) From 7c15784193549503d094a612db1a6b0f0e2503d5 Mon Sep 17 00:00:00 2001 From: tim-kos Date: Thu, 26 Nov 2026 09:22:40 +0100 Subject: [PATCH 6/9] Preserve timeout defaults and add consistent behavior as an opt-in --- README.md | 15 +-- lib/retry.js | 1 + lib/retry_operation.js | 14 ++- test/integration/test-forever-timeout.js | 44 ++++++-- test/integration/test-operation-timeout.js | 118 +++++++++++++++------ 5 files changed, 140 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index a4c755c..28216f9 100644 --- a/README.md +++ b/README.md @@ -105,10 +105,11 @@ main() Creates a new `RetryOperation` object. `options` accepts all of `retry.timeouts()`'s options. Setting `forever` to `true` also makes the operation keep retrying after those timeouts run out, waiting the last (largest) entry in the generated `timeouts` array before each further attempt. -Two additional options apply to the operation: +Three additional options apply to the operation: -* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) all retry-delay and per-attempt timeout timers, including the first attempt's operation timeout (`timeoutOps.timeout`). Defaults to `false`. +* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and operation-timeout timers on retries. The first attempt's operation-timeout timer is also unreferenced when `consistentOperationTimeouts` is `true`. Default is `false`. * `maxRetryTime`: The maximum total elapsed time in milliseconds since `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. +* `consistentOperationTimeouts`: Set to `true` to pass the attempt number to the first operation-timeout callback, apply `unref` to that first timer as well, and pass repeated forever delays to `setTimeout` as numbers. By default, the first callback gets no arguments, the first timer remains referenced even with `unref: true`, and repeated forever delays are passed as one-element arrays. Later attempts already pass their attempt number and honor `unref`. Only the value `true` enables this mode; default is `false`. ### retry.timeouts([options]) @@ -184,8 +185,9 @@ a timeout given in milliseconds. Available options: * `forever`: Whether to retry forever, defaults to `false`. -* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) all retry-delay and per-attempt timeout timers, including the first attempt's operation timeout (`timeoutOps.timeout`). Defaults to `false`. +* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and operation-timeout timers on retries. The first attempt's operation-timeout timer is also unreferenced when `consistentOperationTimeouts` is `true`. Default is `false`. * `maxRetryTime`: The maximum total elapsed time in milliseconds since `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. +* `consistentOperationTimeouts`: The same opt-in behavior described under `retry.operation()`. Default is `false`. If `forever` is true, the following changes happen: * Each retry that reuses the last timeout trims the stored errors to the most recent error. A `retry(error)` call that reaches `maxRetryTime` or occurs after `stop()` skips this trimming. @@ -215,7 +217,7 @@ Defines the function `fn` that is to be retried and executes it for the first time right away. The `fn` function receives the current attempt number as its first argument, starting at `1`. Optionally defines `timeoutOps`, an object with a `timeout` in milliseconds and a callback function `cb`. -For each attempt, if `timeout` milliseconds elapse before `retry()` or `stop()` clears the timer, `cb` is called with the current 1-based attempt number. Calling `cb` is only a notification: the library does not cancel the running work or start a retry on its own. +For each attempt, if `timeout` milliseconds elapse before `retry()` or `stop()` clears the timer, `cb` is called. By default, the first attempt's callback receives no arguments; callbacks on later attempts receive the current 1-based attempt number. With `consistentOperationTimeouts: true`, the first attempt's callback also receives its attempt number. Calling `cb` is only a notification: the library does not cancel the running work or start a retry on its own. Call `retry()` at most once per attempt. To retry on timeout, call `retry(error)` from `cb` and ensure the timed-out work's completion handler cannot also call `retry()` or report a result, either by cancelling the work or by ignoring its late result. Ignoring the late result leaves the work running, so cancel it if attempts must not overlap. If the late handler can still call `retry()`, duplicate attempts can run or work can restart after success has been reported. If the `retry(error)` call in `cb` returns `false`, report the final failure from `cb`. @@ -257,9 +259,8 @@ retry is licensed under the MIT license. Unreleased: -* The operation-timeout callback now receives `1` on the first attempt, where it previously received no argument. Later attempts already received their attempt number. -* `unref: true` now also applies to the first attempt's operation-timeout timer. If nothing else keeps the event loop alive, the process may exit before the operation-timeout callback runs. -* Forever retries now pass the repeated delay to `setTimeout` as a number instead of a one-element array. Node's retry timing is unchanged: the last timeout was already reused. The README's description of cycling through the timeouts was incorrect. +* Add `consistentOperationTimeouts: true` to opt in to consistent timeout behavior: the first operation-timeout callback receives the attempt number, `unref: true` also applies to the first attempt's operation-timeout timer, and forever retries pass repeated delays as numbers instead of one-element arrays. Existing defaults are preserved. In the opt-in mode, an unreferenced first timeout can let the process exit before its callback runs if nothing else keeps the event loop alive. Node's native forever-retry timing is unchanged in both modes. +* Document `retry.timeouts()`'s `forever` option and correct the description of forever retry delays: the last array element is reused, rather than cycling through the array. 0.10.0 Adding `stop` functionality, thanks to @maxnachlinger. diff --git a/lib/retry.js b/lib/retry.js index 5e85e79..639cc72 100644 --- a/lib/retry.js +++ b/lib/retry.js @@ -5,6 +5,7 @@ exports.operation = function(options) { return new RetryOperation(timeouts, { forever: options && (options.forever || options.retries === Infinity), unref: options && options.unref, + consistentOperationTimeouts: options && options.consistentOperationTimeouts, maxRetryTime: options && options.maxRetryTime }); }; diff --git a/lib/retry_operation.js b/lib/retry_operation.js index f8c7a0f..2e519f8 100644 --- a/lib/retry_operation.js +++ b/lib/retry_operation.js @@ -62,7 +62,11 @@ RetryOperation.prototype.retry = function(err) { if (this._cachedTimeouts) { // retry forever, only keep last error this._errors.splice(0, this._errors.length - 1); - timeout = this._cachedTimeouts[this._cachedTimeouts.length - 1]; + if (this._options.consistentOperationTimeouts === true) { + timeout = this._cachedTimeouts[this._cachedTimeouts.length - 1]; + } else { + timeout = this._cachedTimeouts.slice(-1); + } } else { return false; } @@ -107,10 +111,14 @@ RetryOperation.prototype.attempt = function(fn, timeoutOps) { var self = this; if (this._operationTimeoutCb) { this._timeout = setTimeout(function() { - self._operationTimeoutCb(self._attempts); + if (self._options.consistentOperationTimeouts === true) { + self._operationTimeoutCb(self._attempts); + } else { + self._operationTimeoutCb(); + } }, self._operationTimeout); - if (this._options.unref) { + if (this._options.unref && this._options.consistentOperationTimeouts === true) { this._timeout.unref(); } } diff --git a/test/integration/test-forever-timeout.js b/test/integration/test-forever-timeout.js index 0593662..76cdb00 100644 --- a/test/integration/test-forever-timeout.js +++ b/test/integration/test-forever-timeout.js @@ -1,24 +1,50 @@ var common = require('../common'); var assert = common.assert; +var retry = require(common.dir.lib + '/retry'); var RetryOperation = require(common.dir.lib + '/retry_operation'); -(function testForeverRepeatsLastTimeout() { - var operation = new RetryOperation([1, 2], {forever: true}); +function checkDelays(operation, expectedDelays) { var originalSetTimeout = global.setTimeout; - var delays = []; - global.setTimeout = function(fn, delay) { - delays.push(delay); - return originalSetTimeout(function() {}, 0); + var originalClearTimeout = global.clearTimeout; + var timers = []; + var attempts = []; + global.setTimeout = function(callback, delay) { + var timer = {callback: callback, delay: delay}; + timers.push(timer); + return timer; }; + global.clearTimeout = function() {}; try { - operation.attempt(function() {}); + operation.attempt(function(attempt) { attempts.push(attempt); }); for (var i = 0; i < 4; i++) { - operation.retry(new Error('foo')); + assert.strictEqual(operation.retry(new Error('foo')), true); + timers[i].callback(); } + operation.stop(); } finally { global.setTimeout = originalSetTimeout; + global.clearTimeout = originalClearTimeout; } - assert.deepStrictEqual(delays, [1, 2, 2, 2]); + assert.deepStrictEqual(timers.map(function(timer) { return timer.delay; }), expectedDelays); + assert.deepStrictEqual(attempts, [1, 2, 3, 4, 5]); +} + +[undefined, false, true].forEach(function(mode) { + var options = {forever: true}; + if (mode !== undefined) { + options.consistentOperationTimeouts = mode; + } + var expectedDelays = mode === true ? [1, 2, 2, 2] : [1, 2, [2], [2]]; + checkDelays(new RetryOperation([1, 2], options), expectedDelays); + + options.retries = 2; + options.minTimeout = 1; + options.factor = 2; + checkDelays(retry.operation(options), expectedDelays); +}); + +(function testLegacyBooleanForeverSignature() { + checkDelays(new RetryOperation([1, 2], true), [1, 2, [2], [2]]); })(); diff --git a/test/integration/test-operation-timeout.js b/test/integration/test-operation-timeout.js index 8723bb8..4ce5791 100644 --- a/test/integration/test-operation-timeout.js +++ b/test/integration/test-operation-timeout.js @@ -1,48 +1,100 @@ var common = require('../common'); var assert = common.assert; var retry = require(common.dir.lib + '/retry'); +var RetryOperation = require(common.dir.lib + '/retry_operation'); +var execFileSync = require('child_process').execFileSync; -(function testOperationTimeoutReceivesAttempts() { - var operation = retry.operation([1]); - var timedOutAttempts = []; - - operation.attempt(function() {}, { - timeout: 1, - cb: function(attempts) { - timedOutAttempts.push(attempts); - operation.retry(new Error('timeout')); - } - }); - - process.on('exit', function() { - assert.deepStrictEqual(timedOutAttempts, [1, 2]); - }); -})(); - -(function testOperationTimeoutIsUnrefed() { - var operation = retry.operation({unref: true}); +function withTimers(fn) { var originalSetTimeout = global.setTimeout; - var unrefCalls = 0; - - global.setTimeout = function(fn, delay) { - var timer = originalSetTimeout(fn, delay); - var originalUnref = timer.unref; - timer.unref = function() { - unrefCalls++; - return originalUnref.call(timer); + var originalClearTimeout = global.clearTimeout; + var timers = []; + + global.setTimeout = function(callback, delay) { + var timer = { + callback: callback, + delay: delay, + unrefCalls: 0, + unref: function() { this.unrefCalls++; } }; + timers.push(timer); return timer; }; + global.clearTimeout = function(timer) { timer.cleared = true; }; try { - operation.attempt(function() {}, { - timeout: 60 * 1000, - cb: function() {} - }); + fn(timers); } finally { global.setTimeout = originalSetTimeout; - operation.stop(); + global.clearTimeout = originalClearTimeout; } +} + +var factories = [ + function(options) { return retry.operation(options); }, + function(options) { return new RetryOperation([1], options); } +]; - assert.strictEqual(unrefCalls, 1); +factories.forEach(function(createOperation) { + [undefined, false, true].forEach(function(mode) { + [false, true].forEach(function(unref) { + withTimers(function(timers) { + var options = {retries: 1, minTimeout: 1, unref: unref}; + if (mode !== undefined) { + options.consistentOperationTimeouts = mode; + } + var operation = createOperation(options); + var attempts = []; + var timeoutArguments = []; + var timeoutReceivers = []; + var retryResults = []; + + operation.attempt(function(attempt) { + attempts.push(attempt); + }, { + timeout: 10, + cb: function() { + timeoutArguments.push(Array.prototype.slice.call(arguments)); + timeoutReceivers.push(this); + retryResults.push(operation.retry(new Error('timeout'))); + } + }); + + assert.strictEqual(timers[0].delay, 10); + timers[0].callback(); + assert.strictEqual(timers[0].cleared, true); + assert.strictEqual(timers[1].delay, 1); + timers[1].callback(); + assert.strictEqual(timers[2].delay, 10); + timers[2].callback(); + + assert.deepStrictEqual(attempts, [1, 2]); + assert.deepStrictEqual(timeoutArguments, mode === true ? [[1], [2]] : [[], [2]]); + assert.deepStrictEqual(timeoutReceivers, [operation, operation]); + assert.deepStrictEqual(retryResults, [true, false]); + assert.strictEqual(timers.length, 3); + assert.deepStrictEqual(timers.map(function(timer) { + return timer.unrefCalls; + }), unref ? [mode === true ? 1 : 0, 1, 1] : [0, 0, 0]); + }); + }); + }); +}); + +(function testFirstTimeoutProcessLifetime() { + [undefined, false, true].forEach(function(mode) { + [false, true].forEach(function(unref) { + var options = {unref: unref}; + if (mode !== undefined) { + options.consistentOperationTimeouts = mode; + } + var script = 'var retry = require(' + JSON.stringify(common.dir.lib + '/retry') + ');' + + 'retry.operation(' + JSON.stringify(options) + ').attempt(function() {}, {' + + 'timeout: 20, cb: function() { console.log("timeout"); }});'; + var output = execFileSync(process.execPath, ['-e', script], { + encoding: 'utf8', + timeout: 5000 + }); + assert.strictEqual(output, mode === true && unref ? '' : 'timeout\n'); + }); + }); })(); From 8771b5c428ab5820f20bd4e53a70d7ef645f5b9f Mon Sep 17 00:00:00 2001 From: tim-kos Date: Thu, 26 Nov 2026 09:29:16 +0100 Subject: [PATCH 7/9] Cover strict opt-in values and clarify timeout mode effects --- README.md | 8 ++++---- test/integration/test-forever-timeout.js | 2 +- test/integration/test-operation-timeout.js | 22 +++++++++++++--------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 28216f9..283598b 100644 --- a/README.md +++ b/README.md @@ -107,9 +107,9 @@ Creates a new `RetryOperation` object. `options` accepts all of `retry.timeouts( Three additional options apply to the operation: -* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and operation-timeout timers on retries. The first attempt's operation-timeout timer is also unreferenced when `consistentOperationTimeouts` is `true`. Default is `false`. +* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and the operation-timeout timers of later attempts. The first attempt's operation-timeout timer is also unreferenced when `consistentOperationTimeouts` is `true`. Default is `false`. * `maxRetryTime`: The maximum total elapsed time in milliseconds since `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. -* `consistentOperationTimeouts`: Set to `true` to pass the attempt number to the first operation-timeout callback, apply `unref` to that first timer as well, and pass repeated forever delays to `setTimeout` as numbers. By default, the first callback gets no arguments, the first timer remains referenced even with `unref: true`, and repeated forever delays are passed as one-element arrays. Later attempts already pass their attempt number and honor `unref`. Only the value `true` enables this mode; default is `false`. +* `consistentOperationTimeouts`: Set to `true` to pass the attempt number to the first attempt's operation-timeout callback, apply `unref` to that operation-timeout timer as well, and pass the last timeout value directly to `setTimeout` for repeated forever delays. By default, the first attempt's operation-timeout callback gets no arguments, the first attempt's operation-timeout timer remains referenced even with `unref: true`, and repeated forever delays are passed as arrays. Later attempts already pass their attempt number and honor `unref`. Combined with `unref: true`, the process may exit before the first attempt's operation-timeout callback runs if nothing else keeps the event loop alive. The forever delay change applies even without `timeoutOps` and is visible to timer wrappers such as fake timers. Only the value `true` enables this mode; default is `false`. ### retry.timeouts([options]) @@ -185,7 +185,7 @@ a timeout given in milliseconds. Available options: * `forever`: Whether to retry forever, defaults to `false`. -* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and operation-timeout timers on retries. The first attempt's operation-timeout timer is also unreferenced when `consistentOperationTimeouts` is `true`. Default is `false`. +* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and the operation-timeout timers of later attempts. The first attempt's operation-timeout timer is also unreferenced when `consistentOperationTimeouts` is `true`. Default is `false`. * `maxRetryTime`: The maximum total elapsed time in milliseconds since `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. * `consistentOperationTimeouts`: The same opt-in behavior described under `retry.operation()`. Default is `false`. @@ -259,7 +259,7 @@ retry is licensed under the MIT license. Unreleased: -* Add `consistentOperationTimeouts: true` to opt in to consistent timeout behavior: the first operation-timeout callback receives the attempt number, `unref: true` also applies to the first attempt's operation-timeout timer, and forever retries pass repeated delays as numbers instead of one-element arrays. Existing defaults are preserved. In the opt-in mode, an unreferenced first timeout can let the process exit before its callback runs if nothing else keeps the event loop alive. Node's native forever-retry timing is unchanged in both modes. +* Add `consistentOperationTimeouts: true` to opt in to consistent timeout behavior: the first operation-timeout callback receives the attempt number, `unref: true` also applies to the first attempt's operation-timeout timer, and forever retries pass the last timeout value directly instead of an array slice (a number instead of a one-element array for generated timeouts). Existing defaults are preserved. In the opt-in mode, an unreferenced first timeout can let the process exit before its callback runs if nothing else keeps the event loop alive. Node's native forever-retry timing is unchanged in both modes. * Document `retry.timeouts()`'s `forever` option and correct the description of forever retry delays: the last array element is reused, rather than cycling through the array. 0.10.0 Adding `stop` functionality, thanks to @maxnachlinger. diff --git a/test/integration/test-forever-timeout.js b/test/integration/test-forever-timeout.js index 76cdb00..dfbbaad 100644 --- a/test/integration/test-forever-timeout.js +++ b/test/integration/test-forever-timeout.js @@ -31,7 +31,7 @@ function checkDelays(operation, expectedDelays) { assert.deepStrictEqual(attempts, [1, 2, 3, 4, 5]); } -[undefined, false, true].forEach(function(mode) { +[undefined, false, 1, 'true', true].forEach(function(mode) { var options = {forever: true}; if (mode !== undefined) { options.consistentOperationTimeouts = mode; diff --git a/test/integration/test-operation-timeout.js b/test/integration/test-operation-timeout.js index 4ce5791..a8f7468 100644 --- a/test/integration/test-operation-timeout.js +++ b/test/integration/test-operation-timeout.js @@ -35,7 +35,7 @@ var factories = [ ]; factories.forEach(function(createOperation) { - [undefined, false, true].forEach(function(mode) { + [undefined, false, 1, 'true', true].forEach(function(mode) { [false, true].forEach(function(unref) { withTimers(function(timers) { var options = {retries: 1, minTimeout: 1, unref: unref}; @@ -81,20 +81,24 @@ factories.forEach(function(createOperation) { }); (function testFirstTimeoutProcessLifetime() { - [undefined, false, true].forEach(function(mode) { + [undefined, false, 1, 'true', true].forEach(function(mode) { [false, true].forEach(function(unref) { var options = {unref: unref}; if (mode !== undefined) { options.consistentOperationTimeouts = mode; } - var script = 'var retry = require(' + JSON.stringify(common.dir.lib + '/retry') + ');' + - 'retry.operation(' + JSON.stringify(options) + ').attempt(function() {}, {' + - 'timeout: 20, cb: function() { console.log("timeout"); }});'; - var output = execFileSync(process.execPath, ['-e', script], { - encoding: 'utf8', - timeout: 5000 + [false, true].forEach(function(directConstructor) { + var createOperation = directConstructor ? + 'new (require(' + JSON.stringify(common.dir.lib + '/retry_operation') + '))([1], ' + JSON.stringify(options) + ')' : + 'require(' + JSON.stringify(common.dir.lib + '/retry') + ').operation(' + JSON.stringify(options) + ')'; + var script = createOperation + '.attempt(function() {}, {' + + 'timeout: 20, cb: function() { console.log("timeout"); }});'; + var output = execFileSync(process.execPath, ['-e', script], { + encoding: 'utf8', + timeout: 5000 + }); + assert.strictEqual(output, mode === true && unref ? '' : 'timeout\n'); }); - assert.strictEqual(output, mode === true && unref ? '' : 'timeout\n'); }); }); })(); From 4869727c064d3bf06f8439f5388e78672ba6e0e4 Mon Sep 17 00:00:00 2001 From: tim-kos Date: Thu, 26 Nov 2026 09:40:27 +0100 Subject: [PATCH 8/9] Clarify timeout opt-in conditions and elapsed-time checks --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 283598b..16a782a 100644 --- a/README.md +++ b/README.md @@ -107,9 +107,9 @@ Creates a new `RetryOperation` object. `options` accepts all of `retry.timeouts( Three additional options apply to the operation: -* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and the operation-timeout timers of later attempts. The first attempt's operation-timeout timer is also unreferenced when `consistentOperationTimeouts` is `true`. Default is `false`. -* `maxRetryTime`: The maximum total elapsed time in milliseconds since `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. -* `consistentOperationTimeouts`: Set to `true` to pass the attempt number to the first attempt's operation-timeout callback, apply `unref` to that operation-timeout timer as well, and pass the last timeout value directly to `setTimeout` for repeated forever delays. By default, the first attempt's operation-timeout callback gets no arguments, the first attempt's operation-timeout timer remains referenced even with `unref: true`, and repeated forever delays are passed as arrays. Later attempts already pass their attempt number and honor `unref`. Combined with `unref: true`, the process may exit before the first attempt's operation-timeout callback runs if nothing else keeps the event loop alive. The forever delay change applies even without `timeoutOps` and is visible to timer wrappers such as fake timers. Only the value `true` enables this mode; default is `false`. +* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and the operation-timeout timers of later attempts. When `unref` is enabled and `consistentOperationTimeouts` is `true`, the first attempt's operation-timeout timer is also unreferenced. Default is `false`. +* `maxRetryTime`: An elapsed-time threshold in milliseconds, measured from when `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. The check excludes the delay it schedules, so an already scheduled retry can start after `maxRetryTime`. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. +* `consistentOperationTimeouts`: Set to `true` to pass the attempt number to the first attempt's operation-timeout callback, make the `unref` option also apply to that operation-timeout timer, and pass the last timeout value directly to `setTimeout` for repeated forever delays. By default, the first attempt's operation-timeout callback gets no arguments, the first attempt's operation-timeout timer remains referenced even with `unref: true`, and repeated forever delays are passed as arrays. With numeric timeouts, Node's built-in timers use the same forever-retry delays in both modes; timer wrappers can observe the argument-type change. Later attempts already pass their attempt number and honor `unref`. Combined with `unref: true`, the process may exit before the first attempt's operation-timeout callback runs if nothing else keeps the event loop alive. The forever delay change applies even without `timeoutOps` and is visible to timer wrappers such as fake timers. Only the value `true` enables this mode; default is `false`. ### retry.timeouts([options]) @@ -185,8 +185,8 @@ a timeout given in milliseconds. Available options: * `forever`: Whether to retry forever, defaults to `false`. -* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and the operation-timeout timers of later attempts. The first attempt's operation-timeout timer is also unreferenced when `consistentOperationTimeouts` is `true`. Default is `false`. -* `maxRetryTime`: The maximum total elapsed time in milliseconds since `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. +* `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and the operation-timeout timers of later attempts. When `unref` is enabled and `consistentOperationTimeouts` is `true`, the first attempt's operation-timeout timer is also unreferenced. Default is `false`. +* `maxRetryTime`: An elapsed-time threshold in milliseconds, measured from when `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. The check excludes the delay it schedules, so an already scheduled retry can start after `maxRetryTime`. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. * `consistentOperationTimeouts`: The same opt-in behavior described under `retry.operation()`. Default is `false`. If `forever` is true, the following changes happen: @@ -259,7 +259,7 @@ retry is licensed under the MIT license. Unreleased: -* Add `consistentOperationTimeouts: true` to opt in to consistent timeout behavior: the first operation-timeout callback receives the attempt number, `unref: true` also applies to the first attempt's operation-timeout timer, and forever retries pass the last timeout value directly instead of an array slice (a number instead of a one-element array for generated timeouts). Existing defaults are preserved. In the opt-in mode, an unreferenced first timeout can let the process exit before its callback runs if nothing else keeps the event loop alive. Node's native forever-retry timing is unchanged in both modes. +* Add `consistentOperationTimeouts: true` to opt in to consistent timeout behavior: the first operation-timeout callback receives the attempt number, `unref: true` also applies to the first attempt's operation-timeout timer, and forever retries pass the last timeout value directly to `setTimeout` instead of an array slice (a number instead of a one-element array for generated timeouts). Existing defaults are preserved. In the opt-in mode, an unreferenced first timeout can let the process exit before its callback runs if nothing else keeps the event loop alive. With numeric timeouts, Node's built-in timers use the same forever-retry delays in both modes and as in earlier releases. * Document `retry.timeouts()`'s `forever` option and correct the description of forever retry delays: the last array element is reused, rather than cycling through the array. 0.10.0 Adding `stop` functionality, thanks to @maxnachlinger. From d12aca703f89d2165de08f238bdf4fd3f71a5770 Mon Sep 17 00:00:00 2001 From: tim-kos Date: Thu, 26 Nov 2026 09:46:21 +0100 Subject: [PATCH 9/9] Remove overbroad release comparison and define forever delay scope --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 16a782a..42aa641 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ Three additional options apply to the operation: * `unref`: Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) retry-delay timers and the operation-timeout timers of later attempts. When `unref` is enabled and `consistentOperationTimeouts` is `true`, the first attempt's operation-timeout timer is also unreferenced. Default is `false`. * `maxRetryTime`: An elapsed-time threshold in milliseconds, measured from when `attempt()` was called. Retries do not reset this time budget. It is checked when `retry(error)` is called; once reached, that call returns `false` instead of scheduling another attempt. The check excludes the delay it schedules, so an already scheduled retry can start after `maxRetryTime`. It does not interrupt running work. Default is `Infinity`; `0` also means no limit. -* `consistentOperationTimeouts`: Set to `true` to pass the attempt number to the first attempt's operation-timeout callback, make the `unref` option also apply to that operation-timeout timer, and pass the last timeout value directly to `setTimeout` for repeated forever delays. By default, the first attempt's operation-timeout callback gets no arguments, the first attempt's operation-timeout timer remains referenced even with `unref: true`, and repeated forever delays are passed as arrays. With numeric timeouts, Node's built-in timers use the same forever-retry delays in both modes; timer wrappers can observe the argument-type change. Later attempts already pass their attempt number and honor `unref`. Combined with `unref: true`, the process may exit before the first attempt's operation-timeout callback runs if nothing else keeps the event loop alive. The forever delay change applies even without `timeoutOps` and is visible to timer wrappers such as fake timers. Only the value `true` enables this mode; default is `false`. +* `consistentOperationTimeouts`: Set to `true` to pass the attempt number to the first attempt's operation-timeout callback, make the `unref` option also apply to that operation-timeout timer, and pass the last timeout value directly to `setTimeout` for forever delays scheduled after the `timeouts` array has been used up. By default, the first attempt's operation-timeout callback gets no arguments, the first attempt's operation-timeout timer remains referenced even with `unref: true`, and forever delays scheduled after the `timeouts` array has been used up are passed as arrays. With numeric timeouts, Node's built-in timers use the same forever-retry delays in both modes; timer wrappers can observe the argument-type change. Later attempts already pass their attempt number and honor `unref`. Combined with `unref: true`, the process may exit before the first attempt's operation-timeout callback runs if nothing else keeps the event loop alive. The forever delay change applies even without `timeoutOps` and is visible to timer wrappers such as fake timers. Only the value `true` enables this mode; default is `false`. ### retry.timeouts([options]) @@ -259,7 +259,7 @@ retry is licensed under the MIT license. Unreleased: -* Add `consistentOperationTimeouts: true` to opt in to consistent timeout behavior: the first operation-timeout callback receives the attempt number, `unref: true` also applies to the first attempt's operation-timeout timer, and forever retries pass the last timeout value directly to `setTimeout` instead of an array slice (a number instead of a one-element array for generated timeouts). Existing defaults are preserved. In the opt-in mode, an unreferenced first timeout can let the process exit before its callback runs if nothing else keeps the event loop alive. With numeric timeouts, Node's built-in timers use the same forever-retry delays in both modes and as in earlier releases. +* Add `consistentOperationTimeouts: true` to opt in to consistent timeout behavior: the first operation-timeout callback receives the attempt number, `unref: true` also applies to the first attempt's operation-timeout timer, and forever retries scheduled after the `timeouts` array has been used up pass the last timeout value directly to `setTimeout` instead of an array slice (a number instead of a one-element array for generated timeouts). Existing defaults are preserved. In the opt-in mode, an unreferenced first timeout can let the process exit before its callback runs if nothing else keeps the event loop alive. With numeric timeouts, Node's built-in timers use the same forever-retry delays in both modes. * Document `retry.timeouts()`'s `forever` option and correct the description of forever retry delays: the last array element is reused, rather than cycling through the array. 0.10.0 Adding `stop` functionality, thanks to @maxnachlinger.