Skip to content

Instantly share code, notes, and snippets.

@shuhei
Last active May 14, 2017 17:52
Show Gist options
  • Save shuhei/b17cbdb03077cf7bb9bdd933c19baa45 to your computer and use it in GitHub Desktop.
Save shuhei/b17cbdb03077cf7bb9bdd933c19baa45 to your computer and use it in GitHub Desktop.
request() timeout
const net = require('net');
const dns = require('dns');
const request = require('request');
const start = Date.now();
function log() {
const time = (Date.now() - start).toString();
console.log.apply(console, [time].concat(Array.from(arguments)));
}
function delay(func, msecs) {
return function () {
const args = arguments;
setTimeout(() => {
func.apply(this, args);
}, msecs);
};
}
function isRequestEnd(str) {
const len = str.length;
if (len < 4) {
return false;
}
return str[len - 4] === '\r' &&
str[len - 3] === '\n' &&
str[len - 2] === '\r' &&
str[len - 1] === '\n';
}
const server = net.createServer((socket) => {
let buffer = '';
socket.setEncoding('utf8');
socket.on('data', (chunk) => {
buffer += chunk;
if (isRequestEnd(buffer)) {
log('[server]', buffer);
setTimeout(() => {
socket.write('HTTP/1.1 200 OK\r\n');
setTimeout(() => {
socket.write('Content-Length: 5\r\n');
socket.write('\r\n');
socket.write('hello');
socket.end();
}, 200);
}, 200);
}
});
});
server.listen(() => {
const port = server.address().port;
log('[server] Listening on', port);
// Don't specify `path` with `createConnection` because it tries to
// connect to the domain socket specified by the `path`.
const req = request({
port: port,
url: `http://localhost:${port}/`,
// Cannot have delay on net.createConnection easily because it has to return
// a socket synchronously.
// createConnection: net.createConnection,
lookup: delay(dns.lookup, 200),
timeout: 250,
// timeout: 150,
}, (error, res, body) => {
if (error) {
log('[client] error:', error);
server.close();
return;
}
log('[client]', res.statusCode, res.headers);
log('[client]', body);
server.close();
});
req.end();
log('[client] making request');
});
$ node request.js
10 [server] Listening on 53543
25 [client] making request
240 [server] GET / HTTP/1.1
host: localhost:53543
Connection: close


279 [client] error: { Error: ETIMEDOUT
    at Timeout._onTimeout (/Users/shuhei/work/js/timeout/node_modules/request/request.js:759:15)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5) code: 'ETIMEDOUT', connect: false }
events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:306:12)
    at Timeout.setTimeout [as _onTimeout] (/Users/shuhei/work/js/timeout/request.js:40:16)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)

request@^2.76.0

$ node request.js
5 [server] Listening on 53557
24 [client] making request
231 [server] GET / HTTP/1.1
host: localhost:53557
Connection: close


644 [client] 200 { 'content-length': '5' }
647 [client] hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment