Created
August 23, 2018 21:53
-
-
Save crccheck/5f6423db56b11066995a479e2057034c to your computer and use it in GitHub Desktop.
timeout is a good idea
This file contains hidden or 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
// In another window, run | |
// | |
// $ nc -kl 4200 | |
// | |
// -k keepalive (BSD/OSX only) | |
// -l 4200 listen on port 4200 | |
const requestP = require('request-promise-native'); | |
const now = Date.now(); | |
// never finishes | |
const defaults = {}; | |
// n = 100, 599 ms | |
// n = 1000, 1484 ms | |
const timeout = { | |
timeout: 500, | |
}; | |
// never finishes, gets through three calls every 25 seconds though | |
const pool = { | |
pool: { maxSockets: 5 }, | |
}; | |
// n = 100, 10132 ms | |
// n = 1000, 100698 ms | |
const timeoutPool = { | |
...timeout, | |
...pool, | |
}; | |
const request = requestP.defaults(timeoutPool); | |
async function main(n = 1000) { | |
return Promise.all(new Array(n).fill().map( | |
async (x, idx) => { | |
try { | |
await request.get('http://localhost:4200'); | |
} catch (err) { | |
console.error(idx, Date.now() - now, err.message); | |
} | |
})); | |
} | |
main() | |
.then(() => console.log('Finished in %d ms', Date.now() - now)) | |
.catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment