From 406d65a3ef3212b1399e3240582f1a677b9851f8 Mon Sep 17 00:00:00 2001 From: V Sreekanth Date: Wed, 10 Apr 2013 18:25:35 +0530 Subject: [PATCH] Implementing the streaming response back to client Instead of using a redis client wrapped with the raw protocol, we now just use a simple tcp connection for handling request/response. There is one extra status check redis connection that is used to track the redis status and issue info/slave of etc commands This means * that Large responses are handled correctly * There is no conversion from one socket to another. Thus a performance boost esp for large responses. * Large number of events hooked up have been removed. There is still some cruft remaining with the read part of the protocol. Where a stream would help as well. --- lib/redis_proxy.js | 4 ++-- lib/server.js | 12 +++++++----- server.js | 17 ++++++----------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/redis_proxy.js b/lib/redis_proxy.js index 824dee1..25c3c78 100644 --- a/lib/redis_proxy.js +++ b/lib/redis_proxy.js @@ -33,7 +33,7 @@ var RedisProxy = module.exports = function(o){ } else { self._activeSlaves = _.without(self._activeSlaves, this); } - } + }; var onUp = function onUp() { logger.debug("We have a server that went up"); if(!self.active && this.client.server_info.role === 'master'){ @@ -46,7 +46,7 @@ var RedisProxy = module.exports = function(o){ if(self._active) this.slave(self._active); if(! _.include(self._activeSlaves, this)) self._activeSlaves.push(this); } - } + }; this.allServers = _.map(o.servers, function(server){ return new Server(_.defaults(server, {pool_size: self.options.pool_size, softErrorCount: self.options.softErrorCount})) diff --git a/lib/server.js b/lib/server.js index fdab762..1f920a9 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,7 +1,7 @@ -var _ = require('underscore'); +var _ = require('underscore'), + net = require('net'); var redis = require('redis'); -require('node-redis-raw')(redis); // make it support raw. var Pool = require('connection_pool'); @@ -58,7 +58,9 @@ Server.prototype.sendCommand = function(command, id, cb){ self._incrErrorCount(); return cb(err); } - return conn.sendRaw(command, cb); + logger.debug('sending command to redis '+ command); + conn.write(command); + return cb(null, conn); }); }; @@ -82,8 +84,8 @@ Server.prototype._createConnections = function(){ return new Pool({ create: function(cb){ try { - var client = redis.createClient(self.options.port, self.options.host); - self._attachHandlers(client); + var client = net.connect({port: self.options.port, host: self.options.host}); + // self._attachHandlers(client); return cb(null, client); } catch(err) { self._incrErrorCount(); diff --git a/server.js b/server.js index 334e917..174e998 100644 --- a/server.js +++ b/server.js @@ -27,19 +27,14 @@ var server = net.createServer(function (socket) { socket.on('data', function(data) { var command = data.toString('utf8'), id = socket.remoteAddress+':'+socket.remotePort; - redis_proxy.sendCommand(command, id, function(err, res) { + redis_proxy.sendCommand(command, id, function(err, response) { + response.removeAllListeners(); + logger.debug('got response'); if(err){ - logger.error(err); - return socket.write("+ERR "+ err+"\r\n"); + return socket.write("-ERR Error Happened "+ err); } - if(res){ - socket.write(res.toString('utf8')); - } - if(/quit/i.test(data)){ - logger.info('QUIT command received closing the connection' ); - socket.end(); - } - }); + response.pipe(socket); + }) }); });