Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Patch/redisconfig #639

Closed
wants to merge 6 commits into from

2 participants

Commits on Oct 20, 2011
  1. @dshaw

    Simplify configuration if someone just wants to pass in one set of Re…

    dshaw authored
    …disClient configs for all 3 RedisClients. *Needs tests.*
  2. @dshaw

    Introduce defaultConfig.

    dshaw authored
Commits on Nov 14, 2011
  1. @dshaw
  2. @dshaw

    nodeId configuration options.

    dshaw authored
  3. @dshaw

    nodeId configuration support

    dshaw authored
  4. @dshaw
This page is out of date. Refresh to see the latest.
Showing with 48 additions and 20 deletions.
  1. +28 −20 lib/stores/redis.js
  2. +20 −0 test/stores.redis.test.js
View
48 lib/stores/redis.js
@@ -23,13 +23,20 @@ Redis.Client = Client;
/**
* Redis store.
* Options:
- * - nodeId (fn) gets an id that uniquely identifies this node
- * - redis (fn) redis constructor, defaults to redis
+ * - nodeId (string|fn) gets an id that uniquely identifies this node
+ *
+ * - 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
* - redisSub (object) options to pass to the sub 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
*/
@@ -38,12 +45,11 @@ function Redis (opts) {
opts = opts || {};
// node id to uniquely identify this node
- var nodeId = opts.nodeId || function () {
- // by default, we generate a random id
- return Math.abs(Math.random() * Math.random() * Date.now() | 0);
- };
-
- this.nodeId = nodeId();
+ this.nodeId = ('undefined' == typeof opts.nodeId)
+ ? (function () { return Math.abs(Math.random() * Math.random() * Date.now() | 0); })() // generate random id
+ : ('function' == typeof opts.nodeId)
+ ? opts.nodeId()
+ : opts.nodeId;
// packing / unpacking mechanism
if (opts.pack) {
@@ -60,28 +66,30 @@ function Redis (opts) {
}
}
+ // redis setup
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) {
this.pub = opts.redisPub;
} else {
- opts.redisPub || (opts.redisPub = {});
+ opts.redisPub || (opts.redisPub = defaultConfig);
this.pub = redis.createClient(opts.redisPub.port, opts.redisPub.host, opts.redisPub);
}
if (opts.redisSub instanceof RedisClient) {
this.sub = opts.redisSub;
} else {
- opts.redisSub || (opts.redisSub = {});
+ opts.redisSub || (opts.redisSub = defaultConfig);
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);
View
20 test/stores.redis.test.js
@@ -16,6 +16,26 @@ var sio = require('socket.io')
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) {
var a = new RedisStore
, b = new RedisStore;
Something went wrong with that request. Please try again.