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: spwan pool on request when use agent #207

Merged
merged 2 commits into from
Oct 26, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function buildRequest (opts) {

function handleUndici (opts, done) {
const req = {
origin: baseUrl || opts.url.origin,
path: opts.url.pathname + opts.qs,
method: opts.method,
headers: Object.assign({}, opts.headers),
Expand All @@ -128,9 +129,7 @@ function buildRequest (opts) {
done(new Error('unix socket not supported with undici yet'))
return
} else {
const options = getUndiciOptions(undiciOpts)
options.dispatcher = undiciAgent
pool = new undici.Pool(baseUrl || opts.url.origin, options)
pool = undiciAgent
}

// remove forbidden headers
Expand Down
77 changes: 77 additions & 0 deletions test/undici-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict'

const t = require('tap')
const Fastify = require('fastify')
const proxyquire = require('proxyquire')
const http = require('http')
const get = require('simple-get').concat
const undici = require('undici')
const { getUndiciOptions } = require('../lib/request')

t.plan(10)

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

const target = http.createServer((req, res) => {
res.statusCode = 200
res.end('hello world')
})

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

t.teardown(target.close.bind(target))

target.listen(0, err => {
t.error(err)
let poolCreation = 0

const From = proxyquire('..', {
'./lib/request.js': proxyquire('../lib/request.js', {
undici: proxyquire('undici', {
'./lib/agent': proxyquire('undici/lib/agent.js', {
'./pool': class Pool extends undici.Pool {
constructor (url, options) {
super(url, options)
poolCreation++
}
}
})
})
})
})

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

instance.listen(0, err => {
t.error(err)

get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(data.toString(), 'hello world')
t.equal(poolCreation, 1)

get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(data.toString(), 'hello world')
t.equal(poolCreation, 1)
})
})
})
})

function buildUndiciOptions () {
return getUndiciOptions({
connections: 42,
pipelining: 24,
keepAliveTimeout: 4242,
strictContentLength: false
})
}
2 changes: 1 addition & 1 deletion test/undici-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { getUndiciOptions } = require('../lib/request')

const instance = Fastify()

t.plan(7)
t.plan(6)
t.teardown(instance.close.bind(instance))

const target = http.createServer((req, res) => {
Expand Down