Skip to content

Instantly share code, notes, and snippets.

@scic
Last active March 7, 2018 09:39
Show Gist options
  • Save scic/466db4687f62fa23c0bc059c31ae179a to your computer and use it in GitHub Desktop.
Save scic/466db4687f62fa23c0bc059c31ae179a to your computer and use it in GitHub Desktop.
/**
* This simulates a browser aborting XHR requests
*
* needs: npm install request
*/
var request = require('request');
var batch = 20;
var abortTime = 50 // timed so that many requests are aborted
function get(i) {
var req = request('http://localhost:9001', function (error, response, body) {
if (!error) {
console.log('successful', i);
} else {
console.log('error', i);
}
});
setTimeout( () => req.abort(), abortTime);
}
setInterval(() => {
console.log(`Do ${batch} requests and abort each after ${abortTime}`);
for(var i=0; i<batch; i++) {
get(i);
}
}, 100);
/**
* This listens for a request from the client then does
* a GET request on another server and pipes the result back.
*
* I do a
* watch -n 0.5 cat /proc/net/sockstat
* to monitor the tcp mem stats.
*/
var http = require('http');
http.createServer(function (req, res) {
var connector = http.get('http://jsonplaceholder.typicode.com/photos', received => received.pipe(res));
req.pipe(connector);
}).listen(9001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment