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: retry on body support #3294

Merged
merged 7 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 27 additions & 4 deletions lib/handler/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ const assert = require('node:assert')

const { kRetryHandlerDefaultRetry } = require('../core/symbols')
const { RequestRetryError } = require('../core/errors')
const { isDisturbed, parseHeaders, parseRangeHeader } = require('../core/util')
const {
isStream,
isDisturbed,
isIterable,
isAsyncIterable,
parseHeaders,
parseRangeHeader
} = require('../core/util')

function calculateRetryAfterHeader (retryAfter) {
const current = Date.now()
return new Date(retryAfter).getTime() - current
}

class RetryHandler {
#isBodyIteratorLike = false
constructor (opts, handlers) {
const { retryOptions, ...dispatchOpts } = opts
const {
Expand Down Expand Up @@ -63,6 +71,10 @@ class RetryHandler {
this.end = null
this.etag = null
this.resume = null
this.#isBodyIteratorLike =
typeof this.opts.body === 'object' &&
!isStream(this.opts.body) &&
(isIterable(this.opts.body) || isAsyncIterable(this.opts.body))

// Handle possible onConnect duplication
this.handler.onConnect(reason => {
Expand Down Expand Up @@ -174,7 +186,9 @@ class RetryHandler {
this.abort(
new RequestRetryError('Request failed', statusCode, {
headers,
count: this.retryCount
data: {
count: this.retryCount
}
})
)
return false
Expand Down Expand Up @@ -278,7 +292,7 @@ class RetryHandler {

const err = new RequestRetryError('Request failed', statusCode, {
headers,
count: this.retryCount
data: { count: this.retryCount }
})

this.abort(err)
Expand All @@ -298,7 +312,16 @@ class RetryHandler {
}

onError (err) {
if (this.aborted || isDisturbed(this.opts.body)) {
console.log(
this.aborted,
isDisturbed(this.opts.body),
this.#isBodyIteratorLike
)
if (
this.aborted ||
isDisturbed(this.opts.body) ||
this.#isBodyIteratorLike
) {
return this.handler.onError(err)
}

Expand Down
213 changes: 212 additions & 1 deletion test/retry-handler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, ubuntu-latest) / Test with Node.js 21 on ubuntu-latest

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest) / Test with Node.js 18 on ubuntu-latest

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms', code: 'ERR_TEST_FAILURE' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest) / Test with Node.js 20 on ubuntu-latest

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (22, ubuntu-latest) / Test with Node.js 22 on ubuntu-latest

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, macos-latest) / Test with Node.js 18 on macos-latest

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms', code: 'ERR_TEST_FAILURE' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, macos-latest) / Test with Node.js 21 on macos-latest

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (22, macos-latest) / Test with Node.js 22 on macos-latest

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 21 compiled --without-intl

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 22 compiled --without-intl

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 20 compiled --without-intl

test/retry-handler.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
const { createServer } = require('node:http')
const { once } = require('node:events')
const { Readable } = require('node:stream')

const { RetryHandler, Client } = require('..')
const { RequestHandler } = require('../lib/api/api-request')
Expand Down Expand Up @@ -204,6 +205,75 @@
await t.completed
})

test('Issue #3288', { only: true }, async t => {

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, ubuntu-latest) / Test with Node.js 21 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest) / Test with Node.js 18 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest) / Test with Node.js 20 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (22, ubuntu-latest) / Test with Node.js 22 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, macos-latest) / Test with Node.js 18 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, macos-latest) / Test with Node.js 21 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (22, macos-latest) / Test with Node.js 22 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 21 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 22 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 208 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 20 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.
t = tspl(t, { plan: 6 })
const server = createServer()
const dispatchOptions = {
method: 'POST',
path: '/',
headers: {
'content-type': 'application/json'
},
body: (function * () {
yield 'hello'
yield 'world'
})()
}

server.on('request', (req, res) => {
res.writeHead(500, {
'content-type': 'application/json'
})

res.end('{"message": "failed"}')
})

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
const handler = new RetryHandler(dispatchOptions, {
dispatch: client.dispatch.bind(client),
handler: {
onConnect () {
t.ok(true, 'pass')
},
onBodySent () {
t.ok(true, 'pass')
},
onHeaders (status, _rawHeaders, resume, _statusMessage) {
t.strictEqual(status, 500)
return true
},
onData (chunk) {
console.log('chunk', chunk)
return true
},
onComplete () {
t.fail()
},
onError (err) {
t.equal(err.message, 'Request failed')
t.equal(err.statusCode, 500)
t.equal(err.data.count, 1)
}
}
})

after(async () => {
await client.close()
server.close()

await once(server, 'close')
})

client.dispatch(
dispatchOptions,
handler
)
})

await t.completed
})

test('Should use retry-after header for retries', async t => {
t = tspl(t, { plan: 4 })

Expand Down Expand Up @@ -655,7 +725,7 @@
await t.completed
})

test('retrying a request with a body', async t => {
test('retrying a request with a body', { only: true }, async t => {

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, ubuntu-latest) / Test with Node.js 21 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest) / Test with Node.js 18 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest) / Test with Node.js 20 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (22, ubuntu-latest) / Test with Node.js 22 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, macos-latest) / Test with Node.js 18 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, macos-latest) / Test with Node.js 21 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (22, macos-latest) / Test with Node.js 22 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 21 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 22 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 728 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 20 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.
let counter = 0
const server = createServer()
const dispatchOptions = {
Expand Down Expand Up @@ -685,6 +755,7 @@
t = tspl(t, { plan: 1 })

server.on('request', (req, res) => {
console.log({ counter })
switch (counter) {
case 0:
req.destroy()
Expand Down Expand Up @@ -734,6 +805,146 @@
await t.completed
})

test('retrying a request with a body (stream)', { only: true }, async t => {

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, ubuntu-latest) / Test with Node.js 21 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest) / Test with Node.js 18 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest) / Test with Node.js 20 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (22, ubuntu-latest) / Test with Node.js 22 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, macos-latest) / Test with Node.js 18 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, macos-latest) / Test with Node.js 21 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (22, macos-latest) / Test with Node.js 22 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 21 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 22 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 808 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 20 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.
let counter = 0
const server = createServer()
const dispatchOptions = {
retryOptions: {
retry: (err, { state, opts }, done) => {
counter++

if (
err.statusCode === 500 ||
err.message.includes('other side closed')
) {
setTimeout(done, 500)
return
}

return done(err)
}
},
method: 'POST',
path: '/',
headers: {
'content-type': 'application/json'
},
body: Readable.from(Buffer.from(JSON.stringify({ hello: 'world' })))
}

t = tspl(t, { plan: 3 })

server.on('request', (req, res) => {
console.log({ counter })
switch (counter) {
case 0:
res.writeHead(500)
res.end('failed')
return
default:
t.fail()
}
})

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.equal(err.statusCode, 500)
t.equal(err.data.count, 1)
t.equal(err.code, 'UND_ERR_REQ_RETRY')
})
})

after(async () => {
await client.close()
server.close()

await once(server, 'close')
})

client.dispatch(
dispatchOptions,
handler
)
})

await t.completed
})

test('retrying a request with a body (buffer)', { only: true }, async t => {
let counter = 0
const server = createServer()
const dispatchOptions = {
retryOptions: {
retry: (err, { state, opts }, done) => {
counter++

if (
err.statusCode === 500 ||
err.message.includes('other side closed')
) {
setTimeout(done, 500)
return
}

return done(err)
}
},
method: 'POST',
path: '/',
headers: {
'content-type': 'application/json'
},
body: Buffer.from(JSON.stringify({ hello: 'world' }))
}

t = tspl(t, { plan: 1 })

server.on('request', (req, res) => {
switch (counter) {
case 0:
req.destroy()
return
case 1:
res.writeHead(500)
res.end('failed')
return
case 2:
res.writeHead(200)
res.end('hello world!')
return
default:
t.fail()
}
})

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(
dispatchOptions,
handler
)
})

await t.completed
})

test('should not error if request is not meant to be retried', async t => {
t = tspl(t, { plan: 3 })

Expand Down
Loading