Skip to content

Commit

Permalink
fix: fix retry handler option (#2962)
Browse files Browse the repository at this point in the history
  • Loading branch information
acommodari committed Mar 16, 2024
1 parent 3be7ec9 commit 9a56796
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
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
})

0 comments on commit 9a56796

Please sign in to comment.