Navigation Menu

Skip to content

Commit

Permalink
Fixed and hooked up new pool code. cleanup is still not implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Apr 2, 2010
1 parent 4b20d5d commit f1aa5c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
31 changes: 4 additions & 27 deletions lib/balance.js
@@ -1,33 +1,9 @@
var sys = require("sys"),
http = require("http"),
url = require("url"),
pool = require("./pool"),
events = require("events");

var HttpPool = function (options) {
this.options = options;
this.clients = {};
}
HttpPool.prototype.getClient = function (port, hostname) {
var self = this;
if (!this.clients[hostname+':'+port]) {
var clients = [];
this.clients[hostname+':'+port] = clients;
} else {
var clients = this.clients[hostname+':'+port];
}
for (i=0;i<clients.length;i+=1) {
// TODO: Make this work
if (clients[i].busy === false) {
return clients[i];
}
}
var c = http.createClient(port, hostname);
c.busy = true;
c.remove = function () {self.clients[hostname+':'+port].splice(self.clients[hostname+':'+port].indexOf(c),1)}
clients.push(c);
return c;
}

binaryContentTypes = ['application/octet-stream', 'application/ogg', 'application/zip', 'application/pdf',
'image/gif', 'image/jpeg', 'image/png', 'image/tiff', 'image/vnd.microsoft.icon',
'multipart/encrypted', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint',
Expand Down Expand Up @@ -57,7 +33,7 @@ var guessEncoding = function (contentEncoding, contentType) {
}

function Balancer () {
this.pool = new HttpPool();
this.pool = pool.createPool();
}
sys.inherits(Balancer, events.EventEmitter);

Expand All @@ -84,6 +60,8 @@ Balancer.prototype.getRequestHandler = function (clientRequest, clientResponse)
return;
}


var c = self.pool.getGroup(route.port, route.hostname).getClient();
var clientError = function (error) {
clientResponse.writeHead(504, "504 Gateway Timeout", {
"content-type":"text/plain",
Expand All @@ -94,7 +72,6 @@ Balancer.prototype.getRequestHandler = function (clientRequest, clientResponse)
c.removeListener("error", clientError);
c.remove();
}
var c = self.pool.getClient(route.port, route.hostname);
c.addListener("error", clientError)

var proxyRequest = c.request(clientRequest.method, clientRequest.url, clientRequest.headers);
Expand Down
19 changes: 13 additions & 6 deletions lib/pool.js
Expand Up @@ -34,13 +34,13 @@ function createPool (options) {
delete client.response_received;
delete client.endtime;
delete client.starttime;
client.unlockedAt =
client.unlockedAt = Date();
client.busy = false;
}

pool.createGroup = function (port, host) {
var group = { locked:false,
pendingLimit: options.pendingLimit || 10,
pendingLimit: pool.pendingLimit || 10,
limit:false,
clients:[],
}
Expand All @@ -52,30 +52,37 @@ function createPool (options) {
return clients[i];
}
}
if (limit && limit < clients.length) {
if (group.limit && group.limit < clients.length) {
return {error:"Over limit"};
}
var client = pool.createClient(port, host, group);
group.clients.push(client);
return client;
};
group.removeClient = function (client) {
try {client.close()} catch ( ) {}
try {client.close()}
catch(err){}
group.clients.splice(group.clients.indexOf(client), 1);
};
return group;
}

pool.getGroup = function (port, host) {
if (pool.groups[host+":"+port]) {
return pool.groups[host+":"+port];
} else {
pool.groups[host+":"+port] = pool.createGroup(post, host);
var group = pool.createGroup(port, host);
pool.groups[host+":"+port] = group;
return group;
}
}

pool.cleanup = function (client) {
// if ((Date() - client.unlockedAt) > client.pool.)
// return true or false if a client should be removed from the group
}
}
return pool;
}

exports.createPool = createPool;

0 comments on commit f1aa5c7

Please sign in to comment.