Skip to content
This repository has been archived by the owner on May 10, 2019. It is now read-only.

Commit

Permalink
db.ping() must use the same application level query timing and reconn…
Browse files Browse the repository at this point in the history
…ection logic as other query types - issue #1608
  • Loading branch information
lloyd committed May 22, 2012
1 parent c243137 commit cf28389
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions lib/db/mysql_wrapper.js
Expand Up @@ -39,14 +39,15 @@ exports.createClient = function(options) {
logger.warn("database connection down: " + e.toString());
});
},
ping: function(cb) {
if (stalled) {
process.nextTick(function() {
cb("database is intentionally stalled");
});
} else {
this.realClient.ping(cb);
}
ping: function(client_cb) {
// ping queries are added to the front of the pending work queue. they are
// a priority, as they are used by load balancers that want to know the health
// of the system.
queryQueue.unshift({
ping: true,
cb: client_cb
});
this._runNextQuery();
},
_runNextQuery: function() {
var self = this;
Expand Down Expand Up @@ -93,25 +94,41 @@ exports.createClient = function(options) {
}
}, config.get('database.max_query_time_ms'));

this.realClient.query(work.query, work.args, function(err, r) {
// if we want to simulate a "stalled" mysql connection, we simply
// ignore the results from a query.
if (stalled) return;
if (work.ping) {
this.realClient.ping(function(err) {
if (stalled) {
return invokeCallback(work.cb, "database is intentionally stalled");
}

clearTimeout(slowQueryTimer);
slowQueryTimer = null;
consecutiveFailures = 0;
clearTimeout(slowQueryTimer);
slowQueryTimer = null;
consecutiveFailures = 0;

// report query time for all queries via statsd
var reqTime = new Date - work.startTime;
statsd.timing('query_time', reqTime);
invokeCallback(work.cb, err);

// report failed queries via statsd
if (err) statsd.increment('failed_query');
self._runNextQuery();
});
} else {
this.realClient.query(work.query, work.args, function(err, r) {
// if we want to simulate a "stalled" mysql connection, we simply
// ignore the results from a query.
if (stalled) return;

invokeCallback(work.cb, err, r);
self._runNextQuery();
});
clearTimeout(slowQueryTimer);
slowQueryTimer = null;
consecutiveFailures = 0;

// report query time for all queries via statsd
var reqTime = new Date - work.startTime;
statsd.timing('query_time', reqTime);

// report failed queries via statsd
if (err) statsd.increment('failed_query');

invokeCallback(work.cb, err, r);
self._runNextQuery();
});
}
},
query: function() {
var client_cb;
Expand Down

0 comments on commit cf28389

Please sign in to comment.