diff --git a/lib/agent.js b/lib/agent.js index 7b03011..de743ce 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -1,6 +1,7 @@ // Packages const {parse} = require('url') -const http2 = require('spdy') +const http = require('http') +const https = require('https') const fetch = require('node-fetch') /** @@ -18,8 +19,6 @@ module.exports = class Agent { constructor(url, {tls = true, debug} = {}) { this._url = url const parsed = parse(url) - this._host = parsed.hostname - this._port = parsed.port this._protocol = parsed.protocol this._debug = debug if (tls) { @@ -28,30 +27,28 @@ module.exports = class Agent { } _initAgent() { - if (this._protocol !== 'https:') { - return - } - - this._agent = http2.createAgent({ - host: this._host, - port: this._port || 443 - }).once('error', err => this._onError(err)) + const module = this._protocol === 'https:' ? https : http + const agent = this._agent = new module.Agent({ + keepAlive: true, + keepAliveMsecs: 10000, + maxSockets: 8 + }).on('error', err => this._onError(err, agent)) } - _onError(err) { + _onError(err, agent) { if (this._debug) { console.log('> [debug] agent connection error', err.stack) } - this._error = err + if (this._agent === agent) { + this._agent = null + } } fetch(path, opts = {}) { - if (this._error) { + if (!this._agent) { if (this._debug) { - console.log('> [debug] re-initializing agent after error') + console.log('> [debug] re-initializing agent') } - - this._error = null this._initAgent() } @@ -78,7 +75,7 @@ module.exports = class Agent { } if (this._agent) { - this._agent.close() + this._agent.destroy() } } } diff --git a/lib/alias.js b/lib/alias.js index 51d9ce3..d337471 100644 --- a/lib/alias.js +++ b/lib/alias.js @@ -230,13 +230,6 @@ module.exports = class Alias extends Now { console.log(`> Verification ${chalk.bold('OK')}!`) } - // unfortunately there's a situation where the verification - // ownership code path in the `catch` above makes the - // agent unexpectedly close. this is a workaround until - // we figure out what's going on with `node-spdy` - this._agent.close() - this._agent._initAgent() - const newAlias = await this.createAlias(depl, alias) if (!newAlias) { throw new Error(`Unexpected error occurred while setting up alias: ${JSON.stringify(newAlias)}`)