Skip to content

Commit

Permalink
Fixed faradayio#36
Browse files Browse the repository at this point in the history
Currently, a pg pool was created for each request : this is bad, as each pool keeps a connection to the db so it can reuse it. This caused issue faradayio#36 for me. 
By following the example here : https://github.com/brianc/node-postgres/tree/v6.4.1 the pool must be created only once, and reused across requests (that's the purpose of a pool).
  • Loading branch information
philippeauriach authored Sep 13, 2017
1 parent 2c453cf commit aa4746d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ var pgMiddleware = function(_dbOptions){
dbOptions.poolSize = dbOptions.poolSize || 64;
}

var pool = new pg.Pool(dbOptions);

return function(req, res, next){
req.db = {};
req.db.query = function(sql, bindvars, callback){
Expand All @@ -63,8 +65,6 @@ var pgMiddleware = function(_dbOptions){
poolTimedOut = true;
callback(new Error('failed to get db connection'));
}, 5000);

var pool = new pg.Pool(dbOptions);

pool.connect(function(err, client, done){
if (poolTimedOut) {
Expand Down

0 comments on commit aa4746d

Please sign in to comment.