Skip to content

Commit

Permalink
fix(proxy): destroy agents on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed May 25, 2020
1 parent 15590a7 commit f25edeb
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/middleware/proxy.js
Expand Up @@ -45,13 +45,13 @@ function parseProxyConfig (proxies, config) {
}
const changeOrigin = proxyConfiguration.changeOrigin || false
const Agent = https ? httpsAgent : httpAgent
const keepAliveAgent = new Agent({ keepAlive: true })
const agent = new Agent({ keepAlive: true })
const proxy = httpProxy.createProxyServer({
target: { host: hostname, port, https, protocol },
xfwd: true,
changeOrigin: changeOrigin,
secure: config.proxyValidateSSL,
agent: keepAliveAgent
agent
})

;['proxyReq', 'proxyRes'].forEach(function (name) {
Expand All @@ -71,7 +71,7 @@ function parseProxyConfig (proxies, config) {
res.destroy()
})

return { path: proxyPath, baseUrl: pathname, host: hostname, port, https, proxy }
return { path: proxyPath, baseUrl: pathname, host: hostname, port, https, proxy, agent }
}), 'path').reverse()
}

Expand Down Expand Up @@ -117,6 +117,14 @@ function createProxyHandler (proxies, urlRoot) {
return createProxy
}

exports.create = function (/* config */config, /* config.proxies */proxies) {
return createProxyHandler(parseProxyConfig(proxies, config), config.urlRoot)
exports.create = function (/* config */config, /* config.proxies */proxies, /* emitter */emitter) {
const proxyRecords = parseProxyConfig(proxies, config)
emitter.on('exit', (done) => {
log.debug('Destroying proxy agents')
proxyRecords.forEach((proxyRecord) => {
proxyRecord.agent.destroy()
})
done()
})
return createProxyHandler(proxyRecords, config.urlRoot)
}

0 comments on commit f25edeb

Please sign in to comment.