node-rollout
Advanced tools
Comparing version 1.0.1 to 1.1.0
55
index.js
var crypto = require('crypto') | ||
var Promise = require('bluebird') | ||
module.exports = function (client) { | ||
return new Rollout(client) | ||
module.exports = function (client, options) { | ||
return new Rollout(client, options) | ||
} | ||
function Rollout(client) { | ||
this.client = client | ||
function Rollout(client, options) { | ||
if (client && client.clientFactory) { | ||
this.clientFactory = client.clientFactory | ||
} else { | ||
this.clientFactory = function () { | ||
return client | ||
} | ||
} | ||
if (options && options.prefix) { | ||
this.prefix = options.prefix | ||
} | ||
this._handlers = {} | ||
@@ -14,3 +23,2 @@ } | ||
Rollout.prototype.handler = function (key, modifiers) { | ||
var self = this | ||
this._handlers[key] = modifiers | ||
@@ -20,5 +28,5 @@ var configPercentages = [] | ||
configPercentages.push(modifiers[modName].percentage) | ||
return key + ':' + modName | ||
}) | ||
return getRedisKeys(this.client, configKeys) | ||
return this.generate_key(key, modName) | ||
}.bind(this)) | ||
return getRedisKeys(this.clientFactory(), configKeys) | ||
.then(function(persistentPercentages) { | ||
@@ -34,3 +42,3 @@ var persistKeys = [] | ||
if (persistKeys.length) { | ||
return setRedisKeys(self.client, persistKeys) | ||
return setRedisKeys(this.clientFactory(), persistKeys) | ||
.then(function() { | ||
@@ -41,7 +49,7 @@ return persistentPercentages | ||
return persistentPercentages | ||
}) | ||
}.bind(this)) | ||
} | ||
Rollout.prototype.multi = function (keys) { | ||
var multi = this.client.multi() | ||
var multi = this.clientFactory().multi() | ||
// Accumulate get calls into a single "multi" query | ||
@@ -64,7 +72,5 @@ var promises = keys.map(function (k) { | ||
var modifiers = this._handlers[key] | ||
var keys = Object.keys(modifiers).map(this.generate_key.bind(this, key)) | ||
var likely = this.val_to_percent(key + id) | ||
var keys = Object.keys(modifiers).map(function (modName) { | ||
return key + ':' + modName | ||
}) | ||
return getRedisKeys(multi || this.client, keys) | ||
return getRedisKeys(multi || this.clientFactory(), keys) | ||
.then(function (percentages) { | ||
@@ -89,3 +95,8 @@ var i = 0 | ||
} | ||
output = modifiers[modName].condition(opt_values[modName]) | ||
try { | ||
output = modifiers[modName].condition(opt_values[modName]) | ||
} catch (err) { | ||
console.warn('rollout key[' + key + '] mod[' + modName + '] condition threw:', err) | ||
continue | ||
} | ||
if (output) { | ||
@@ -132,5 +143,5 @@ if (typeof output.then === 'function') { | ||
percentage = normalizePercentageRange(modifierPercentages[modName]) | ||
persistKeys.push(key + ':' + modName, JSON.stringify(percentage)) | ||
persistKeys.push(this.generate_key(key, modName), JSON.stringify(percentage)) | ||
} | ||
return setRedisKeys(this.client, persistKeys) | ||
return setRedisKeys(this.clientFactory(), persistKeys) | ||
} | ||
@@ -144,6 +155,6 @@ | ||
for (modName in modifiers) { | ||
keys.push(handlerName + ':' + modName) | ||
keys.push(this.generate_key(handlerName, modName)) | ||
modNames.push(modName) | ||
} | ||
return getRedisKeys(this.client, keys) | ||
return getRedisKeys(this.clientFactory(), keys) | ||
.then(function (percentages) { | ||
@@ -179,2 +190,6 @@ var modPercentages = {} | ||
Rollout.prototype.generate_key = function (key, modName) { | ||
return (this.prefix ? this.prefix + ':' : '') + key + ':' + modName | ||
} | ||
function defaultCondition() { | ||
@@ -181,0 +196,0 @@ return true |
{ | ||
"name": "node-rollout", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "feature rollout management", | ||
@@ -5,0 +5,0 @@ "author": "Ali Faiz & Dustin Diaz", |
@@ -118,2 +118,38 @@ ## Node Rollout | ||
#### Advanced Configuration | ||
#### `clientFactory` | ||
For clients that require a client factory or function that returns connections, the `clientFactory` can be given a | ||
function that returns a client. | ||
This can be useful when using `ioredis` with Cluster support. | ||
*Note*: Functions like `multi()` may not work as expected with `ioredis` clusters. | ||
``` js | ||
// client_factory_configuration.js | ||
var Redis = require('ioredis') | ||
var rollout = require('node-rollout')({ | ||
clientFactory: function () { | ||
return new Redis.Cluster([{ | ||
port: 6380, | ||
host: '127.0.0.1' | ||
}, { | ||
port: 6381, | ||
host: '127.0.0.1' | ||
}]); | ||
} | ||
}) | ||
``` | ||
#### Prefix option | ||
An optional prefix can be passed to the constructor that prepends all keys used by the rollout library. | ||
``` js | ||
var client = require('redis').createClient() | ||
var rollout = require('node-rollout')(client, { | ||
prefix: 'my_rollouts' | ||
}) | ||
``` | ||
### API Options | ||
@@ -120,0 +156,0 @@ |
@@ -12,2 +12,4 @@ var chai = require('chai') | ||
Promise.promisifyAll(redis) | ||
function isCompanyEmail(val) { | ||
@@ -380,2 +382,52 @@ return /@company\.com$/.test(val) | ||
}) | ||
context('with a prefix option', function () { | ||
beforeEach(function () { | ||
subject = rollout(redis, { prefix: 'TEST_PREFIX' }) | ||
}) | ||
it('fulfills', function () { | ||
return subject.handler('secret_feature', { | ||
employee: { | ||
percentage: 100, | ||
condition: isCompanyEmail | ||
} | ||
}) | ||
.then(function () { | ||
return redis.keysAsync('TEST_PREFIX:*') | ||
}) | ||
.then(function (keys) { | ||
expect(keys).to.have.length(1) | ||
var result = subject.get('secret_feature', 123, { | ||
employee: 'me@company.com' | ||
}) | ||
return expect(result).to.be.fulfilled | ||
}) | ||
}) | ||
}) | ||
context('with a client factory', function () { | ||
beforeEach(function () { | ||
subject = rollout({ | ||
clientFactory: function () { | ||
return redis | ||
} | ||
}) | ||
}) | ||
it('fulfills', function () { | ||
return subject.handler('secret_feature', { | ||
employee: { | ||
percentage: 100, | ||
condition: isCompanyEmail | ||
} | ||
}) | ||
.then(function () { | ||
var result = subject.get('secret_feature', 123, { | ||
employee: 'me@company.com' | ||
}) | ||
return expect(result).to.be.fulfilled | ||
}) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
640
309
77426