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

Commit d559bcfb by Sascha Depold

Merge pull request #211 from megshark/master

Add pooling support for postgres
2 parents 559069e4 d9781b31
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() {
......
......@@ -19,7 +19,8 @@ module.exports = {
postgres: {
database: 'sequelize_test',
username: "postgres",
port: 5432
port: 5432,
pool: { maxConnections: 5, maxIdleTime: 30}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!