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

Commit ea4fa212 by Jan Aagaard Meier

Change pg connector manager to use promises correctly

1 parent f2d62129
...@@ -37,9 +37,9 @@ matrix: ...@@ -37,9 +37,9 @@ matrix:
fast_finish: true fast_finish: true
include: include:
- node_js: "0.12" - node_js: "0.11"
env: DB=mysql DIALECT=mysql env: DB=mysql DIALECT=mysql
allow_failures: allow_failures:
- node_js: "0.12" - node_js: "0.11"
env: DB=mysql DIALECT=mysql env: DB=mysql DIALECT=mysql
\ No newline at end of file
...@@ -64,111 +64,109 @@ module.exports = (function() { ...@@ -64,111 +64,109 @@ module.exports = (function() {
ConnectorManager.prototype.connect = function(callback) { ConnectorManager.prototype.connect = function(callback) {
var self = this var self = this
var emitter = new Utils.Promise();
// in case database is slow to connect, prevent orphaning the client return new Utils.Promise(function (resolve, reject) {
// TODO: We really need some sort of queue/flush/drain mechanism // in case database is slow to connect, prevent orphaning the client
if (this.isConnecting && !this.pooling && this.client === null) { // TODO: We really need some sort of queue/flush/drain mechanism
emitter.emit('success', null) if (this.isConnecting && !this.pooling && this.client === null) {
return emitter return resolve()
} }
this.isConnecting = true
this.isConnected = false
var uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config) this.isConnecting = true
, config = new this.ConnectionParameters(uri) this.isConnected = false
// set pooling parameters if specified var uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config)
if (this.pooling) { , config = new this.ConnectionParameters(uri)
config.poolSize = this.config.pool.maxConnections || 10
config.poolIdleTimeout = this.config.pool.maxIdleTime || 30000
config.reapIntervalMillis = this.config.pool.reapInterval || 1000
config.uuid = this.config.uuid
}
var connectCallback = function(err, client, done) { // set pooling parameters if specified
self.isConnecting = false if (this.pooling) {
config.poolSize = this.config.pool.maxConnections || 10
if (!!err) { config.poolIdleTimeout = this.config.pool.maxIdleTime || 30000
// release the pool immediately, very important. config.reapIntervalMillis = this.config.pool.reapInterval || 1000
done && done(err) config.uuid = this.config.uuid
self.client = null }
var connectCallback = function(err, client, done) {
if (err.code) {
switch(err.code) {
case 'ECONNREFUSED':
emitter.emit('error', new Error("Failed to authenticate for PostgresSQL. Please double check your settings."))
break
case 'ENOTFOUND':
case 'EHOSTUNREACH':
case 'EINVAL':
emitter.emit('error', new Error("Failed to find PostgresSQL server. Please double check your settings."))
break
default:
emitter.emit('error', err)
break
}
} else {
emitter.emit('error', new Error(err.message))
}
} else if (client) {
var timezoneCallback = function() { var timezoneCallback = function() {
self.isConnected = true self.isConnected = true
self.client = client self.client = client
emitter.emit('success', done) resolve(done)
} }
if (self.config.keepDefaultTimezone) { self.isConnecting = false
Utils.tick(timezoneCallback)
if (!!err) {
// release the pool immediately, very important.
done && done(err)
self.client = null
if (err.code) {
switch(err.code) {
case 'ECONNREFUSED':
reject(new Error("Failed to authenticate for PostgresSQL. Please double check your settings."))
break
case 'ENOTFOUND':
case 'EHOSTUNREACH':
case 'EINVAL':
reject(new Error("Failed to find PostgresSQL server. Please double check your settings."))
break
default:
reject(err)
break
}
} else {
reject(new Error(err.message))
}
} else if (client) {
if (self.config.keepDefaultTimezone) {
timezoneCallback()
} else {
self.setTimezone(client, 'UTC', timezoneCallback)
}
} else if (self.config.native) {
if (self.config.keepDefaultTimezone) {
timezoneCallback()
} else {
self.setTimezone(self.client, 'UTC', timezoneCallback)
}
} else { } else {
self.setTimezone(client, 'UTC', timezoneCallback) done && done()
self.client = null
resolve()
} }
} else if (self.config.native) {
self.setTimezone(self.client, 'UTC', function() {
self.isConnected = true
emitter.emit('success', done)
})
} else {
done && done()
self.client = null
emitter.emit('success')
} }
}
if (this.pooling) { if (this.pooling) {
// acquire client from pool // acquire client from pool
this.pg.connect(config, connectCallback) this.pg.connect(config, connectCallback)
} else {
if (!!this.client) {
connectCallback(null, this.client)
} else { } else {
//create one-off client if (!!this.client) {
connectCallback(null, this.client)
var responded = false } else {
//create one-off client
this.client = new this.pg.Client(config)
this.client.connect(function(err, client, done) { var responded = false
responded = true
connectCallback(err, client || self.client, done) this.client = new this.pg.Client(config)
}) this.client.connect(function(err, client, done) {
responded = true
// If we didn't ever hear from the client.connect() callback the connection timeout, node-postgres does not treat this as an error since no active query was ever emitted connectCallback(err, client || self.client, done)
this.client.on('end', function () { })
if (!responded) {
connectCallback(new Error('Connection timed out')) // If we didn't ever hear from the client.connect() callback the connection timeout, node-postgres does not treat this as an error since no active query was ever emitted
} this.client.on('end', function () {
}) if (!responded) {
connectCallback(new Error('Connection timed out'))
// Closes a client correctly even if we have backed up queries }
// https://github.com/brianc/node-postgres/pull/346 })
this.client.on('drain', function() {
self.clientDrained = true // Closes a client correctly even if we have backed up queries
}) // https://github.com/brianc/node-postgres/pull/346
this.client.on('drain', function() {
self.clientDrained = true
})
}
} }
} }.bind(this))
return emitter
} }
ConnectorManager.prototype.setTimezone = function(client, timezone, callback) { ConnectorManager.prototype.setTimezone = function(client, timezone, callback) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!