Skip to content

Commit 27450d0

Browse files
committed
Throw on reconnect attempt
Clients are not reusable. This changes the client to raise errors whenever you try to reconnect a client that's already been used. They're cheap to create: just instantiate a new one (or use the pool) 😉. Closes brianc#1352
1 parent a83655a commit 27450d0

17 files changed

+219
-181
lines changed

lib/client.js

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var Client = function(config) {
3434
this._types = new TypeOverrides(c.types);
3535
this._ending = false;
3636
this._connecting = false;
37+
this._connected = false;
3738
this._connectionError = false;
3839

3940
this.connection = c.connection || new Connection({
@@ -54,6 +55,14 @@ util.inherits(Client, EventEmitter);
5455
Client.prototype.connect = function(callback) {
5556
var self = this;
5657
var con = this.connection;
58+
if (this._connecting || this._connected) {
59+
const err = new Error('Client has already been connected. You cannot reuse a client.')
60+
if (callback) {
61+
callback(err)
62+
return undefined
63+
}
64+
return Promise.reject(err)
65+
}
5766
this._connecting = true;
5867

5968
if(this.host && this.host.indexOf('/') === 0) {
@@ -135,6 +144,7 @@ Client.prototype.connect = function(callback) {
135144
//after the connection initially becomes ready for queries
136145
con.once('readyForQuery', function() {
137146
self._connecting = false;
147+
self._connected = true;
138148
self._attachListeners(con);
139149
con.removeListener('error', connectingErrorHandler);
140150
con.on('error', connectedErrorHandler)

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const poolFactory = (Client) => {
2121
for (var key in options) {
2222
config[key] = options[key];
2323
}
24-
Pool.call(this, config);
24+
return new Pool(config)
2525
};
2626

2727
util.inherits(BoundPool, Pool);

lib/native/client.js

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var Client = module.exports = function(config) {
3333

3434
this._queryQueue = [];
3535
this._connected = false;
36+
this._connecting = false;
3637

3738
//keep these on the object for legacy reasons
3839
//for the time being. TODO: deprecate all this jazz
@@ -74,6 +75,13 @@ Client.prototype.connect = function(cb) {
7475
})
7576
}
7677

78+
if (this._connecting) {
79+
process.nextTick(() => cb(new Error('Client has already been connected. You cannot reuse a client.')))
80+
return result
81+
}
82+
83+
this._connecting = true
84+
7785
this.connectionParameters.getLibpqConnectionString(function(err, conString) {
7886
if(err) return onError(err);
7987
self.native.connect(conString, function(err) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"buffer-writer": "1.0.1",
2222
"packet-reader": "0.3.1",
2323
"pg-connection-string": "0.1.3",
24-
"pg-pool": "1.*",
24+
"pg-pool": "2.*",
2525
"pg-types": "1.*",
2626
"pgpass": "1.x",
2727
"semver": "4.3.2"

test/integration/client/api-tests.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ suite.test("pool callback behavior", done => {
88
//test weird callback behavior with node-pool
99
const pool = new pg.Pool();
1010
pool.connect(function(err) {
11-
assert.isNull(err);
11+
assert(!err);
1212
arguments[1].emit("drain");
1313
arguments[2]();
1414
pool.end(done);
@@ -52,7 +52,7 @@ suite.test("executing nested queries", function(done) {
5252
const pool = new pg.Pool();
5353
pool.connect(
5454
assert.calls(function(err, client, release) {
55-
assert.isNull(err);
55+
assert(!err);
5656
client.query(
5757
"select now as now from NOW()",
5858
assert.calls(function(err, result) {
@@ -91,7 +91,7 @@ suite.test("query errors are handled and do not bubble if callback is provded",
9191
const pool = new pg.Pool();
9292
pool.connect(
9393
assert.calls(function(err, client, release) {
94-
assert.isNull(err);
94+
assert(!err);
9595
client.query(
9696
"SELECT OISDJF FROM LEIWLISEJLSE",
9797
assert.calls(function(err, result) {
@@ -109,7 +109,7 @@ suite.test("callback is fired once and only once", function(done) {
109109
const pool = new pg.Pool()
110110
pool.connect(
111111
assert.calls(function(err, client, release) {
112-
assert.isNull(err);
112+
assert(!err);
113113
client.query("CREATE TEMP TABLE boom(name varchar(10))");
114114
var callCount = 0;
115115
client.query(
@@ -136,14 +136,14 @@ suite.test("can provide callback and config object", function(done) {
136136
const pool = new pg.Pool()
137137
pool.connect(
138138
assert.calls(function(err, client, release) {
139-
assert.isNull(err);
139+
assert(!err);
140140
client.query(
141141
{
142142
name: "boom",
143143
text: "select NOW()"
144144
},
145145
assert.calls(function(err, result) {
146-
assert.isNull(err);
146+
assert(!err);
147147
assert.equal(result.rows[0].now.getYear(), new Date().getYear());
148148
release();
149149
pool.end(done)
@@ -157,15 +157,15 @@ suite.test("can provide callback and config and parameters", function(done) {
157157
const pool = new pg.Pool()
158158
pool.connect(
159159
assert.calls(function(err, client, release) {
160-
assert.isNull(err);
160+
assert(!err);
161161
var config = {
162162
text: "select $1::text as val"
163163
};
164164
client.query(
165165
config,
166166
["hi"],
167167
assert.calls(function(err, result) {
168-
assert.isNull(err);
168+
assert(!err);
169169
assert.equal(result.rows.length, 1);
170170
assert.equal(result.rows[0].val, "hi");
171171
release()
@@ -180,7 +180,7 @@ suite.test("null and undefined are both inserted as NULL", function(done) {
180180
const pool = new pg.Pool()
181181
pool.connect(
182182
assert.calls(function(err, client, release) {
183-
assert.isNull(err);
183+
assert(!err);
184184
client.query(
185185
"CREATE TEMP TABLE my_nulls(a varchar(1), b varchar(1), c integer, d integer, e date, f date)"
186186
);
@@ -191,7 +191,7 @@ suite.test("null and undefined are both inserted as NULL", function(done) {
191191
client.query(
192192
"SELECT * FROM my_nulls",
193193
assert.calls(function(err, result) {
194-
assert.isNull(err);
194+
assert(!err);
195195
assert.equal(result.rows.length, 1);
196196
assert.isNull(result.rows[0].a);
197197
assert.isNull(result.rows[0].b);

0 commit comments

Comments
 (0)