Skip to content

Instantly share code, notes, and snippets.

@mikeal
Created August 2, 2010 05:32
Show Gist options
  • Save mikeal/504156 to your computer and use it in GitHub Desktop.
Save mikeal/504156 to your computer and use it in GitHub Desktop.
function createPool (port, host, https, credentials) {
var pool = new events.EventEmitter();
pool.clients = [];
var getClient = function () {
for (var i=0;i<pool.clients.length;i+=1) {
if (!pool.clients[i].busy) {
return pool.clients[i];
}
}
var client = createClient(port, host, https, credentials);
clients.push(client);
return client;
};
pool.request = function (method, url, headers) {
if (!headers) header = {};
if (!headers.connection) headers.connection = 'keep-alive';
var client = getClient();
var request = client.request(method, url, headers);
var errorListener = function (error) {
pool.emit("error", error);
request.emit("error", error);
}
client.addListener(errorListener);
request.addListener("response", function (response) {
response.addListener("end", function () {
client.removeListener(errorListener);
client.busy = false;
})
})
client.busy = true;
return request;
};
pool.end = function () {
pool.clients.forEach(function (c) {c.end()});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment