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

You need to sign in or sign up before continuing.
Commit f09f2de1 by Mick Hansen Committed by Jan Aagaard Meier

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

1 parent 4a7b6084
Showing with 50 additions and 20 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();
var queueItem = { if (this.useQueue) {
query: new Query(this.client, this.sequelize, callee, options || {}), var queueItem = {
sql: sql query: new Query(this.client, this.sequelize, callee, options || {}),
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);
}
}
});
return queueItem.query if (!this.pool) query.run(sql);
} else {
this.pool.acquire(function(err, client) {
if (err) return query.emit('error', err);
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!