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

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") var Query = require("./query")
, Utils = require("../../utils") , Utils = require("../../utils")
, pg = require("pg")
, without = function(arr, elem) { return arr.filter(function(e) { return e != elem }) } , without = function(arr, elem) { return arr.filter(function(e) { return e != elem }) }
module.exports = (function() { module.exports = (function() {
...@@ -7,6 +8,12 @@ module.exports = (function() { ...@@ -7,6 +8,12 @@ module.exports = (function() {
this.sequelize = sequelize this.sequelize = sequelize
this.client = null this.client = null
this.config = config || {} 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.disconnectTimeoutId = null
this.pendingQueries = 0 this.pendingQueries = 0
this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50) this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50)
...@@ -44,12 +51,9 @@ module.exports = (function() { ...@@ -44,12 +51,9 @@ module.exports = (function() {
this.isConnecting = true this.isConnecting = true
this.isConnected = false this.isConnected = false
var pg = require("pg") var uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config)
, uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config)
this.client = new pg.Client(uri)
this.client.connect(function(err, client) { var connectCallback = function(err, client) {
self.isConnecting = false self.isConnecting = false
if (!err && client) { if (!err && client) {
client.query("SET TIME ZONE 'UTC'") client.query("SET TIME ZONE 'UTC'")
...@@ -60,7 +64,17 @@ module.exports = (function() { ...@@ -60,7 +64,17 @@ module.exports = (function() {
} else { } else {
this.client = null 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() { ConnectorManager.prototype.disconnect = function() {
......
...@@ -19,7 +19,8 @@ module.exports = { ...@@ -19,7 +19,8 @@ module.exports = {
postgres: { postgres: {
database: 'sequelize_test', database: 'sequelize_test',
username: "postgres", 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!