diff --git a/lib/middleware/proxy.js b/lib/middleware/proxy.js index af7aa95a4..7c9a43214 100644 --- a/lib/middleware/proxy.js +++ b/lib/middleware/proxy.js @@ -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) { @@ -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() } @@ -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) }