Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch/redisconfig #639

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 28 additions & 20 deletions lib/stores/redis.js
Expand Up @@ -23,13 +23,20 @@ Redis.Client = Client;
/** /**
* Redis store. * Redis store.
* Options: * Options:
* - nodeId (fn) gets an id that uniquely identifies this node * - nodeId (string|fn) gets an id that uniquely identifies this node
* - redis (fn) redis constructor, defaults to redis *
* - pack (fn) custom packing, defaults to JSON or msgpack if installed
* - unpack (fn) custom packing, defaults to JSON or msgpack if installed
*
* Light touch
* - host (string) redis server host url
* - port (number) redis server port
*
* Deep configuration
* - redis (fn) redis module, defaults to redis (node_redis)
* - redisPub (object) options to pass to the pub redis client * - redisPub (object) options to pass to the pub redis client
* - redisSub (object) options to pass to the sub redis client * - redisSub (object) options to pass to the sub redis client
* - redisClient (object) options to pass to the general redis client * - redisClient (object) options to pass to the general redis client
* - pack (fn) custom packing, defaults to JSON or msgpack if installed
* - unpack (fn) custom packing, defaults to JSON or msgpack if installed
* *
* @api public * @api public
*/ */
Expand All @@ -38,12 +45,11 @@ function Redis (opts) {
opts = opts || {}; opts = opts || {};


// node id to uniquely identify this node // node id to uniquely identify this node
var nodeId = opts.nodeId || function () { this.nodeId = ('undefined' == typeof opts.nodeId)
// by default, we generate a random id ? (function () { return Math.abs(Math.random() * Math.random() * Date.now() | 0); })() // generate random id
return Math.abs(Math.random() * Math.random() * Date.now() | 0); : ('function' == typeof opts.nodeId)
}; ? opts.nodeId()

: opts.nodeId;
this.nodeId = nodeId();


// packing / unpacking mechanism // packing / unpacking mechanism
if (opts.pack) { if (opts.pack) {
Expand All @@ -60,28 +66,30 @@ function Redis (opts) {
} }
} }


// redis setup
var redis = opts.redis || require('redis') var redis = opts.redis || require('redis')
, RedisClient = redis.RedisClient; , RedisClient = redis.RedisClient
, defaultConfig = { port: opts.port, host: opts.host };


// initialize a pubsub client and a regular client // initialize a pubsub clients and a regular client
if (opts.redisClient instanceof RedisClient) {
this.cmd = opts.redisClient;
} else {
opts.redisClient || (opts.redisClient = defaultConfig);
this.cmd = redis.createClient(opts.redisClient.port, opts.redisClient.host, opts.redisClient);
}
if (opts.redisPub instanceof RedisClient) { if (opts.redisPub instanceof RedisClient) {
this.pub = opts.redisPub; this.pub = opts.redisPub;
} else { } else {
opts.redisPub || (opts.redisPub = {}); opts.redisPub || (opts.redisPub = defaultConfig);
this.pub = redis.createClient(opts.redisPub.port, opts.redisPub.host, opts.redisPub); this.pub = redis.createClient(opts.redisPub.port, opts.redisPub.host, opts.redisPub);
} }
if (opts.redisSub instanceof RedisClient) { if (opts.redisSub instanceof RedisClient) {
this.sub = opts.redisSub; this.sub = opts.redisSub;
} else { } else {
opts.redisSub || (opts.redisSub = {}); opts.redisSub || (opts.redisSub = defaultConfig);
this.sub = redis.createClient(opts.redisSub.port, opts.redisSub.host, opts.redisSub); this.sub = redis.createClient(opts.redisSub.port, opts.redisSub.host, opts.redisSub);
} }
if (opts.redisClient instanceof RedisClient) {
this.cmd = opts.redisClient;
} else {
opts.redisClient || (opts.redisClient = {});
this.cmd = redis.createClient(opts.redisClient.port, opts.redisClient.host, opts.redisClient);
}


Store.call(this, opts); Store.call(this, opts);


Expand Down
20 changes: 20 additions & 0 deletions test/stores.redis.test.js
Expand Up @@ -16,6 +16,26 @@ var sio = require('socket.io')


module.exports = { module.exports = {


'test nodeId default assignment - random numeric value': function (done) {
var r = new RedisStore;
(typeof r.nodeId).should.equal('number'); // coerced to string
done();
},

'test nodeId custom id generator': function (done) {
var r = new RedisStore({ nodeId: function() { return 1 } });
r.nodeId.should.equal(1);
done();
},

'test nodeId assigned value': function (done) {
var a = new RedisStore({ nodeId: 'id' })
, b = new RedisStore({ nodeId: 9000 });
a.nodeId.should.equal('id');
b.nodeId.should.equal(9000);
done();
},

'test publishing doesnt get caught by the own store subscriber': function (done) { 'test publishing doesnt get caught by the own store subscriber': function (done) {
var a = new RedisStore var a = new RedisStore
, b = new RedisStore; , b = new RedisStore;
Expand Down