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