Skip to content
Browse files

Fix opcounter scope problem

  • Loading branch information...
1 parent a367e94 commit 7192243f741eb2699ca53912af9398c1c2a23130 @carlos8f committed Jul 22, 2012
Showing with 31 additions and 35 deletions.
  1. +30 −1 index.js
  2. +1 −1 lib/multi.js
  3. +0 −33 lib/node.js
View
31 index.js
@@ -263,7 +263,7 @@ commands.forEach(function(k) {
client[command].apply(client, args);
// Increment opcounter if necessary.
if (!self.isRead(command, args) && command != 'publish' && command != 'monitor') {
- self.master.incrOpcounter(function(err) {
+ self.incrOpcounter(function(err) {
if (err) {
// Will trigger failover!
return self.master.emit('err', err);
@@ -274,6 +274,35 @@ commands.forEach(function(k) {
};
});
+RedisHAClient.prototype.incrOpcounter = function(count, done) {
+ if (typeof count == 'function') {
+ done = count;
+ count = 1;
+ }
+ var master = this.master;
+ // For write operations, increment an op counter, to judge freshness of slaves.
+ if (!master.opcounterClient) {
+ master.opcounterClient = redis.createClient(master.port, master.host, this.options);
+ if (master.auth_pass) {
+ master.opcounterClient.auth(master.auth_pass);
+ }
+ master.clients.push(master.opcounterClient);
+ if (this.options.haredis_db_num) {
+ // Make redis connect to a special db (upon ready) for the opcounter.
+ master.opcounterClient.selected_db = this.options.haredis_db_num;
+ }
+ master.opcounterClient.on('error', function(err) {
+ master.emit('error', err);
+ });
+ }
+ if (this.opcounter++ % this.options.opcounterDiviser === 0) {
+ master.opcounterClient.INCRBY('haredis:opcounter', count, done);
+ }
+ else {
+ done();
+ }
+};
+
// Stash auth for connect and reconnect. Send immediately if already connected.
RedisHAClient.prototype.auth = RedisHAClient.prototype.AUTH = function () {
var args = Array.prototype.slice.call(arguments);
View
2 lib/multi.js
@@ -30,7 +30,7 @@ HAMulti.prototype.exec = function(callback) {
}
});
if (incr) {
- self.client.master.incrOpcounter(incr, function(err) {
+ self.client.incrOpcounter(incr, function(err) {
if (err) {
self.client.master.emit('error', err);
}
View
33 lib/node.js
@@ -144,39 +144,6 @@ Node.prototype.connect = function() {
});
};
-Node.prototype.incrOpcounter = function(count, done) {
- // Master-only operation
- if (this.role != 'master' || this.options.single_mode) {
- return;
- }
- if (typeof count == 'function') {
- done = count;
- count = 1;
- }
- var self = this;
- // For write operations, increment an op counter, to judge freshness of slaves.
- if (!this.opcounterClient) {
- this.opcounterClient = redis.createClient(this.port, this.host, this.options);
- if (this.auth_pass) {
- this.opcounterClient.auth(this.auth_pass);
- }
- this.clients.push(this.opcounterClient);
- if (this.options.haredis_db_num) {
- // Make redis connect to a special db (upon ready) for the opcounter.
- this.opcounterClient.selected_db = this.options.haredis_db_num;
- }
- this.opcounterClient.on('error', function(err) {
- self.emit('error', err);
- });
- }
- if (this.opcounter++ % this.options.opcounterDiviser === 0) {
- this.opcounterClient.INCRBY('haredis:opcounter', count, done);
- }
- else {
- done();
- }
-};
-
Node.prototype.quit = function(callback) {
var tasks = [];

0 comments on commit 7192243

Please sign in to comment.
Something went wrong with that request. Please try again.