Skip to content

Instantly share code, notes, and snippets.

@yannooo
Created April 5, 2011 09:39
Show Gist options
  • Save yannooo/903338 to your computer and use it in GitHub Desktop.
Save yannooo/903338 to your computer and use it in GitHub Desktop.
var http = require('http');
var agent = http.getAgent('127.0.0.1', 8124);
agent.maxSockets = 10;
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World\n');
//res.end();
});
server.listen(8124, "127.0.0.1");
for (var i = 0; i < 20; i++) {
var options = {
host: '127.0.0.1',
port: 8124,
path: '/',
}
http.get(options, function(res) {
});
console.log('Socket: ' + agent.sockets.length + '/' + agent.maxSockets + ' queued: '+ agent.queue.length);
}
var http = require('http');
//--------------------------------------------------------------------------
// Fixes a bug in node http client that prevents using maxSockets connection
//--------------------------------------------------------------------------
var assert = require('assert').ok;
var debug;
if (process.env.NODE_DEBUG && /http/.test(process.env.NODE_DEBUG)) {
debug = function(x) { console.error('HTTP: %s', x); };
} else {
debug = function() { };
}
function httpSocketSetup(socket) {
// NOTE: be sure not to use ondrain elsewhere in this file!
socket.ondrain = function() {
if (socket._httpMessage) {
socket._httpMessage.emit('drain');
}
};
}
http.Agent.prototype._cycle = function() {
debug('Agent _cycle sockets=' + this.sockets.length + ' queue=' + this.queue.length);
var self = this;
var first = this.queue[0];
if (!first) return;
var haveConnectingSocket = false;
// First try to find an available socket.
for (var i = 0; i < this.sockets.length; i++) {
var socket = this.sockets[i];
// If the socket doesn't already have a message it's sending out
// and the socket is available for writing or it's connecting.
// In particular this rules out sockets that are closing.
if (!socket._httpMessage &&
((socket.writable && socket.readable) || socket._httpConnecting)) {
debug('Agent found socket, shift');
// We found an available connection!
this.queue.shift(); // remove first from queue.
assert(first._queue === this.queue);
first._queue = null;
first.assignSocket(socket);
httpSocketSetup(socket);
process.nextTick(function() {
first.emit('start');
});
self._cycle(); // try to dispatch another
return;
}
//if (socket._httpConnecting) haveConnectingSocket = true;
}
// If no sockets are connecting, and we have space for another we should
// be starting a new connection to handle this request.
if (!haveConnectingSocket && this.sockets.length < this.maxSockets) {
this._establishNewConnection();
}
// All sockets are filled and all sockets are busy.
};
//--------------------------------------------------------------------------
// end bug fix
//--------------------------------------------------------------------------
@mbrevoort
Copy link

Yann, do you know if this bug has been fixed in core?

@yannooo
Copy link
Author

yannooo commented Jan 24, 2012

I haven't tested, but I think it has been fixed in the 0.6 branch

@mbrevoort
Copy link

ok, thanks! I'll try to confirm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment