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

Commit 59fa81f8 by Mick Hansen Committed by Jan Aagaard Meier

provide option (not exposed currently) to turn off mysql query queuing

1 parent 60fe7eaf
Showing with 46 additions and 16 deletions
...@@ -14,6 +14,8 @@ module.exports = (function() { ...@@ -14,6 +14,8 @@ module.exports = (function() {
this.activeQueue = [] this.activeQueue = []
this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50) this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50)
this.poolCfg = this.config.pool this.poolCfg = this.config.pool
this.pendingQueries = 0;
this.useQueue = false;
var self = this var self = this
...@@ -47,41 +49,69 @@ module.exports = (function() { ...@@ -47,41 +49,69 @@ module.exports = (function() {
}) })
} }
Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype) Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype);
var isConnecting = false var isConnecting = false;
ConnectorManager.prototype.query = function(sql, callee, options) { ConnectorManager.prototype.query = function(sql, callee, options) {
if(!this.isConnected && !this.pool) this.connect() if (!this.isConnected && !this.pool) this.connect();
if (this.useQueue) {
var queueItem = { var queueItem = {
query: new Query(this.client, this.sequelize, callee, options || {}), query: new Query(this.client, this.sequelize, callee, options || {}),
sql: sql sql: sql
};
enqueue.call(this, queueItem);
return queueItem.query;
} }
enqueue.call(this, queueItem) var self = this, query = new Query(this.client, this.sequelize, callee, options || {});
this.pendingQueries++;
query.done(function() {
self.pendingQueries--;
if (self.pool) self.pool.release(query.client);
else {
if (self.pendingQueries === 0) {
setTimeout(function() {
self.pendingQueries === 0 && self.disconnect.call(self);
}, 1);
}
}
});
if (!this.pool) query.run(sql);
else {
this.pool.acquire(function(err, client) {
if (err) return query.emit('error', err);
return queueItem.query query.client = client;
query.run(sql);
return;
});
} }
return query;
};
ConnectorManager.prototype.connect = function() { ConnectorManager.prototype.connect = function() {
var self = this var self = this;
// in case database is slow to connect, prevent orphaning the client // in case database is slow to connect, prevent orphaning the client
if (this.isConnecting || this.pool) { if (this.isConnecting || this.pool) {
return return;
} }
connect.call(self, function(err, client) { connect.call(self, function(err, client) {
self.client = client self.client = client;
return return;
}) });
return return;
} };
ConnectorManager.prototype.disconnect = function() { ConnectorManager.prototype.disconnect = function() {
if (this.client) if (this.client) disconnect.call(this, this.client);
disconnect.call(this, this.client) return;
return };
}
// private // private
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!