Skip to content

Commit f1c335f

Browse files
acommodarimetcoder95
authored andcommitted
fix: fix retry handler option (#2962)
1 parent 8810a8a commit f1c335f

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

lib/handler/retry-handler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict'
12
const assert = require('node:assert')
23

34
const { kRetryHandlerDefaultRetry } = require('../core/symbols')
@@ -37,7 +38,7 @@ class RetryHandler {
3738
retry: retryFn ?? RetryHandler[kRetryHandlerDefaultRetry],
3839
retryAfter: retryAfter ?? true,
3940
maxTimeout: maxTimeout ?? 30 * 1000, // 30s,
40-
timeout: minTimeout ?? 500, // .5s
41+
minTimeout: minTimeout ?? 500, // .5s
4142
timeoutFactor: timeoutFactor ?? 2,
4243
maxRetries: maxRetries ?? 5,
4344
// What errors we should retry
@@ -105,7 +106,7 @@ class RetryHandler {
105106
const { method, retryOptions } = opts
106107
const {
107108
maxRetries,
108-
timeout,
109+
minTimeout,
109110
maxTimeout,
110111
timeoutFactor,
111112
statusCodes,

test/retry-handler.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,3 +804,75 @@ test('should not error if request is not meant to be retried', async t => {
804804

805805
await t.completed
806806
})
807+
808+
test('Should be able to properly pass the minTimeout to the RetryContext when constructing a RetryCallback function', async t => {
809+
t = tspl(t, { plan: 2 })
810+
811+
let counter = 0
812+
const server = createServer()
813+
server.on('request', (req, res) => {
814+
switch (counter) {
815+
case 0:
816+
res.writeHead(500)
817+
res.end('failed')
818+
return
819+
case 1:
820+
res.writeHead(200)
821+
res.end('hello world!')
822+
return
823+
default:
824+
t.fail()
825+
}
826+
})
827+
828+
const dispatchOptions = {
829+
retryOptions: {
830+
retry: (err, { state, opts }, done) => {
831+
counter++
832+
t.strictEqual(opts.retryOptions.minTimeout, 100)
833+
834+
if (err.statusCode === 500) {
835+
return done()
836+
}
837+
838+
return done(err)
839+
},
840+
minTimeout: 100
841+
},
842+
method: 'GET',
843+
path: '/',
844+
headers: {
845+
'content-type': 'application/json'
846+
}
847+
}
848+
849+
server.listen(0, () => {
850+
const client = new Client(`http://localhost:${server.address().port}`)
851+
const handler = new RetryHandler(dispatchOptions, {
852+
dispatch: client.dispatch.bind(client),
853+
handler: new RequestHandler(dispatchOptions, (err, data) => {
854+
t.ifError(err)
855+
})
856+
})
857+
858+
after(async () => {
859+
await client.close()
860+
server.close()
861+
862+
await once(server, 'close')
863+
})
864+
865+
client.dispatch(
866+
{
867+
method: 'GET',
868+
path: '/',
869+
headers: {
870+
'content-type': 'application/json'
871+
}
872+
},
873+
handler
874+
)
875+
})
876+
877+
await t.completed
878+
})

0 commit comments

Comments
 (0)