From a1864bad269cfd94246d66cd96429fc23b4a1903 Mon Sep 17 00:00:00 2001 From: Alessandro Commodari Date: Thu, 14 Mar 2024 17:38:31 -0400 Subject: [PATCH 1/2] fix: fix retry handler option --- lib/handler/retry-handler.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/handler/retry-handler.js b/lib/handler/retry-handler.js index f2e22f6d4c0..15894470ff3 100644 --- a/lib/handler/retry-handler.js +++ b/lib/handler/retry-handler.js @@ -1,3 +1,4 @@ +'use strict' const assert = require('node:assert') const { kRetryHandlerDefaultRetry } = require('../core/symbols') @@ -37,7 +38,7 @@ class RetryHandler { retry: retryFn ?? RetryHandler[kRetryHandlerDefaultRetry], retryAfter: retryAfter ?? true, maxTimeout: maxTimeout ?? 30 * 1000, // 30s, - timeout: minTimeout ?? 500, // .5s + minTimeout: minTimeout ?? 500, // .5s timeoutFactor: timeoutFactor ?? 2, maxRetries: maxRetries ?? 5, // What errors we should retry @@ -104,7 +105,7 @@ class RetryHandler { const { method, retryOptions } = opts const { maxRetries, - timeout, + minTimeout, maxTimeout, timeoutFactor, statusCodes, @@ -114,7 +115,7 @@ class RetryHandler { let { counter, currentTimeout } = state currentTimeout = - currentTimeout != null && currentTimeout > 0 ? currentTimeout : timeout + currentTimeout != null && currentTimeout > 0 ? currentTimeout : minTimeout // Any code that is not a Undici's originated and allowed to retry if ( From a921d47affaae5cedfe7bb1e3be3584202141886 Mon Sep 17 00:00:00 2001 From: Alessandro Commodari Date: Fri, 15 Mar 2024 14:54:59 -0400 Subject: [PATCH 2/2] fix: add test --- test/retry-handler.js | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/test/retry-handler.js b/test/retry-handler.js index f6b83cd34dd..2792f23c2b0 100644 --- a/test/retry-handler.js +++ b/test/retry-handler.js @@ -706,3 +706,75 @@ test('should not error if request is not meant to be retried', async t => { await t.completed }) + +test('Should be able to properly pass the minTimeout to the RetryContext when constructing a RetryCallback function', async t => { + t = tspl(t, { plan: 2 }) + + let counter = 0 + const server = createServer() + server.on('request', (req, res) => { + switch (counter) { + case 0: + res.writeHead(500) + res.end('failed') + return + case 1: + res.writeHead(200) + res.end('hello world!') + return + default: + t.fail() + } + }) + + const dispatchOptions = { + retryOptions: { + retry: (err, { state, opts }, done) => { + counter++ + t.strictEqual(opts.retryOptions.minTimeout, 100) + + if (err.statusCode === 500) { + return done() + } + + return done(err) + }, + minTimeout: 100 + }, + method: 'GET', + path: '/', + headers: { + 'content-type': 'application/json' + } + } + + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`) + const handler = new RetryHandler(dispatchOptions, { + dispatch: client.dispatch.bind(client), + handler: new RequestHandler(dispatchOptions, (err, data) => { + t.ifError(err) + }) + }) + + after(async () => { + await client.close() + server.close() + + await once(server, 'close') + }) + + client.dispatch( + { + method: 'GET', + path: '/', + headers: { + 'content-type': 'application/json' + } + }, + handler + ) + }) + + await t.completed +})