Comparing version 0.1.2 to 0.2.0
@@ -92,4 +92,8 @@ var _ = require('lodash'); | ||
} else { | ||
// Redis(port, host, options) | ||
this.options = _.defaults(options ? _.cloneDeep(options) : {}, { port: port, host: host }); | ||
// Redis(port, host, options) or Redis(port, options) | ||
if (host && typeof host === 'object') { | ||
this.options = _.defaults(_.cloneDeep(host), { port: port }); | ||
} else { | ||
this.options = _.defaults(options ? _.cloneDeep(options) : {}, { port: port, host: host }); | ||
} | ||
} | ||
@@ -145,6 +149,5 @@ | ||
Redis.prototype.connect = function () { | ||
this.retryAttempts += 1; | ||
if (this._sentinel) { | ||
var _this = this; | ||
this._sentinel.removeAllListeners(); | ||
this._sentinel.once('connect', function (connection) { | ||
@@ -155,2 +158,5 @@ debug('received connection from sentinel'); | ||
}); | ||
this._sentinel.on('error', function (error) { | ||
_this.silentEmit('error', error); | ||
}); | ||
this._sentinel.connect(this.options.role); | ||
@@ -188,9 +194,11 @@ } else { | ||
if (options.destroy) { | ||
this.connection.destroy(); | ||
} else { | ||
this.connection.end(); | ||
if (this.connection) { | ||
if (options.destroy) { | ||
this.connection.destroy(); | ||
} else { | ||
this.connection.end(); | ||
} | ||
} | ||
if (this._sentinel) { | ||
this._sentinel.sentinel.disconnect(options); | ||
this._sentinel.disconnect(options); | ||
} | ||
@@ -197,0 +205,0 @@ }; |
@@ -20,7 +20,6 @@ /** | ||
db: 0, | ||
sentinel: { | ||
endpoints: null, | ||
name: null, | ||
role: 'master' | ||
} | ||
role: 'master', | ||
sentinel: null, | ||
roleRetryDelay: 500, | ||
name: null | ||
}; |
@@ -46,5 +46,8 @@ var Queue = require('fastqueue'); | ||
} | ||
if (self.options.role && info.role && self.options.role !== info.role) { | ||
if (self._sentinel && info.role && self.options.role !== info.role) { | ||
debug('role invalid, expected %s, but got %s', self.options.role, info.role); | ||
self.disconnect({ reconnect: true }); | ||
self.disconnect(); | ||
setTimeout(function () { | ||
self.connect(); | ||
}, self.options.roleRetryDelay); | ||
return; | ||
@@ -74,3 +77,3 @@ } | ||
} | ||
var retryDelay = self.options.retryStrategy(self.retryAttempts); | ||
var retryDelay = self.options.retryStrategy(++self.retryAttempts); | ||
@@ -77,0 +80,0 @@ if (typeof retryDelay !== 'number') { |
@@ -13,4 +13,6 @@ var util = require('util'); | ||
this.endpoints = endpoints; | ||
this.endpointsIndex = 0; | ||
this.role = role; | ||
this.name = name; | ||
this.connecting = 0; | ||
} | ||
@@ -21,5 +23,22 @@ | ||
Sentinel.prototype.connect = function () { | ||
var item = this.endpoints.shift(); | ||
this.endpoints.push(item); | ||
this.connecting = true; | ||
var _this = this; | ||
if (this.endpointsIndex === this.endpoints.length) { | ||
this.emit('error', new Error('All sentinels are unreachable')); | ||
if (this.endpoints.length > 0) { | ||
this.endpointsIndex = 0; | ||
setImmediate(function () { | ||
if (_this.connecting) { | ||
_this.connect(); | ||
} | ||
}); | ||
} | ||
return; | ||
} | ||
var item = this.endpoints[this.endpointsIndex]; | ||
this.endpointsIndex += 1; | ||
debug('connecting to sentinel %s:%s', item.host, item.port); | ||
@@ -35,19 +54,25 @@ | ||
var _this = this; | ||
resolveClient(this, function (err, result) { | ||
resolveClient(this, utils.timeout(function (err, result) { | ||
if (result) { | ||
debug('get %s from sentinels %s:%s', _this.role, result.host, result.port); | ||
var connection = net.createConnection(result); | ||
_this.emit('connect', connection); | ||
debug('get %s from sentinels %s:%s', _this.role || 'master', result.host, result.port); | ||
_this.emit('connect', net.createConnection(result)); | ||
} else { | ||
if (err) { | ||
debug('get %s failed with error %s, try next sentinel...', _this.role, err.message); | ||
debug('get %s failed with error "%s"', _this.role, err.message); | ||
} else { | ||
debug('get %s failed, try next sentinel...', _this.role); | ||
debug('get %s failed', _this.role); | ||
} | ||
_this.connect(); | ||
if (_this.connecting) { | ||
debug('try next sentinel...'); | ||
_this.connect(); | ||
} | ||
} | ||
}); | ||
}, 1000)); | ||
}; | ||
Sentinel.prototype.disconnect = function (options) { | ||
this.connecting = false; | ||
this.sentinel.disconnect(options); | ||
}; | ||
function resolveClient(self, callback) { | ||
@@ -66,3 +91,3 @@ debug('connected to sentinel'); | ||
var slave = utils.packObject(result[i]); | ||
if (slave.flags.indexOf('disconnected') === -1) { | ||
if (slave.flags && slave.flags.indexOf('disconnected') === -1) { | ||
availableSlaves.push(slave); | ||
@@ -69,0 +94,0 @@ } |
@@ -105,1 +105,14 @@ /** Test if two buffers are equal | ||
}; | ||
exports.timeout = function (callback, timeout) { | ||
var timer; | ||
var run = function () { | ||
if (timer) { | ||
clearTimeout(timer); | ||
timer = null; | ||
callback.apply(this, arguments); | ||
} | ||
}; | ||
timer = setTimeout(run, timeout, new Error('timeout')); | ||
return run; | ||
}; |
{ | ||
"name": "ioredis", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "A delightful, performance-focused Redis client for Node and io.js", | ||
@@ -33,2 +33,3 @@ "main": "index.js", | ||
"mocha": "^2.2.1", | ||
"server-destroy": "^1.0.0", | ||
"sinon": "^1.14.1" | ||
@@ -35,0 +36,0 @@ }, |
38
test.js
var Redis = require('./'); | ||
// var redis = new Redis(6379); | ||
var redis = new Redis({ | ||
sentinels: [ | ||
{ host: '127.0.0.1', port: '26379' }, | ||
{ host: '127.0.0.1', port: '26380' }, | ||
], | ||
name: 'master1', | ||
role: 'master' | ||
var redis = new Redis(6388, 'localhost', { | ||
enableReadyCheck: false | ||
}); | ||
// var redis = new Redis({ | ||
// sentinels: [ | ||
// { host: '127.0.0.1', port: '26379' }, | ||
// { host: '127.0.0.1', port: '26380' }, | ||
// ], | ||
// name: 'master1', | ||
// role: 'master' | ||
// }); | ||
redis.set('foo', 'bar', function () { | ||
@@ -16,12 +18,12 @@ console.log(arguments); | ||
var i = 0; | ||
setInterval(function () { | ||
redis.set('foo', 'bar', function () { | ||
if (arguments[1] === 'bar') { | ||
console.log(++i); | ||
} else { | ||
console.log(arguments); | ||
} | ||
}); | ||
}, 300); | ||
// var i = 0; | ||
// setInterval(function () { | ||
// redis.set('foo', 'bar', function () { | ||
// if (arguments[1] === 'bar') { | ||
// console.log(++i); | ||
// } else { | ||
// console.log(arguments); | ||
// } | ||
// }); | ||
// }, 300); | ||
@@ -28,0 +30,0 @@ // redis.defineCommand('loop', { |
@@ -44,2 +44,8 @@ describe('Redis', function () { | ||
expect(option7).to.have.property('host', '192.168.1.1'); | ||
var option8 = getOption(6380, { | ||
host: '192.168.1.1' | ||
}); | ||
expect(option8).to.have.property('port', 6380); | ||
expect(option8).to.have.property('host', '192.168.1.1'); | ||
} catch (err) { | ||
@@ -46,0 +52,0 @@ Redis.prototype.connect.restore(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
720457
66
3756
5
3