不要怂,就是干,撸起袖子干!

Commit 708976c2 by Meg Sharkey

added pooling support for postgres

1 parent 5d053be7
Showing with 20 additions and 6 deletions
var Query = require("./query")
, Utils = require("../../utils")
, pg = require("pg")
, without = function(arr, elem) { return arr.filter(function(e) { return e != elem }) }
module.exports = (function() {
......@@ -7,6 +8,12 @@ module.exports = (function() {
this.sequelize = sequelize
this.client = null
this.config = config || {}
this.pooling = (this.config.poolCfg != null && this.config.poolCfg.maxConnections > 0)
// set pooling parameters if specified
if (this.pooling) {
pg.defaults.poolSize = this.config.poolCfg.maxConnections
pg.defaults.poolIdleTimeout = this.config.poolCfg.maxIdleTime
}
this.disconnectTimeoutId = null
this.pendingQueries = 0
this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50)
......@@ -44,12 +51,9 @@ module.exports = (function() {
this.isConnecting = true
this.isConnected = false
var pg = require("pg")
, uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config)
this.client = new pg.Client(uri)
var uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config)
this.client.connect(function(err, client) {
var connectCallback = function(err, client) {
self.isConnecting = false
if (!err && client) {
client.query("SET TIME ZONE 'UTC'")
......@@ -60,7 +64,17 @@ module.exports = (function() {
} else {
this.client = null
}
})
}
if (this.pooling) {
// acquire client from pool
pg.connect(uri, connectCallback)
} else {
//create one-off client
this.client = new pg.Client(uri)
this.client.connect(connectCallback)
}
}
ConnectorManager.prototype.disconnect = function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!