Skip to content

Instantly share code, notes, and snippets.

@HairyMike
Last active August 20, 2020 16:58
Show Gist options
  • Save HairyMike/da75ce31971fcf1674fbceb232eabeef to your computer and use it in GitHub Desktop.
Save HairyMike/da75ce31971fcf1674fbceb232eabeef to your computer and use it in GitHub Desktop.
const request = require("request");
const http = require("http");
var agent = new http.Agent({
keepAlive: true,
//maxSockets: 25, // uncomment this line to fix the problem
keepAliveMsecs: 3000
});
async function asyncGet() {
await new Promise((resolve, reject) => {
setTimeout(function() {
request({
agent: agent,
url: "http://www.google.com",
timeout: 5000,
}, (error, response, body) => {
if (!error) {
console.log(`request got ${response.statusCode}`);
resolve();
} else {
console.log(`request got an error`, error);
reject();
}
});
}, 10);
});
}
async function awaitAll() {
const promises = [...Array(500).keys()].map(() => asyncGet());
await Promise.all(promises);
}
awaitAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment