Skip to content
Browse files

Managing all fail overs correctly

  • Loading branch information...
1 parent b45781d commit 7ac73b13b29eb674d16b63837a3b94de5bc84630 @sreeix committed Feb 27, 2012
Showing with 51 additions and 21 deletions.
  1. +1 −1 config/config.json
  2. +39 −16 lib/redis_proxy.js
  3. +10 −3 lib/server.js
  4. +1 −1 server.js
View
2 config/config.json
@@ -9,5 +9,5 @@
}
]
,"listen_port": 9999
- ,"check_period": 5
+ ,"check_period": 5000
}
View
55 lib/redis_proxy.js
@@ -23,23 +23,49 @@ var RedisProxy = module.exports = function(o){
}
this.allServers = _.map(o.servers, function(server){
- var s =new Server(server);
- s.on('up', function(){
+ return new Server(server).on('up', function() {
console.log("We have a server that went up");
- console.log(this);
- });
- s.on('down', function(){
- console.log("We have a server that went DOWN");
- console.log(this);
+ if(_.isNull(self._active) || _.isUndefined(self._active)){
+ self._active = this;
+ console.log("setting up the active "+ self._active.options.port);
+ self.readyup(this);
+ }
+ }).on('down', function(){
+ if(_.isEqual(self._active.options, this.options)){
+ console.log("Main server down PANIC");
+ console.log("finding next active server.");
+ self.nextActive();
+ }
});
});
};
+
RedisProxy.prototype.readyup = function(server){
- console.log("Creating the pool for active server"+ server.options.host);
+ console.log("Creating the pool for active server"+ server.options.port);
this.connections = new Pool({create: function(){
- return redis.createClient(server.options.port, server.options.host);
+ try {
+ var client = redis.createClient(server.options.port, server.options.host, {max_attempts: 1});
+ client.on('error', redis.print);
+ } catch(err) {
+ console.log(err);
+ console.log(">>>>>>>>>>>>>>>");
+ }
}});
};
+
+RedisProxy.prototype.nextActive = function() {
+ this._active = _.find(this.allServers, function(server) {
+ return server.isUp()
+ });
+
+ if(this._active){
+ this.readyup(this._active);
+ console.log("Setting up as active "+ this._active.options.port);
+ } else {
+ throw new Error("Expected to have atleast one redis to proxy");
+ }
+}
+
RedisProxy.prototype.active = function(command, callback) {
return this._active;
}
@@ -48,16 +74,13 @@ RedisProxy.prototype.sendCommand = function(command, callback) {
return this.connections.take().sendRaw(command, callback);
};
-RedisProxy.prototype.findByClient = function(client){
- return _.find(this.allServers, function(server){
- return (server.client.port == client.port && server.client.host === client.host);
- });
-};
-
RedisProxy.prototype.watch = function(){
var self = this;
setInterval(function(){
- _.each(this.allServers, function(server){
+ console.log("Timeout.....");
+ _.each(self.allServers, function(server){
+ console.log("Pinging ");
+ console.log(server.options.port);
server.ping();
});
}, this.options.check_period);
View
13 lib/server.js
@@ -22,6 +22,8 @@ Server.prototype.setupClient = function(serverInfo){
self.up();
});
this.client.on('error', function(data){
+ console.log("EEEEEEEERRRRRRRRRRRRROOOOOOOOOORRRRRRRR");
+ console.log(data);
self.down();
});
@@ -30,6 +32,7 @@ Server.prototype.setupClient = function(serverInfo){
self.down();
});
} catch(err) {
+ console.log("-------------");
console.log(err);
self.down();
// Its ok... we will bring this guy up some point in time
@@ -49,13 +52,17 @@ Server.prototype.ping = function(){
if(!this.client){
this.setupClient(this.options);
} else {
- this.client.ping(function(){
- console.log("xxxxxxxxxx");
+ this.client.ping(function(err, data){
+ if(err){
+ return self.down();
+ }
self.up();
});
}
};
-
+Server.prototype.isUp = function(){
+ return this.status === 'up';
+}
Server.prototype.down = function(){
this.status = 'down';
this.emit('down');
View
2 server.js
@@ -26,6 +26,6 @@ var server = net.createServer(function (socket) {
});
});
});
-
+redis_proxy.watch();
server.listen(config.listen_port, "127.0.0.1");
console.log("server is listening on 127.0.0.1:"+ config.listen_port);

0 comments on commit 7ac73b1

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