Browse Source

Improved agent (#240)

* improves agent

- reverts spdy for now (causing issues with alias)
- sets up an agent pool of 4 max sockets with a 10s keep alive timeout
- better error handling and re-initialization

* improves agent

- reverts spdy for now (causing issues with alias)
- sets up an agent pool of 4 max sockets with a 10s keep alive timeout
- better error handling and re-initialization
master
Guillermo Rauch 8 years ago
committed by Leo Lamprecht
parent
commit
5fd5802880
  1. 33
      lib/agent.js
  2. 7
      lib/alias.js

33
lib/agent.js

@ -1,6 +1,7 @@
// Packages // Packages
const {parse} = require('url') const {parse} = require('url')
const http2 = require('spdy') const http = require('http')
const https = require('https')
const fetch = require('node-fetch') const fetch = require('node-fetch')
/** /**
@ -18,8 +19,6 @@ module.exports = class Agent {
constructor(url, {tls = true, debug} = {}) { constructor(url, {tls = true, debug} = {}) {
this._url = url this._url = url
const parsed = parse(url) const parsed = parse(url)
this._host = parsed.hostname
this._port = parsed.port
this._protocol = parsed.protocol this._protocol = parsed.protocol
this._debug = debug this._debug = debug
if (tls) { if (tls) {
@ -28,30 +27,28 @@ module.exports = class Agent {
} }
_initAgent() { _initAgent() {
if (this._protocol !== 'https:') { const module = this._protocol === 'https:' ? https : http
return const agent = this._agent = new module.Agent({
} keepAlive: true,
keepAliveMsecs: 10000,
this._agent = http2.createAgent({ maxSockets: 8
host: this._host, }).on('error', err => this._onError(err, agent))
port: this._port || 443
}).once('error', err => this._onError(err))
} }
_onError(err) { _onError(err, agent) {
if (this._debug) { if (this._debug) {
console.log('> [debug] agent connection error', err.stack) console.log('> [debug] agent connection error', err.stack)
} }
this._error = err if (this._agent === agent) {
this._agent = null
}
} }
fetch(path, opts = {}) { fetch(path, opts = {}) {
if (this._error) { if (!this._agent) {
if (this._debug) { if (this._debug) {
console.log('> [debug] re-initializing agent after error') console.log('> [debug] re-initializing agent')
} }
this._error = null
this._initAgent() this._initAgent()
} }
@ -78,7 +75,7 @@ module.exports = class Agent {
} }
if (this._agent) { if (this._agent) {
this._agent.close() this._agent.destroy()
} }
} }
} }

7
lib/alias.js

@ -230,13 +230,6 @@ module.exports = class Alias extends Now {
console.log(`> Verification ${chalk.bold('OK')}!`) 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) const newAlias = await this.createAlias(depl, alias)
if (!newAlias) { if (!newAlias) {
throw new Error(`Unexpected error occurred while setting up alias: ${JSON.stringify(newAlias)}`) throw new Error(`Unexpected error occurred while setting up alias: ${JSON.stringify(newAlias)}`)

Loading…
Cancel
Save