Skip to content

Commit

Permalink
Implementing the streaming response back to client
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sreeix committed Apr 10, 2013
1 parent 52b6ded commit 406d65a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/redis_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'){
Expand All @@ -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}))
Expand Down
12 changes: 7 additions & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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);
});
};

Expand All @@ -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();
Expand Down
17 changes: 6 additions & 11 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
});
});

Expand Down

0 comments on commit 406d65a

Please sign in to comment.