Skip to content

Commit 5b1b13d

Browse files
committed
Initial work on removing internal pool
1 parent d1c5fc6 commit 5b1b13d

11 files changed

+43
-409
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ test-binary: test-connection-binary
6060
@echo "***Testing Pure Javascript (binary)***"
6161
@find test/integration -name "*-tests.js" | $(node-command) binary
6262

63+
test-pool:
64+
@find test/integration/connection-pool -name "*.js" | $(node-command) binary
65+
6366
prepare-test-db:
6467
@echo "***Preparing the database for tests***"
6568
@find script/create-test-tables.js | $(node-command)

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ $ npm install pg
1919
Generally you will access the PostgreSQL server through a pool of clients. A client takes a non-trivial amount of time to establish a new connection. A client also consumes a non-trivial amount of resources on the PostgreSQL server - not something you want to do on every http request. Good news: node-postgres ships with built in client pooling.
2020

2121
```javascript
22-
var pg = require('pg');
23-
var conString = "postgres://username:password@localhost/database";
22+
var Pool = require('pg').Pool;
23+
24+
var config = {
25+
user: 'foo', //env var: PGUSER
26+
database: 'my_db', //env var: PGDATABASE
27+
password: 'secret', //env var: PGPASSWORD
28+
port: 5432 //env var: PGPORT
29+
};
30+
31+
var pool = new Pool(config);
2432

2533
//this initializes a connection pool
2634
//it will keep idle connections open for a (configurable) 30 seconds
2735
//and set a limit of 10 (also configurable)
28-
pg.connect(conString, function(err, client, done) {
36+
pool.connect(function(err, client, done) {
2937
if(err) {
3038
return console.error('error fetching client from pool', err);
3139
}
@@ -42,6 +50,8 @@ pg.connect(conString, function(err, client, done) {
4250
});
4351
```
4452

53+
node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling and only provides a very thin layer on top. It's highly recommend you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git)
54+
4555
[Check this out for the get up and running quickly example](https://github.com/brianc/node-postgres/wiki/Example)
4656

4757
### Client instance
@@ -85,7 +95,7 @@ node-postgres contains a pure JavaScript protocol implementation which is quite
8595

8696
To use the native bindings, first install [pg-native](https://github.com/brianc/node-pg-native.git). Once pg-native is installed, simply replace `require('pg')` with `require('pg').native`.
8797

88-
node-postgres abstracts over the pg-native module to provide exactly the same interface as the pure JavaScript version. __No other code changes are required__. If you find yourself having to change code other than the require statement when switching from `require('pg')` to `require('pg').native` please report an issue.
98+
node-postgres abstracts over the pg-native module to provide exactly the same interface as the pure JavaScript version. Care has been taken to keep the number of api differences between the two modules to a minimum; however, it is recommend you use either the pure JavaScript or native bindings in both development and production and don't mix & match them in the same process - it can get confusing!
8999

90100
## Features
91101

lib/index.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ var EventEmitter = require('events').EventEmitter;
22
var util = require('util');
33
var Client = require('./client');
44
var defaults = require('./defaults');
5-
var pool = require('./pool');
65
var Connection = require('./connection');
6+
var ConnectionParameters = require('./connection-parameters');
7+
var Pool = require('pg-pool');
78

89
var PG = function(clientConstructor) {
910
EventEmitter.call(this);
1011
this.defaults = defaults;
1112
this.Client = clientConstructor;
1213
this.Query = this.Client.Query;
13-
this.pools = pool(clientConstructor);
14+
this.Pool = Pool;
15+
this.pools = [];
1416
this.Connection = Connection;
1517
this.types = require('pg-types');
1618
};
@@ -19,16 +21,16 @@ util.inherits(PG, EventEmitter);
1921

2022
PG.prototype.end = function() {
2123
var self = this;
22-
var keys = Object.keys(self.pools.all);
24+
var keys = Object.keys(this.pools);
2325
var count = keys.length;
2426
if(count === 0) {
2527
self.emit('end');
2628
} else {
2729
keys.forEach(function(key) {
28-
var pool = self.pools.all[key];
29-
delete self.pools.all[key];
30-
pool.drain(function() {
31-
pool.destroyAllNow(function() {
30+
var pool = self.pools[key];
31+
delete self.pools[key];
32+
pool.pool.drain(function() {
33+
pool.pool.destroyAllNow(function() {
3234
count--;
3335
if(count === 0) {
3436
self.emit('end');
@@ -39,17 +41,24 @@ PG.prototype.end = function() {
3941
}
4042
};
4143

42-
4344
PG.prototype.connect = function(config, callback) {
4445
if(typeof config == "function") {
4546
callback = config;
4647
config = null;
4748
}
48-
var pool = this.pools.getOrCreate(config);
49+
var poolName = config || '__DEFAULT';
50+
if (typeof config == 'string') {
51+
config = new ConnectionParameters(config);
52+
}
53+
54+
this.pools[poolName] = this.pools[poolName] || new Pool(config, this.Client);
55+
var pool = this.pools[poolName];
4956
pool.connect(callback);
5057
if(!pool.listeners('error').length) {
5158
//propagate errors up to pg object
52-
pool.on('error', this.emit.bind(this, 'error'));
59+
pool.on('error', function(e) {
60+
this.emit('error', e, e.client);
61+
}.bind(this));
5362
}
5463
};
5564

lib/pool.js

Lines changed: 0 additions & 99 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919
"main": "./lib",
2020
"dependencies": {
2121
"buffer-writer": "1.0.1",
22-
"generic-pool": "2.4.2",
2322
"packet-reader": "0.2.0",
2423
"pg-connection-string": "0.1.3",
24+
"pg-pool": "1.*",
2525
"pg-types": "1.*",
2626
"pgpass": "0.0.6",
2727
"semver": "4.3.2"
2828
},
2929
"devDependencies": {
3030
"async": "0.9.0",
3131
"jshint": "2.5.2",
32+
"lodash": "4.13.1",
3233
"pg-copy-streams": "0.3.0"
3334
},
3435
"minNativeVersion": "1.7.0",

test/integration/client/query-callback-error-tests.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/integration/connection-pool/idle-timeout-tests.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
var helper = require(__dirname + '/test-helper');
2+
var _ = require('lodash')
23

3-
helper.pg.defaults.poolIdleTimeout = 200;
4+
const config = _.extend({ }, helper.config, { idleTimeoutMillis: 50 })
45

56
test('idle timeout', function() {
6-
helper.pg.connect(helper.config, assert.calls(function(err, client, done) {
7+
helper.pg.connect(config, assert.calls(function(err, client, done) {
78
assert.isNull(err);
89
client.query('SELECT NOW()');
9-
//just let this one time out
10+
//just let this one time out
1011
//test will hang if pool doesn't timeout
1112
done();
1213
}));

test/integration/connection-pool/optional-config-tests.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ helper.pg.connect(assert.calls(function(err, client, done) {
1515
setTimeout(function() {
1616
helper.pg.end();
1717
done();
18-
1918
}, 10);
2019
});
2120
}));

test/integration/domain-tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var async = require('async')
44
var testWithoutDomain = function(cb) {
55
test('no domain', function() {
66
assert(!process.domain)
7+
console.log(helper.pg.Client)
78
helper.pg.connect(helper.config, assert.success(function(client, done) {
89
assert(!process.domain)
910
done()

0 commit comments

Comments
 (0)