Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resurrect timeout formula #833

Merged
merged 2 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions lib/ConnectionPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ class ConnectionPool {
this._ssl = opts.ssl
this._agent = opts.agent
// the resurrect timeout is 60s
// we multiply it by 2 because the resurrect formula is
// `Math.pow(resurrectTimeout * 2, deadCount -1)`
// and we don't need to multiply by 2
// the resurrectTimeout every time
this.resurrectTimeout = 1000 * 60 * 2
this.resurrectTimeout = 1000 * 60
// number of consecutive failures after which
// the timeout doesn't increase
this.resurrectTimeoutCutoff = 5
Expand Down Expand Up @@ -94,15 +90,9 @@ class ConnectionPool {
connection.status = Connection.statuses.DEAD
connection.deadCount++
// resurrectTimeout formula:
// `Math.pow(resurrectTimeout * 2, deadCount -1)`
// we don't need to multiply the resurrectTimeout by 2
// every time, it is cached during the initialization
connection.resurrectTimeout = Date.now() + Math.pow(
this.resurrectTimeout,
Math.min(
connection.deadCount - 1,
this.resurrectTimeoutCutoff
)
// `resurrectTimeout * 2 ** min(deadCount - 1, resurrectTimeoutCutoff)`
connection.resurrectTimeout = Date.now() + this.resurrectTimeout * Math.pow(
2, Math.min(connection.deadCount - 1, this.resurrectTimeoutCutoff)
)

// sort the dead list in ascending order
Expand Down
8 changes: 4 additions & 4 deletions test/behavior/resurrect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test('Should execute the recurrect API with the ping strategy', t => {
})

q.add((q, done) => {
clock.tick(10)
clock.tick(1000 * 61)
client.info((err, result) => {
t.error(err)
done()
Expand Down Expand Up @@ -133,15 +133,15 @@ test('Resurrect a node and handle 502/3/4 status code', t => {
})

q.add((q, done) => {
clock.tick(10)
clock.tick(1000 * 61)
client.info((err, result) => {
t.error(err)
done()
})
})

q.add((q, done) => {
clock.tick(150000)
clock.tick(1000 * 10 * 60)
client.info((err, result) => {
t.error(err)
done()
Expand Down Expand Up @@ -194,7 +194,7 @@ test('Should execute the recurrect API with the optimistic strategy', t => {
})

q.add((q, done) => {
clock.tick(10)
clock.tick(1000 * 61)
client.info((err, result) => {
t.error(err)
done()
Expand Down