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

Commit ea4fa212 by Jan Aagaard Meier

Change pg connector manager to use promises correctly

1 parent f2d62129
......@@ -37,9 +37,9 @@ matrix:
fast_finish: true
include:
- node_js: "0.12"
- node_js: "0.11"
env: DB=mysql DIALECT=mysql
allow_failures:
- node_js: "0.12"
- node_js: "0.11"
env: DB=mysql DIALECT=mysql
\ No newline at end of file
......@@ -64,111 +64,109 @@ module.exports = (function() {
ConnectorManager.prototype.connect = function(callback) {
var self = this
var emitter = new Utils.Promise();
// in case database is slow to connect, prevent orphaning the client
// TODO: We really need some sort of queue/flush/drain mechanism
if (this.isConnecting && !this.pooling && this.client === null) {
emitter.emit('success', null)
return emitter
}
this.isConnecting = true
this.isConnected = false
return new Utils.Promise(function (resolve, reject) {
// in case database is slow to connect, prevent orphaning the client
// TODO: We really need some sort of queue/flush/drain mechanism
if (this.isConnecting && !this.pooling && this.client === null) {
return resolve()
}
var uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config)
, config = new this.ConnectionParameters(uri)
this.isConnecting = true
this.isConnected = false
// set pooling parameters if specified
if (this.pooling) {
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 uri = this.sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri(this.config)
, config = new this.ConnectionParameters(uri)
var connectCallback = function(err, client, done) {
self.isConnecting = false
if (!!err) {
// release the pool immediately, very important.
done && done(err)
self.client = null
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) {
// set pooling parameters if specified
if (this.pooling) {
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) {
var timezoneCallback = function() {
self.isConnected = true
self.client = client
emitter.emit('success', done)
}
self.isConnected = true
self.client = client
resolve(done)
}
if (self.config.keepDefaultTimezone) {
Utils.tick(timezoneCallback)
self.isConnecting = false
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 {
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) {
// acquire client from pool
this.pg.connect(config, connectCallback)
} else {
if (!!this.client) {
connectCallback(null, this.client)
if (this.pooling) {
// acquire client from pool
this.pg.connect(config, connectCallback)
} else {
//create one-off client
var responded = false
this.client = new this.pg.Client(config)
this.client.connect(function(err, client, done) {
responded = true
connectCallback(err, client || self.client, done)
})
// 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
})
if (!!this.client) {
connectCallback(null, this.client)
} else {
//create one-off client
var responded = false
this.client = new this.pg.Client(config)
this.client.connect(function(err, client, done) {
responded = true
connectCallback(err, client || self.client, done)
})
// 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
})
}
}
}
return emitter
}.bind(this))
}
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!