-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add maxRetriesPerRequest option to limit the retries attempts p…
…er command #634, #61 BREAKING CHANGE: The maxRetriesPerRequest is set to 20 instead of null (same behavior as ioredis v3) by default. So when a redis server is down, pending commands won't wait forever until the connection become alive, instead, they only wait about 10s (depends on the retryStrategy option)
- Loading branch information
Showing
6 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = class MaxRetriesPerRequestError extends Error { | ||
constructor (maxRetriesPerRequest) { | ||
var message = `Reached the max retries per request limit (which is ${maxRetriesPerRequest}). Refer to "maxRetriesPerRequest" option for details.`; | ||
|
||
super(message); | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.MaxRetriesPerRequestError = require('./MaxRetriesPerRequestError') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
var {MaxRetriesPerRequestError} = require('../../lib/errors') | ||
|
||
describe('maxRetriesPerRequest', function () { | ||
it('throw the correct error when reached the limit', function (done) { | ||
var redis = new Redis(9999, { | ||
retryStrategy() { | ||
return 1 | ||
} | ||
}); | ||
redis.get('foo', (err) => { | ||
expect(err).instanceOf(MaxRetriesPerRequestError) | ||
done() | ||
}) | ||
}) | ||
|
||
it('defaults to max 20 retries', function (done) { | ||
var redis = new Redis(9999, { | ||
retryStrategy() { | ||
return 1 | ||
} | ||
}); | ||
redis.get('foo', () => { | ||
expect(redis.retryAttempts).to.eql(21) | ||
redis.get('foo', () => { | ||
expect(redis.retryAttempts).to.eql(42) | ||
done() | ||
}) | ||
}) | ||
}); | ||
|
||
it('can be changed', function (done) { | ||
var redis = new Redis(9999, { | ||
maxRetriesPerRequest: 1, | ||
retryStrategy() { | ||
return 1 | ||
} | ||
}); | ||
redis.get('foo', () => { | ||
expect(redis.retryAttempts).to.eql(2) | ||
redis.get('foo', () => { | ||
expect(redis.retryAttempts).to.eql(4) | ||
done() | ||
}) | ||
}) | ||
}); | ||
|
||
it('allows 0', function (done) { | ||
var redis = new Redis(9999, { | ||
maxRetriesPerRequest: 0, | ||
retryStrategy() { | ||
return 1 | ||
} | ||
}); | ||
redis.get('foo', () => { | ||
expect(redis.retryAttempts).to.eql(1) | ||
redis.get('foo', () => { | ||
expect(redis.retryAttempts).to.eql(2) | ||
done() | ||
}) | ||
}) | ||
}); | ||
}); |