Skip to content

Commit e4daade

Browse files
author
Raymond Feng
committed
Merge branch 'AccelerationNet-master'
2 parents 2ab4e93 + 501037a commit e4daade

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

lib/postgresql.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,38 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
5050
}
5151
};
5252

53+
// there is a bug in the generic pool where it will not recreate
54+
// destroyed workers (even if there is waiting work to do) unless
55+
// there is a min specified. Make sure we keep some connections
56+
// SEE: https://github.com/coopernurse/node-pool/pull/186
57+
// SEE: https://github.com/brianc/node-pg-pool/issues/48
58+
// SEE: https://github.com/strongloop/loopback-connector-postgresql/issues/231
59+
function _ensureMinimum() {
60+
var i, diff, waiting;
61+
if (this._draining) return;
62+
waiting = this._waitingClients.size();
63+
if (this._factory.min > 0) { // we have positive specified minimum
64+
diff = this._factory.min - this._count;
65+
} else if (waiting > 0) { // we have no minimum, but we do have work to do
66+
diff = Math.min(waiting, this._factory.max - this._count);
67+
}
68+
for (i = 0; i < diff; i++) {
69+
this._createResource();
70+
}
71+
};
72+
function makePool(options) {
73+
var pg = new postgresql.Pool(options);
74+
// Monkey patch to ensure we always finish our work
75+
// - There is a bug where callbacks go uncalled if min is not set
76+
// - We might still not want a connection to *always* exist
77+
// - but we do want to create up to max connections if we have work
78+
// - still waiting
79+
// This should be safe till the version of pg-pool is upgraded
80+
// SEE: https://github.com/coopernurse/node-pool/pull/186
81+
pg.pool._ensureMinimum = _ensureMinimum;
82+
return pg;
83+
}
84+
5385
/**
5486
* PostgreSQL connector constructor
5587
*
@@ -78,7 +110,8 @@ function PostgreSQL(postgresql, settings) {
78110
this.clientConfig.connectionString = settings.url;
79111
}
80112
this.clientConfig.Promise = Promise;
81-
this.pg = new postgresql.Pool(this.clientConfig);
113+
this.pg = makePool(this.clientConfig);
114+
82115
this.settings = settings;
83116
if (settings.debug) {
84117
debug('Settings %j', settings);

test/postgresql.initialization.test.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77
require('./init');
8-
8+
var Promise = require('bluebird');
99
var connector = require('..');
1010
var DataSource = require('loopback-datasource-juggler').DataSource;
1111
var should = require('should');
@@ -52,3 +52,36 @@ describe('initialization', function() {
5252
clientConfig.connectionString.should.equal(urlOnly.url);
5353
});
5454
});
55+
56+
describe('postgresql connector errors', function() {
57+
it('Should complete these 4 queries without dying', function(done) {
58+
var dataSource = getDataSource();
59+
var db = dataSource.connector;
60+
var pool = db.pg.pool;
61+
pool._factory.max = 5;
62+
pool._factory.min = null;
63+
var errors = 0;
64+
var shouldGet = 0;
65+
function runErrorQuery() {
66+
shouldGet++;
67+
return new Promise(function(resolve, reject) {
68+
db.executeSQL("SELECT 'asd'+1 ", [], {}, function(err, res) {
69+
if (err) {
70+
errors++;
71+
resolve(err);
72+
} else {
73+
reject(res); // this should always error
74+
}
75+
});
76+
});
77+
};
78+
var ps = [];
79+
for (var i = 0; i < 12; i++) {
80+
ps.push(runErrorQuery());
81+
}
82+
Promise.all(ps).then(function() {
83+
shouldGet.should.equal(errors);
84+
done();
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)