Skip to content

Commit

Permalink
Dont allow nested connection requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmanjunath committed Jan 9, 2017
1 parent 3d3bdae commit ee95760
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = require('./lib/connection.js');
module.exports.setConfig = require('./lib/connection.js').setConfig;
module.exports.query = require('./lib/query.js');
module.exports.model = require('./lib/model.js');
module.exports.import = require('./lib/model.js').import;
24 changes: 17 additions & 7 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ function _connectPool(pool, that, callback){
// connection functions for client pooling
Redshift.prototype.close = function(callback){
var that = this; //store original context of this because it will change inside callbacks
if(that.connectionType && that.connectionType === 'client' && that.client) that.client.end(callback);
else if(that.connectionType && that.connectionType === 'pool' && that.pool) that.pool.end();
if(that.connectionType && that.connectionType === 'client' && that.client){
that.client.end(callback);
}
else if(that.connectionType && that.connectionType === 'pool' && that.pool){
that.pool.end(callback);
delete that.pool;
}
};


Expand All @@ -63,12 +68,17 @@ Redshift.prototype.connect = function(callback){
that.client.connect(callback);
}
else if(that.connectionType === 'pool'){
var pool = new pg.Pool(that.config);
if(that.pool){
callback(new Error("A connection pool already exists, can't call connect"));
}
else{
var pool = new pg.Pool(that.config);

_connectPool(pool, that, function(err){
if(err) callback(err);
else callback();
});
_connectPool(pool, that, function(err){
if(err) callback(err);
else callback();
});
}
}
else callback(new Error("Couldn't connect to redshift. Invalid connection type"));
};
Expand Down
2 changes: 1 addition & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Redshift.prototype.query = function(query, options, callback){
count = count + 1; // count the attempts
if(count > 600){ // and after 1 min, give up
clearInterval(intId);
throw new Error("Taking too long to estable connection or unable to make query");
throw new Error("Unable to establish connection to redshift");
}
}
}, 100);
Expand Down

0 comments on commit ee95760

Please sign in to comment.