Skip to content

Commit b77f94c

Browse files
authoredJun 11, 2020
feat(proxy): use keepAlive agent (#3527)
Using an agent with keepAlive improves performance as the socket is reused across multiple requests.
1 parent 41fa9c0 commit b77f94c

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed
 

‎lib/middleware/proxy.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const url = require('url')
2+
const { Agent: httpAgent } = require('http')
3+
const { Agent: httpsAgent } = require('https')
24
const httpProxy = require('http-proxy')
35
const _ = require('lodash')
46

@@ -42,11 +44,14 @@ function parseProxyConfig (proxies, config) {
4244
port = config.port
4345
}
4446
const changeOrigin = proxyConfiguration.changeOrigin || false
47+
const Agent = https ? httpsAgent : httpAgent
48+
const agent = new Agent({ keepAlive: true })
4549
const proxy = httpProxy.createProxyServer({
4650
target: { host: hostname, port, https, protocol },
4751
xfwd: true,
4852
changeOrigin: changeOrigin,
49-
secure: config.proxyValidateSSL
53+
secure: config.proxyValidateSSL,
54+
agent
5055
})
5156

5257
;['proxyReq', 'proxyRes'].forEach(function (name) {
@@ -66,7 +71,7 @@ function parseProxyConfig (proxies, config) {
6671
res.destroy()
6772
})
6873

69-
return { path: proxyPath, baseUrl: pathname, host: hostname, port, https, proxy }
74+
return { path: proxyPath, baseUrl: pathname, host: hostname, port, https, proxy, agent }
7075
}), 'path').reverse()
7176
}
7277

@@ -112,6 +117,14 @@ function createProxyHandler (proxies, urlRoot) {
112117
return createProxy
113118
}
114119

115-
exports.create = function (/* config */config, /* config.proxies */proxies) {
116-
return createProxyHandler(parseProxyConfig(proxies, config), config.urlRoot)
120+
exports.create = function (/* config */config, /* config.proxies */proxies, /* emitter */emitter) {
121+
const proxyRecords = parseProxyConfig(proxies, config)
122+
emitter.on('exit', (done) => {
123+
log.debug('Destroying proxy agents')
124+
proxyRecords.forEach((proxyRecord) => {
125+
proxyRecord.agent.destroy()
126+
})
127+
done()
128+
})
129+
return createProxyHandler(proxyRecords, config.urlRoot)
117130
}

‎test/unit/middleware/proxy.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,24 @@ describe('middleware.proxy', () => {
352352
it('should handle empty proxy config', () => {
353353
expect(m.parseProxyConfig({})).to.deep.equal([])
354354
})
355+
356+
it('should use http agent with keepAlive=true', () => {
357+
const proxy = { '/base': 'http://localhost:8000/proxy' }
358+
const parsedProxyConfig = m.parseProxyConfig(proxy, {})
359+
expect(parsedProxyConfig).to.have.length(1)
360+
expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({
361+
keepAlive: true,
362+
protocol: 'http:'
363+
})
364+
})
365+
366+
it('should use https agent with keepAlive=true', () => {
367+
const proxy = { '/base': 'https://localhost:8000/proxy' }
368+
const parsedProxyConfig = m.parseProxyConfig(proxy, {})
369+
expect(parsedProxyConfig).to.have.length(1)
370+
expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({
371+
keepAlive: true,
372+
protocol: 'https:'
373+
})
374+
})
355375
})

0 commit comments

Comments
 (0)
Please sign in to comment.