-
Notifications
You must be signed in to change notification settings - Fork 80
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
fixed retry() and forever #76
Conversation
Codecov Report
@@ Coverage Diff @@
## master #76 +/- ##
==========================================
- Coverage 90.41% 90.34% -0.07%
==========================================
Files 2 2
Lines 146 145 -1
==========================================
- Hits 132 131 -1
Misses 14 14
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work, thank you very much!
@tim-kos |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! Thanks for the detailed explanation!
@tim-kos is there plans to publish a new version with this fix? |
0.13.1 pushed with the patch! |
TLDR (short explanation)
Problem 1
retry()
was not properly resetting.this._originalTimeouts
was being modified when callingthis._timeouts.shift()
.Solution 1
Before
After
Problem 2
When
forever: true
andretries
has been reached, it would keep retrying (expected behaviour becauseforever: true
) however it would start from the beginning ofthis._cachedTimeouts
again.The expected behaviour is to keep retrying with the last interval in
this._cachedTimeouts
.Solution 2
Before
After
Long explanation
Problem 1
The way
node-retry
works is by creating an array with a length ofretries
, whereretries
is the max number of retries.This array is stored in two variables,
this._originalTimeouts
andthis._timeouts
.Each element in this array, is the interval (in milliseconds) in-between each retry/attempt.
For each retry/attempt,
this._timeouts
is.shift()
'ed.When
retryOperation.reset()
is called,this._timeouts
is restored by referencingthis._originalTimeouts
.Arrays in Javascript are mutable, which means
this._timeouts = this._originalTimeouts;
does not make a new copy ofthis._originalTimeouts
intothis._timeouts
. Therefore, whenthis._timeouts
is.shift()
'ed,this._originalTimeouts
also gets.shift()
'ed, resulting in the loss of the intervals inthis._originalTimeouts
.Solution 1
Make a new copy of
this._originalTimeouts
and then assign it tothis._timeouts
.Before
After
Reference
Read more about
.shift()
hereRead more about
.slice()
hereRead more about mutable here
Problem 2
The way
node-retry
works is by creating an array with a length ofretries
, whereretries
is the max number of retries.This array is stored in
this._timeouts
.Each element in this array, is the interval (in milliseconds) in-between each retry/attempt.
If
forever: true
, a copy ofthis._timeouts
is made and assigned tothis._cachedTimeouts
.When
forever: true
andretries
has been reached, it would keep retrying (expected behaviour becauseforever: true
) however it would start from the beginning ofthis._cachedTimeouts
again.The expected behaviour is to keep retrying with the last interval in
this._cachedTimeouts
.An example of a current behaviour: suppose an array with the following intervals:
Retries would behave like so:
An example of an expected behaviour: suppose the same array with the same intervals:
Retries should behave like so:
Solution 2
Assign the last interval of
this._cachedTimeouts
totimeout
.timeout
is used asdelay
argument forsetTImeout
.Before
After