Skip to content

Commit

Permalink
fix: spwan pool on request when use agent (#207)
Browse files Browse the repository at this point in the history
* fix: spwan pool on request when use agent

* test: ensure pool is not spawn twice for same host
  • Loading branch information
climba03003 authored Oct 26, 2021
1 parent 95f2d48 commit 6a31087
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
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

0 comments on commit 6a31087

Please sign in to comment.