Increase maxSockets
suggest changeBasics
require('http').globalAgent.maxSockets = 25
// You can change 25 to Infinity or to a different value by experimenting
Since v0.12.0, http.globalAgent.maxSockets defaults to Infinity. Before that, the default was 5 (see v0.11.0), and further requests waited in a queue. Setting the property to 25 raises the old limit and lowers the current one.
Setting your own agent
The http API uses a global agent. You can supply your own:
const http = require('http')
const myGloriousAgent = new http.Agent({ keepAlive: true })
myGloriousAgent.maxSockets = Infinity
http.request({
hostname: 'example.com',
path: '/',
agent: myGloriousAgent
}, function (res) {
res.resume()
}).end()
Turning off Socket Pooling entirely
const http = require('http')
const options = {
hostname: 'example.com',
path: '/'
}
options.agent = false
const request = http.request(options)
request.end()
Pitfalls
- You should do the same thing for
httpsAPI if you want the same effects - Beware that, for example, AWS will use 50 instead of
Infinity.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents