Skip to content

Commit

Permalink
Simplified pool implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Feb 4, 2011
1 parent 004741c commit 8548541
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ var toBase64 = function(str) {

var isUrl = /^https?:/;

var agents = {};
var getAgent = function (host, port) {
return agents[host+':'+port] || new http.Agent();
}

var Request = function (options) {
stream.Stream.call(this);
this.readable = true;
Expand All @@ -38,8 +33,15 @@ var Request = function (options) {
for (i in options) {
this[i] = options[i];
}
if (!this.pool) this.pool = {};
}
util.inherits(Request, stream.Stream);
Request.prototype.getAgent = function (host, port) {
if (!this.pool[host+':'+port]) {
this.pool[host+':'+port] = new http.Agent(host, port);
}
return this.pool[host+':'+port];
}

Request.prototype.pipe = function (dest) {
this.dest = dest;
Expand Down Expand Up @@ -155,10 +157,13 @@ Request.prototype.request = function () {
}
}

options.agent = getAgent(options.host, options.port);
options.agent = options.getAgent(options.host, options.port);
if (options.maxSockets) {
options.agent.maxSockets = options.maxSockets;
}
if (options.pool.maxSockets) {
options.agent.maxSockets = options.pool.maxSockets;
}

options.req = http.request(options, function (response) {
if (setHost) delete options.headers.host;
Expand Down

0 comments on commit 8548541

Please sign in to comment.