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

Commit bd670508 by Daniel Durante

Non pooling connections will no longer leak.

1 parent ec2d5dd3
Showing with 23 additions and 7 deletions
...@@ -46,11 +46,15 @@ module.exports = (function() { ...@@ -46,11 +46,15 @@ module.exports = (function() {
ConnectorManager.prototype.query = function(sql, callee, options) { ConnectorManager.prototype.query = function(sql, callee, options) {
var self = this var self = this
// we really want pendingQueries to increment as fast as possible...
self.pendingQueries++ self.pendingQueries++
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
self.connect() self.connect()
.on('error', function(err) { .on('error', function(err) {
// zero-out the previous increment
self.pendingQueries--
self.endQuery.call(self)
emitter.emit('error', err) emitter.emit('error', err)
}) })
.on('success', function(done) { .on('success', function(done) {
...@@ -58,14 +62,22 @@ module.exports = (function() { ...@@ -58,14 +62,22 @@ module.exports = (function() {
done = done || null done = done || null
query.run(sql, done) query.run(sql, done)
.success(function(results) { emitter.emit('success', results); self.endQuery.call(self) }) .success(function(results) {
.error(function(err) { emitter.emit('error', err); self.endQuery.call(self) }) self.pendingQueries--
emitter.emit('success', results)
self.endQuery.call(self)
})
.error(function(err) {
self.pendingQueries--
emitter.emit('error', err)
self.endQuery.call(self)
})
.on('sql', function(sql) { emitter.emit('sql', sql) }) .on('sql', function(sql) { emitter.emit('sql', sql) })
}) })
}).run().complete(function() { self.pendingQueries-- }) }).run()
} }
ConnectorManager.prototype.connect = function(callback) { ConnectorManager.prototype.connect = function() {
var self = this var self = this
var emitter = new (require('events').EventEmitter)() var emitter = new (require('events').EventEmitter)()
...@@ -119,9 +131,13 @@ module.exports = (function() { ...@@ -119,9 +131,13 @@ module.exports = (function() {
this.poolIdentifier = this.pg.pools.getOrCreate(this.sequelize.config) this.poolIdentifier = this.pg.pools.getOrCreate(this.sequelize.config)
this.poolIdentifier.connect(connectCallback) this.poolIdentifier.connect(connectCallback)
} else { } else {
//create one-off client if (this.client !== null) {
this.client = new this.pg.Client(uri) connectCallback(null, this.client)
this.client.connect(connectCallback) } else {
//create one-off client
this.client = new this.pg.Client(uri)
this.client.connect(connectCallback)
}
} }
return emitter return emitter
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!