Last active
August 20, 2020 16:58
-
-
Save HairyMike/da75ce31971fcf1674fbceb232eabeef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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