Add ability to disable the http agent (#1251)

This commit is contained in:
Tomas Della Vedova
2020-07-13 15:07:51 +02:00
committed by GitHub
parent 39cf023426
commit 1592c4d575
8 changed files with 117 additions and 13 deletions

View File

@ -38,12 +38,13 @@ class Connection {
if (typeof opts.agent === 'function') {
this.agent = opts.agent()
} else if (opts.agent === false) {
this.agent = undefined
} else {
const keepAliveFalse = opts.agent && opts.agent.keepAlive === false
const agentOptions = Object.assign({}, {
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: keepAliveFalse ? Infinity : 256,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo'
}, opts.agent)
@ -146,7 +147,9 @@ class Connection {
if (this._openRequests > 0) {
setTimeout(() => this.close(callback), 1000)
} else {
this.agent.destroy()
if (this.agent !== undefined) {
this.agent.destroy()
}
callback()
}
}