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: undici connect timeout (#302) #303

Merged
merged 4 commits into from
Mar 17, 2023
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
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
ServiceUnavailableError,
GatewayTimeoutError,
ConnectionResetError,
ConnectTimeoutError,
UndiciSocketError,
InternalServerError
} = require('./lib/errors')
Expand Down Expand Up @@ -157,6 +158,8 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
onError(this, { error: new ConnectionResetError() })
} else if (err.code === 'UND_ERR_SOCKET') {
onError(this, { error: new UndiciSocketError() })
} else if (err.code === 'UND_ERR_CONNECT_TIMEOUT') {
onError(this, { error: new ConnectTimeoutError() })
} else {
onError(this, { error: new InternalServerError(err.message) })
}
Expand Down
1 change: 1 addition & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ module.exports.Http2SessionTimeoutError = createError('FST_REPLY_FROM_HTTP2_SESS
module.exports.ServiceUnavailableError = createError('FST_REPLY_FROM_SERVICE_UNAVAILABLE', 'Service Unavailable', 503)
module.exports.GatewayTimeoutError = createError('FST_REPLY_FROM_GATEWAY_TIMEOUT', 'Gateway Timeout', 504)
module.exports.ConnectionResetError = createError('ECONNRESET', 'Connection Reset', 500)
module.exports.ConnectTimeoutError = createError('UND_ERR_CONNECT_TIMEOUT', 'Connect Timeout Error', 500)
module.exports.UndiciSocketError = createError('UND_ERR_SOCKET', 'Undici Socket Error', 500)
module.exports.InternalServerError = createError('FST_REPLY_FROM_INTERNAL_SERVER_ERROR', '%s', 500)
2 changes: 1 addition & 1 deletion test/disable-request-logging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ t.test('use a custom instance of \'undici\'', async t => {
await new Promise((resolve, reject) => target.listen({ port: 0 }, err => err ? reject(err) : resolve()))

t.test('disableRequestLogging is set to true', t => {
t.plan(10)
t.plan(9)
const logStream = split(JSON.parse)
const instance = Fastify({
logger: {
Expand Down
58 changes: 58 additions & 0 deletions test/undici-connect-timeout.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict'

const t = require('tap')
const http = require('http')
const net = require('net')
const Fastify = require('fastify')
const From = require('..')
const got = require('got')

t.autoend(false)

// never connect
net.connect = function (options) {
return new net.Socket(options)
}

const target = http.createServer((req, res) => {
t.fail('target never called')
})

async function main () {
t.plan(2)
await target.listen({ port: 0 })

const instance = Fastify()
t.teardown(instance.close.bind(instance))
t.teardown(target.close.bind(target))

instance.register(From, {
base: `http://localhost:${target.address().port}`,
undici: {
connectTimeout: 50
}
})

instance.get('/', (request, reply) => {
reply.from()
})

await instance.listen({ port: 0 })

try {
await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
} catch (err) {
t.equal(err.response.statusCode, 500)
t.same(JSON.parse(err.response.body), {
statusCode: 500,
code: 'UND_ERR_CONNECT_TIMEOUT',
error: 'Internal Server Error',
message: 'Connect Timeout Error'
})
return
}

t.fail()
}

main()