2010-09-29 12:18:46 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var sys = require('sys');
|
|
|
|
var net = require('net');
|
2010-11-15 13:04:41 +08:00
|
|
|
var Client = require(__dirname+'/client');
|
2010-12-11 07:32:34 +08:00
|
|
|
var defaults = require(__dirname + '/defaults');
|
2011-03-02 04:35:14 +08:00
|
|
|
var Pool = require(__dirname + '/utils').Pool;
|
2010-12-14 07:21:40 +08:00
|
|
|
|
|
|
|
//wrap up common connection management boilerplate
|
|
|
|
var connect = function(config, callback) {
|
|
|
|
if(poolEnabled()) {
|
|
|
|
return getPooledClient(config, callback)
|
|
|
|
}
|
2010-12-14 13:28:30 +08:00
|
|
|
|
2010-12-14 07:21:40 +08:00
|
|
|
var client = new Client(config);
|
|
|
|
client.connect();
|
|
|
|
|
|
|
|
var onError = function(error) {
|
|
|
|
client.connection.removeListener('readyForQuery', onReady);
|
|
|
|
callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
var onReady = function() {
|
|
|
|
client.removeListener('error', onError);
|
|
|
|
callback(null, client);
|
|
|
|
client.on('drain', client.end.bind(client));
|
|
|
|
}
|
|
|
|
|
|
|
|
client.once('error', onError);
|
|
|
|
|
|
|
|
//TODO refactor
|
|
|
|
//i don't like reaching into the client's connection for attaching
|
|
|
|
//to specific events here
|
|
|
|
client.connection.once('readyForQuery', onReady);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-11 07:32:34 +08:00
|
|
|
//connection pool global cache
|
|
|
|
var clientPools = {
|
|
|
|
}
|
|
|
|
|
|
|
|
var poolEnabled = function() {
|
|
|
|
return defaults.poolSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
var log = function() {
|
2010-12-14 07:21:40 +08:00
|
|
|
//do nothing
|
2010-12-11 07:32:34 +08:00
|
|
|
}
|
|
|
|
|
2010-12-14 07:21:40 +08:00
|
|
|
//for testing
|
|
|
|
// var log = function() {
|
|
|
|
// console.log.apply(console, arguments);
|
|
|
|
// }
|
2010-12-11 07:32:34 +08:00
|
|
|
|
|
|
|
var getPooledClient = function(config, callback) {
|
|
|
|
//lookup pool using config as key
|
|
|
|
//TODO this don't work so hot w/ object configs
|
|
|
|
var pool = clientPools[config];
|
|
|
|
|
|
|
|
//create pool if doesn't exist
|
|
|
|
if(!pool) {
|
2010-12-15 11:21:28 +08:00
|
|
|
//log("creating pool %s", config)
|
2010-12-11 07:32:34 +08:00
|
|
|
pool = clientPools[config] = new Pool(defaults.poolSize, function() {
|
2010-12-15 11:21:28 +08:00
|
|
|
//log("creating new client in pool %s", config)
|
2010-12-11 07:32:34 +08:00
|
|
|
var client = new Client(config);
|
|
|
|
client.connected = false;
|
|
|
|
return client;
|
|
|
|
})
|
|
|
|
}
|
2010-12-14 07:21:40 +08:00
|
|
|
|
2010-12-11 07:32:34 +08:00
|
|
|
pool.checkOut(function(err, client) {
|
2010-12-14 07:21:40 +08:00
|
|
|
|
2010-12-11 07:32:34 +08:00
|
|
|
//if client already connected just
|
|
|
|
//pass it along to the callback and return
|
|
|
|
if(client.connected) {
|
|
|
|
callback(null, client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var onError = function(error) {
|
|
|
|
client.connection.removeListener('readyForQuery', onReady);
|
|
|
|
callback(error);
|
|
|
|
pool.checkIn(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
var onReady = function() {
|
|
|
|
client.removeListener('error', onError);
|
|
|
|
client.connected = true;
|
|
|
|
callback(null, client);
|
|
|
|
client.on('drain', function() {
|
|
|
|
pool.checkIn(client);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
client.once('error', onError);
|
|
|
|
|
|
|
|
//TODO refactor
|
|
|
|
//i don't like reaching into the client's connection for attaching
|
|
|
|
//to specific events here
|
|
|
|
client.connection.once('readyForQuery', onReady);
|
|
|
|
|
|
|
|
client.connect();
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//destroys the world
|
2010-12-14 07:21:40 +08:00
|
|
|
//or optionally only a single pool
|
|
|
|
//mostly used for testing or
|
|
|
|
//a hard shutdown
|
|
|
|
var end = function(name) {
|
|
|
|
if(!name) {
|
|
|
|
for(var poolName in clientPools) {
|
|
|
|
end(poolName)
|
|
|
|
return
|
|
|
|
}
|
2010-12-11 07:32:34 +08:00
|
|
|
}
|
2010-12-14 07:21:40 +08:00
|
|
|
var pool = clientPools[name];
|
2010-12-15 11:21:28 +08:00
|
|
|
//log("destroying pool %s", name);
|
2010-12-14 07:21:40 +08:00
|
|
|
pool.waits.forEach(function(wait) {
|
|
|
|
wait(new Error("Client is being destroyed"))
|
|
|
|
})
|
|
|
|
pool.waits = [];
|
|
|
|
pool.items.forEach(function(item) {
|
|
|
|
var client = item.ref;
|
|
|
|
if(client.activeQuery) {
|
2010-12-15 11:21:28 +08:00
|
|
|
//log("client is still active, waiting for it to complete");
|
2010-12-14 07:21:40 +08:00
|
|
|
client.on('drain', client.end.bind(client))
|
|
|
|
} else {
|
|
|
|
client.end();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
//remove reference to pool lookup
|
|
|
|
clientPools[name] = null;
|
|
|
|
delete(clientPools[name])
|
2010-12-11 07:32:34 +08:00
|
|
|
}
|
2010-11-15 13:04:41 +08:00
|
|
|
|
|
|
|
|
2010-09-29 12:18:46 +08:00
|
|
|
module.exports = {
|
2010-11-15 14:10:21 +08:00
|
|
|
Client: Client,
|
2010-11-15 13:04:41 +08:00
|
|
|
Connection: require(__dirname + '/connection'),
|
2010-11-21 04:09:18 +08:00
|
|
|
connect: connect,
|
2010-12-11 07:32:34 +08:00
|
|
|
end: end,
|
|
|
|
defaults: defaults
|
2010-11-15 14:10:21 +08:00
|
|
|
}
|