Skip to content

Commit 4cb97a2

Browse files
committed
switch to node-pool from custom client pool
1 parent 390f4e8 commit 4cb97a2

File tree

11 files changed

+85
-25
lines changed

11 files changed

+85
-25
lines changed

lib/index.js

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,81 @@
11
var EventEmitter = require('events').EventEmitter;
22
var Client = require(__dirname+'/client');
33
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+
};
559

660
module.exports = {
761
Client: Client,
862
Connection: require(__dirname + '/connection'),
9-
connect: pool.connect,
10-
end: pool.end,
63+
connect: makeConnectFunction(Client),
64+
end: end,
1165
defaults: defaults
1266
}
1367

68+
var nativeExport = null;
1469
//lazy require native module...the c++ may not have been compiled
1570
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;
1781
})

lib/native.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,4 @@ p.handleReadyForQuery = function() {
178178

179179
var pool = require(__dirname + '/client-pool').init(ctor);
180180

181-
module.exports = {
182-
Client: ctor,
183-
connect: pool.connect,
184-
end: pool.end,
185-
defaults: require(__dirname + '/defaults')
186-
};
181+
module.exports = ctor;

test/integration/client/api-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var helper = require(__dirname + '/../test-helper');
22
var pg = require(__dirname + '/../../../lib');
33

44
if(helper.args.native) {
5-
pg = require(__dirname + '/../../../lib/native')
5+
pg = require(__dirname + '/../../../lib').native;
66
}
77

88
if(helper.args.libpq) {

test/integration/client/transaction-tests.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var helper = require(__dirname + '/test-helper');
22

3+
var sink = new helper.Sink(2, function() {
4+
helper.pg.end();
5+
});
6+
37
test('a single connection transaction', function() {
48
var connectionString = helper.connectionString();
5-
var sink = new helper.Sink(1, function() {
6-
helper.pg.end();
7-
});
89

910
helper.pg.connect(connectionString, assert.calls(function(err, client) {
1011
assert.isNull(err);
@@ -43,7 +44,6 @@ test('a single connection transaction', function() {
4344
sink.add();
4445
}))
4546
})
46-
4747
}))
4848
})
4949

@@ -68,7 +68,8 @@ test('gh#36', function() {
6868
if(err) throw err;
6969
assert.equal(result.rows.length, 1);
7070
}))
71-
client.query("COMMIT")
72-
client.on('drain', client.end.bind(client))
71+
client.query("COMMIT", function() {
72+
sink.add();
73+
})
7374
})
7475
})

test/integration/client/type-coercion-tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var valueCount = 0;
8383
types.forEach(function(type) {
8484
valueCount += type.values.length;
8585
})
86-
sink = new helper.Sink(types.length, function() {
86+
sink = new helper.Sink(types.length + 1, function() {
8787
helper.pg.end();
8888
})
8989

@@ -135,6 +135,6 @@ helper.pg.connect(helper.connectionString(), assert.calls(function(err, client)
135135
client.query('select 7 <> $1 as res;',[null], function(err, res) {
136136
assert.isNull(err);
137137
assert.strictEqual(res.rows[0].res, null);
138-
client.end();
138+
sink.add();
139139
})
140140
}))

test/integration/test-helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var helper = require(__dirname + '/../test-helper');
22

33
//TODO would this be better served set at ../test-helper?
44
if(helper.args.native) {
5-
Client = require(__dirname + '/../../lib/native').Client;
5+
Client = require(__dirname + '/../../lib/native');
66
helper.pg = helper.pg.native;
77
}
88
//export parent helper stuffs

test/native/callback-api-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var helper = require(__dirname + "/../test-helper");
2-
var Client = require(__dirname + "/../../lib/native").Client;
2+
var Client = require(__dirname + "/../../lib/native");
33
var conString = helper.connectionString();
44

55
test('fires callback with results', function() {

test/native/connection-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var helper = require(__dirname + "/../test-helper");
2-
var Client = require(__dirname + "/../../lib/native").Client;
2+
var Client = require(__dirname + "/../../lib/native");
33

44
test('connecting with wrong parameters', function() {
55
var con = new Client("user=asldfkj hostaddr=127.0.0.1 port=5432 dbname=asldkfj");

test/native/error-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var helper = require(__dirname + "/../test-helper");
2-
var Client = require(__dirname + "/../../lib/native").Client;
2+
var Client = require(__dirname + "/../../lib/native");
33
var conString = helper.connectionString();
44

55
test('query with non-text as first parameter throws error', function() {

test/native/evented-api-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var helper = require(__dirname + "/../test-helper");
2-
var Client = require(__dirname + "/../../lib/native").Client;
2+
var Client = require(__dirname + "/../../lib/native");
33
var conString = helper.connectionString();
44

55
var setupClient = function() {

0 commit comments

Comments
 (0)