|
1 | 1 | var EventEmitter = require('events').EventEmitter; |
2 | 2 | var Client = require(__dirname+'/client'); |
3 | 3 | var defaults = require(__dirname + '/defaults'); |
4 | | -var pool = require(__dirname + "/client-pool").init(Client); |
| 4 | +var genericPool = require('generic-pool'); |
| 5 | + |
| 6 | +//cache of existing client pools |
| 7 | +var pools = {}; |
| 8 | + |
| 9 | +//returns connect function using supplied client constructor |
| 10 | +var makeConnectFunction = function(ClientConstructor) { |
| 11 | + return function(config, callback) { |
| 12 | + var c = config; |
| 13 | + var cb = callback; |
| 14 | + //allow for no config to be passed |
| 15 | + if(typeof c === 'function') { |
| 16 | + cb = c; |
| 17 | + c = defaults; |
| 18 | + } |
| 19 | + //get unique pool name if using a config object instead of config string |
| 20 | + var poolName = typeof(c) === 'string' ? c : c.user+c.host+c.port+c.database; |
| 21 | + var pool = pools[poolName]; |
| 22 | + if(pool) return pool.acquire(cb); |
| 23 | + var pool = pools[poolName] = genericPool.Pool({ |
| 24 | + name: poolName, |
| 25 | + create: function(callback) { |
| 26 | + var client = new Client(c); |
| 27 | + client.connect(); |
| 28 | + var connectError = function(err) { |
| 29 | + client.removeListener('connect', connectSuccess); |
| 30 | + callback(err, null); |
| 31 | + }; |
| 32 | + var connectSuccess = function() { |
| 33 | + client.removeListener('error', connectError); |
| 34 | + callback(null, client); |
| 35 | + }; |
| 36 | + client.once('connect', connectSuccess); |
| 37 | + client.once('error', connectError); |
| 38 | + client.on('drain', function() { |
| 39 | + pool.release(client); |
| 40 | + }); |
| 41 | + }, |
| 42 | + destroy: function(client) { |
| 43 | + client.end(); |
| 44 | + }, |
| 45 | + max: defaults.poolSize |
| 46 | + }); |
| 47 | + return pool.acquire(cb); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +var end = function() { |
| 52 | + Object.keys(pools).forEach(function(name) { |
| 53 | + var pool = pools[name]; |
| 54 | + pool.drain(function() { |
| 55 | + pool.destroyAllNow(); |
| 56 | + }); |
| 57 | + }) |
| 58 | +}; |
5 | 59 |
|
6 | 60 | module.exports = { |
7 | 61 | Client: Client, |
8 | 62 | Connection: require(__dirname + '/connection'), |
9 | | - connect: pool.connect, |
10 | | - end: pool.end, |
| 63 | + connect: makeConnectFunction(Client), |
| 64 | + end: end, |
11 | 65 | defaults: defaults |
12 | 66 | } |
13 | 67 |
|
| 68 | +var nativeExport = null; |
14 | 69 | //lazy require native module...the c++ may not have been compiled |
15 | 70 | module.exports.__defineGetter__("native", function() { |
16 | | - return require(__dirname + '/native'); |
| 71 | + if(nativeExport === null) { |
| 72 | + var NativeClient = require(__dirname + '/native'); |
| 73 | + nativeExport = { |
| 74 | + Client: NativeClient, |
| 75 | + connect: makeConnectFunction(NativeClient), |
| 76 | + end: end, |
| 77 | + defaults: defaults |
| 78 | + } |
| 79 | + } |
| 80 | + return nativeExport; |
17 | 81 | }) |
0 commit comments