Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix retry handler option #2962

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/handler/retry-handler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'
const assert = require('node:assert')

const { kRetryHandlerDefaultRetry } = require('../core/symbols')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -104,7 +105,7 @@ class RetryHandler {
const { method, retryOptions } = opts
const {
maxRetries,
timeout,
minTimeout,
maxTimeout,
timeoutFactor,
statusCodes,
Expand All @@ -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 (
Expand Down
72 changes: 72 additions & 0 deletions test/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Loading