Skip to content

Commit 796a44f

Browse files
authored
Remove internal pool (brianc#1049)
* Initial work on removing internal pool * Port backwards-compabible properties * Cleanup test execution & makefile cruft * Attempt to fix flakey error test
1 parent 1596a93 commit 796a44f

14 files changed

+83
-474
lines changed

Makefile

+5-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ all:
1313
npm install
1414

1515
help:
16-
@echo "make prepare-test-db [connectionString=postgres://<your connection string>]"
1716
@echo "make test-all [connectionString=postgres://<your connection string>]"
1817

1918
test: test-unit
@@ -32,11 +31,7 @@ test-unit:
3231

3332
test-connection:
3433
@echo "***Testing connection***"
35-
@node script/test-connection.js $(params)
36-
37-
test-connection-binary:
38-
@echo "***Testing binary connection***"
39-
@node script/test-connection.js $(params) binary
34+
@node script/create-test-tables.js $(params)
4035

4136
test-missing-native:
4237
@echo "***Testing optional native install***"
@@ -47,7 +42,7 @@ test-missing-native:
4742
node_modules/pg-native/index.js:
4843
@npm i pg-native
4944

50-
test-native: node_modules/pg-native/index.js
45+
test-native: node_modules/pg-native/index.js test-connection
5146
@echo "***Testing native bindings***"
5247
@find test/native -name "*-tests.js" | $(node-command)
5348
@find test/integration -name "*-tests.js" | $(node-command) native
@@ -56,13 +51,12 @@ test-integration: test-connection
5651
@echo "***Testing Pure Javascript***"
5752
@find test/integration -name "*-tests.js" | $(node-command)
5853

59-
test-binary: test-connection-binary
54+
test-binary: test-connection
6055
@echo "***Testing Pure Javascript (binary)***"
6156
@find test/integration -name "*-tests.js" | $(node-command) binary
6257

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

6761
jshint:
6862
@echo "***Starting jshint***"

README.md

+14-4
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

+26-10
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,31 @@ 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 = JSON.stringify(config || {});
50+
if (typeof config == 'string') {
51+
config = new ConnectionParameters(config);
52+
}
53+
54+
config = config || {};
55+
56+
//for backwards compatibility
57+
config.max = config.max || config.poolSize || defaults.poolSize;
58+
config.idleTimeoutMillis = config.idleTimeoutMillis || config.poolIdleTimeout || defaults.poolIdleTimeout;
59+
config.log = config.log || config.poolLog || defaults.poolLog;
60+
61+
this.pools[poolName] = this.pools[poolName] || new Pool(config, this.Client);
62+
var pool = this.pools[poolName];
4963
pool.connect(callback);
5064
if(!pool.listeners('error').length) {
5165
//propagate errors up to pg object
52-
pool.on('error', this.emit.bind(this, 'error'));
66+
pool.on('error', function(e) {
67+
this.emit('error', e, e.client);
68+
}.bind(this));
5369
}
5470
};
5571

lib/pool.js

-99
This file was deleted.

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@
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-
"pg-copy-streams": "0.3.0"
32+
"lodash": "4.13.1",
33+
"pg-copy-streams": "0.3.0",
34+
"promise-polyfill": "5.2.1"
3335
},
3436
"minNativeVersion": "1.7.0",
3537
"scripts": {

script/create-test-tables.js

+14-20
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,17 @@ var con = new pg.Client({
3838
database: args.database
3939
});
4040
con.connect();
41-
if(args.down) {
42-
console.log("Dropping table 'person'")
43-
var query = con.query("drop table if exists person");
44-
query.on('end', function() {
45-
console.log("Dropped!");
46-
con.end();
47-
});
48-
} else {
49-
console.log("Creating table 'person'");
50-
con.query("create table person(id serial, name varchar(10), age integer)").on('end', function(){
51-
console.log("Created!");
52-
console.log("Filling it with people");
53-
});;
54-
people.map(function(person) {
55-
return con.query("insert into person(name, age) values('"+person.name + "', '" + person.age + "')");
56-
}).pop().on('end', function(){
57-
console.log("Inserted 26 people");
58-
con.end();
59-
});
60-
}
41+
var query = con.query("drop table if exists person");
42+
query.on('end', function() {
43+
console.log("Dropped table 'person'")
44+
});
45+
con.query("create table person(id serial, name varchar(10), age integer)").on('end', function(){
46+
console.log("Created table person");
47+
console.log("Filling it with people");
48+
});
49+
people.map(function(person) {
50+
return con.query("insert into person(name, age) values('"+person.name + "', '" + person.age + "')");
51+
}).pop().on('end', function(){
52+
console.log("Inserted 26 people");
53+
con.end();
54+
});

script/test-connection.js

-24
This file was deleted.

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

-34
This file was deleted.

0 commit comments

Comments
 (0)