Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

http: Remove timeout handler when data arrives

  • Loading branch information...
commit 451ff1540ab536237e8d751d241d7fc3391a4087 1 parent 58a5bc1
fredericgermain fredericgermain authored committed
7 lib/http.js
@@ -1474,6 +1474,10 @@ function responseOnEnd() {
1474 1474 assert(!socket.writable);
1475 1475 } else {
1476 1476 debug('AGENT socket keep-alive');
  1477 + if (req.timeoutCb) {
  1478 + socket.setTimeout(0, req.timeoutCb);
  1479 + req.timeoutCb = null;
  1480 + }
1477 1481 socket.removeListener('close', socketCloseListener);
1478 1482 socket.removeListener('error', socketErrorListener);
1479 1483 socket.emit('free');
@@ -1554,6 +1558,9 @@ ClientRequest.prototype.setTimeout = function(msecs, callback) {
1554 1558 }
1555 1559
1556 1560 if (this.socket && this.socket.writable) {
  1561 + if (this.timeoutCb)
  1562 + this.socket.setTimeout(0, this.timeoutCb);
  1563 + this.timeoutCb = emitTimeout;
1557 1564 this.socket.setTimeout(msecs, emitTimeout);
1558 1565 return;
1559 1566 }
89 test/simple/test-http-client-timeout-agent.js
... ... @@ -0,0 +1,89 @@
  1 +// Copyright Joyent, Inc. and other Node contributors.
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining a
  4 +// copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to permit
  8 +// persons to whom the Software is furnished to do so, subject to the
  9 +// following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be included
  12 +// in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +
  22 +var common = require('../common');
  23 +var assert = require('assert');
  24 +var http = require('http');
  25 +
  26 +var request_number = 0;
  27 +var requests_sent = 0;
  28 +var requests_done = 0;
  29 +var options = {
  30 + method: 'GET',
  31 + port: common.PORT,
  32 + host: '127.0.0.1',
  33 +};
  34 +
  35 +//http.globalAgent.maxSockets = 15;
  36 +
  37 +var server = http.createServer(function(req, res) {
  38 + var m = /\/(.*)/.exec(req.url),
  39 + reqid = parseInt(m[1], 10);
  40 + if ( reqid % 2 ) {
  41 + // do not reply the request
  42 + } else {
  43 + res.writeHead(200, {'Content-Type': 'text/plain'});
  44 + res.write(reqid.toString());
  45 + res.end();
  46 + }
  47 + request_number+=1;
  48 +});
  49 +
  50 +server.listen(options.port, options.host, function() {
  51 + var req;
  52 +
  53 + for (requests_sent = 0; requests_sent < 30; requests_sent+=1) {
  54 + options.path = '/' + requests_sent;
  55 + req = http.request(options);
  56 + req.id = requests_sent;
  57 + req.on('response', function(res) {
  58 + res.on('data', function(data) {
  59 + console.log('res#'+this.req.id+' data:'+data);
  60 + });
  61 + res.on('end', function(data) {
  62 + console.log('res#'+this.req.id+' end');
  63 + requests_done += 1;
  64 + });
  65 + });
  66 + req.on('close', function() {
  67 + console.log('req#'+this.id+' close');
  68 + });
  69 + req.on('error', function() {
  70 + console.log('req#'+this.id+' error');
  71 + this.destroy();
  72 + });
  73 + req.setTimeout(50, function () {
  74 + var req = this;
  75 + console.log('req#'+this.id + ' timeout');
  76 + req.abort();
  77 + requests_done += 1;
  78 + });
  79 + req.end();
  80 + }
  81 + setTimeout(function() {
  82 + server.close();
  83 + }, 150);
  84 +});
  85 +
  86 +process.on('exit', function() {
  87 + console.error('done=%j sent=%j', requests_done, requests_sent);
  88 + assert.ok(requests_done == requests_sent, 'timeout on http request called too much');
  89 +});

0 comments on commit 451ff15

Please sign in to comment.
Something went wrong with that request. Please try again.