Comparing version 1.2.6 to 1.2.7
@@ -5,2 +5,6 @@ ## Changelog | ||
### v1.2.7 - May 15, 2015 | ||
* `Redis#connect` returns a promise. | ||
### v1.2.6 - May 13, 2015 | ||
@@ -7,0 +11,0 @@ |
@@ -43,3 +43,3 @@ 'use strict'; | ||
this.connect(); | ||
this.connect().catch(function () {}); | ||
} | ||
@@ -66,32 +66,49 @@ | ||
Cluster.prototype.connect = function () { | ||
this.setStatus('connecting'); | ||
this.once('refresh', function () { | ||
this.retryAttempts = 0; | ||
this.manuallyClosing = false; | ||
this.setStatus('connect'); | ||
this.setStatus('ready'); | ||
this.executeOfflineCommands(); | ||
}); | ||
this.once('end', function () { | ||
this.removeAllListeners('refresh'); | ||
var retryDelay; | ||
if (!this.manuallyClosing && typeof this.options.clusterRetryStrategy === 'function') { | ||
retryDelay = this.options.clusterRetryStrategy(++this.retryAttempts); | ||
return new Promise(function (resolve, reject) { | ||
if (this.status === 'connecting' || this.status === 'connect' || this.status === 'ready') { | ||
reject(new Error('Redis is already connecting/connected')); | ||
return; | ||
} | ||
if (typeof retryDelay === 'number') { | ||
this.setStatus('reconnecting'); | ||
setTimeout(function () { | ||
debug('Cluster is disconnected. Retrying after %dms', retryDelay); | ||
this.connect(); | ||
}.bind(this), retryDelay); | ||
} else { | ||
this.flushQueue(new Error('None of startup nodes is available')); | ||
} | ||
}); | ||
this.setStatus('connecting'); | ||
this.startupNodes.forEach(function (options) { | ||
this.createNode(options.port, options.host); | ||
}, this); | ||
this.refreshSlotsCache(); | ||
var refreshListener = function () { | ||
this.removeListener('end', endListener); | ||
this.retryAttempts = 0; | ||
this.manuallyClosing = false; | ||
this.setStatus('connect'); | ||
this.setStatus('ready'); | ||
this.executeOfflineCommands(); | ||
resolve(); | ||
}; | ||
var endListener = function () { | ||
this.removeListener('refresh', refreshListener); | ||
reject(new Error('None of startup nodes is available')); | ||
}; | ||
this.once('refresh', refreshListener); | ||
this.once('end', endListener); | ||
this.once('close', function () { | ||
var retryDelay; | ||
if (!this.manuallyClosing && typeof this.options.clusterRetryStrategy === 'function') { | ||
retryDelay = this.options.clusterRetryStrategy(++this.retryAttempts); | ||
} | ||
if (typeof retryDelay === 'number') { | ||
this.setStatus('reconnecting'); | ||
setTimeout(function () { | ||
debug('Cluster is disconnected. Retrying after %dms', retryDelay); | ||
this.connect().catch(function () {}); | ||
}.bind(this), retryDelay); | ||
} else { | ||
this.setStatus('end'); | ||
this.flushQueue(new Error('None of startup nodes is available')); | ||
} | ||
}); | ||
this.startupNodes.forEach(function (options) { | ||
this.createNode(options.port, options.host); | ||
}, this); | ||
this.refreshSlotsCache(); | ||
}.bind(this)); | ||
}; | ||
@@ -135,3 +152,3 @@ | ||
if (Object.keys(_this.nodes).length === 0) { | ||
_this.setStatus('end'); | ||
_this.setStatus('close'); | ||
} | ||
@@ -138,0 +155,0 @@ }); |
@@ -7,3 +7,3 @@ 'use strict'; | ||
var utils = require('./utils'); | ||
var commands = require('ioredis-commands'); | ||
var commands = require('../commands'); | ||
@@ -10,0 +10,0 @@ /** |
@@ -20,3 +20,3 @@ 'use strict'; | ||
var commands = _.difference(_.keys(require('ioredis-commands'), ['monitor'])); | ||
var commands = _.difference(_.keys(require('../commands'), ['monitor'])); | ||
commands.push('sentinel', 'quit'); | ||
@@ -23,0 +23,0 @@ |
@@ -119,3 +119,3 @@ 'use strict'; | ||
} else { | ||
this.connect(); | ||
this.connect().catch(function () {}); | ||
} | ||
@@ -197,6 +197,6 @@ } | ||
Redis.prototype.setStatus = function (status) { | ||
Redis.prototype.setStatus = function (status, arg) { | ||
debug('status[%s:%s]: %s -> %s', this.options.host, this.options.port, this.status || '[empty]', status); | ||
this.status = status; | ||
process.nextTick(this.emit.bind(this, status)); | ||
process.nextTick(this.emit.bind(this, status, arg)); | ||
}; | ||
@@ -208,45 +208,61 @@ | ||
* This method will be invoked automatically when creating a new Redis instance. | ||
* @param {function} callback | ||
* @return {Promise} | ||
* @public | ||
*/ | ||
Redis.prototype.connect = function () { | ||
if (this.status === 'connecting' || this.status === 'connect') { | ||
return false; | ||
} | ||
this.setStatus('connecting'); | ||
this.condition = { | ||
select: this.options.db, | ||
auth: this.options.password, | ||
mode: { | ||
subscriber: false, | ||
monitor: false | ||
} | ||
}; | ||
var _this = this; | ||
this.connector.connect(function (err, stream) { | ||
if (err) { | ||
_this.flushQueue(err); | ||
Redis.prototype.connect = function (callback) { | ||
return new Promise(function (resolve, reject) { | ||
if (this.status === 'connecting' || this.status === 'connect' || this.status === 'ready') { | ||
reject(new Error('Redis is already connecting/connected')); | ||
return; | ||
} | ||
this.setStatus('connecting'); | ||
_this.stream = stream; | ||
this.condition = { | ||
select: this.options.db, | ||
auth: this.options.password, | ||
mode: { | ||
subscriber: false, | ||
monitor: false | ||
} | ||
}; | ||
stream.once('connect', eventHandler.connectHandler(_this)); | ||
stream.once('error', eventHandler.errorHandler(_this)); | ||
stream.once('close', eventHandler.closeHandler(_this)); | ||
stream.on('data', eventHandler.dataHandler(_this)); | ||
var _this = this; | ||
this.connector.connect(function (err, stream) { | ||
if (err) { | ||
_this.flushQueue(err); | ||
reject(err); | ||
return; | ||
} | ||
if (_this.options.connectTimeout) { | ||
stream.setTimeout(_this.options.connectTimeout, function () { | ||
stream.setTimeout(0); | ||
_this.manuallyClosing = true; | ||
stream.destroy(); | ||
}); | ||
stream.once('connect', function () { | ||
stream.setTimeout(0); | ||
}); | ||
} | ||
}); | ||
return true; | ||
_this.stream = stream; | ||
stream.once('connect', eventHandler.connectHandler(_this)); | ||
stream.once('error', eventHandler.errorHandler(_this)); | ||
stream.once('close', eventHandler.closeHandler(_this)); | ||
stream.on('data', eventHandler.dataHandler(_this)); | ||
if (_this.options.connectTimeout) { | ||
stream.setTimeout(_this.options.connectTimeout, function () { | ||
stream.setTimeout(0); | ||
_this.manuallyClosing = true; | ||
stream.destroy(); | ||
}); | ||
stream.once('connect', function () { | ||
stream.setTimeout(0); | ||
}); | ||
} | ||
var connectionReadyHandler = function () { | ||
_this.removeListener('end', connectionEndHandler); | ||
resolve(); | ||
}; | ||
var connectionEndHandler = function (err) { | ||
_this.removeListener('ready', connectionReadyHandler); | ||
reject(err); | ||
}; | ||
_this.once('ready', connectionReadyHandler); | ||
_this.once('end', connectionEndHandler); | ||
}); | ||
}.bind(this)).nodeify(callback); | ||
}; | ||
@@ -442,3 +458,3 @@ | ||
if (this.status === 'wait') { | ||
this.connect(); | ||
this.connect().catch(function () {}); | ||
} | ||
@@ -445,0 +461,0 @@ if (this.status === 'end') { |
@@ -45,3 +45,3 @@ 'use strict'; | ||
return function () { | ||
self.setStatus('end'); | ||
self.setStatus('close'); | ||
@@ -72,5 +72,5 @@ self.prevCondition = self.condition; | ||
self.setStatus('reconnecting'); | ||
self.setStatus('reconnecting', retryDelay); | ||
setTimeout(function () { | ||
self.connect(); | ||
self.connect().catch(function () {}); | ||
}, retryDelay); | ||
@@ -80,2 +80,3 @@ }; | ||
function close() { | ||
self.setStatus('end'); | ||
self.flushQueue(new Error('Connection is closed.')); | ||
@@ -82,0 +83,0 @@ } |
{ | ||
"name": "ioredis", | ||
"version": "1.2.6", | ||
"version": "1.2.7", | ||
"description": "A delightful, performance-focused Redis client for Node and io.js", | ||
@@ -10,2 +10,3 @@ "main": "index.js", | ||
"generate-docs": "jsdoc2md lib/redis.js lib/cluster.js lib/commander.js > API.md", | ||
"build": "node tools/build > commands.js", | ||
"bench": "matcha benchmarks/*.js" | ||
@@ -29,3 +30,2 @@ }, | ||
"flexbuffer": "0.0.6", | ||
"ioredis-commands": "4.0.0", | ||
"lodash": "^3.6.0" | ||
@@ -32,0 +32,0 @@ }, |
@@ -406,11 +406,16 @@ # ioredis | ||
### "ready" | ||
If `enableReadyCheck` is `true`, client will emit `ready` when the server reports that it is ready to receive commands. | ||
If `enableReadyCheck` is `true`, client will emit `ready` when the server reports that it is ready to receive commands(e.g. finish loading data from disk). | ||
Otherwise `ready` will be emitted immediately right after the `connect` event. | ||
### "close" | ||
client will emit `close` when an established Redis server connection has closed. | ||
### "reconnecting" | ||
client will emit `reconnecting` after `close` when a reconnection would be made. The argument of the event is the time(ms) before reconnecting. | ||
### "end" | ||
client will emit `end` when an established Redis server connection has closed. | ||
client will emit `end` after `close` when no more reconnections would be made. | ||
## Offline Queue | ||
When a command can't be processed by Redis(e.g. the connection hasn't been established or | ||
Redis is loading data from disk), by default it's added to the offline queue and will be | ||
When a command can't be processed by Redis(being sent before `ready` event), by default it's added to the offline queue and will be | ||
executed when it can be processed. You can disable this feature by set `enableOfflineQueue` | ||
@@ -417,0 +422,0 @@ option to `false`: |
@@ -65,2 +65,23 @@ 'use strict'; | ||
describe('#connect', function () { | ||
it('should return a promise', function (done) { | ||
var pending = 2; | ||
var redis = new Redis({ lazyConnect: true }); | ||
redis.connect().then(function () { | ||
redis.disconnect(); | ||
if (!--pending) { | ||
done(); | ||
} | ||
}); | ||
var redis2 = new Redis(6390, { lazyConnect: true, retryStrategy: null }); | ||
redis2.connect().catch(function (err) { | ||
if (!--pending) { | ||
redis2.disconnect(); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
describe('retryStrategy', function () { | ||
@@ -113,6 +134,4 @@ it('should pass the correct retry times', function (done) { | ||
}); | ||
redis.once('end', function () { | ||
pub.lpush('l', 1, function () { | ||
redis.connect(); | ||
}); | ||
redis.once('close', function () { | ||
pub.lpush('l', 1); | ||
}); | ||
@@ -119,0 +138,0 @@ }); |
@@ -6,3 +6,3 @@ 'use strict'; | ||
it('should parse options correctly', function () { | ||
stub(Redis.prototype, 'connect'); | ||
stub(Redis.prototype, 'connect').returns(Promise.resolve()); | ||
@@ -9,0 +9,0 @@ var option; |
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
239915
4
64
6382
696
- Removedioredis-commands@4.0.0
- Removedioredis-commands@4.0.0(transitive)