Navigation Menu

Skip to content

Commit

Permalink
Much better reuse of clients and cleanup of clients. Not tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Apr 8, 2010
1 parent 9d53047 commit 0b217c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/balance.js
Expand Up @@ -89,7 +89,7 @@ Balancer.prototype.getRequestHandler = function (clientRequest, clientResponse)
response.addListener("end", function () {
clientResponse.close();
c.removeListener("error", clientError);
c.busy = false;
c.unlock();
})
// work-around for end event bug in node
if (clientRequest.method == 'HEAD') {
Expand Down
14 changes: 9 additions & 5 deletions lib/pool.js
Expand Up @@ -6,6 +6,8 @@ function createPool (options) {

pool.createClient = function (port, host, group) {
var client = http.createClient(port, host);
client.lock = function () {pool.lock(client)};
client.unlock = function () {pool.unlock(client)};
client.group = group;
var _request = client.request;
client.request = function () {
Expand All @@ -22,6 +24,7 @@ function createPool (options) {
client.addListener("error", function (error) {
group.removeClient(client);
})

return client;
}

Expand All @@ -30,13 +33,18 @@ function createPool (options) {
client.pending_request = 0;
client.locktime = Date();
client.response_received = false;
clearTimeout(client.cleanup);
}
pool.unlock = function (client) {
delete client.response_received;
delete client.endtime;
delete client.starttime;
client.unlockedAt = Date();
client.busy = false;
client.cleanup = function () {
try {client.group.removeClient(client);} catch() {}
}
setTimeout(client.cleanup, 1000 * 60);
}

pool.createGroup = function (port, host) {
Expand All @@ -49,7 +57,7 @@ function createPool (options) {
var clients = group.clients;
for (var i=0;i<clients.length;i+=1) {
if (!clients[i].busy) {
pool.lock(clients[i]);
clients[i].lock();
return clients[i];
}
}
Expand Down Expand Up @@ -78,10 +86,6 @@ function createPool (options) {
}
}

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;
}

Expand Down

0 comments on commit 0b217c3

Please sign in to comment.