-
Notifications
You must be signed in to change notification settings - Fork 640
refactor(timeout): make retryOnFail always a function #6762
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
Open
tomcat323
wants to merge
2
commits into
aws:master
Choose a base branch
from
tomcat323:timeoutUtil
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -222,13 +222,16 @@ interface WaitUntilOptions { | |||||
/** A backoff multiplier for how long the next interval will be (default: None, i.e 1) */ | ||||||
readonly backoff?: number | ||||||
/** | ||||||
* Only retries when an error is thrown, otherwise returning the immediate result. | ||||||
* Can also be a callback for conditional retry based on errors | ||||||
* - 'truthy' arg is ignored | ||||||
* - If the timeout is reached it throws the last error | ||||||
* - default: false | ||||||
* Call back, true when the function should be retried on failure. | ||||||
* Example usage: | ||||||
* - () => boolean if the retry logic is "constant" | ||||||
* - (error) => { error ? retryOnError(error) : boolean } | ||||||
* where retryOnError determines what errors to retry, and the boolean is for base case when there's no error (undefined) | ||||||
* 'truthy' arg is ignored | ||||||
tomcat323 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
* If the timeout is reached it throws the last error | ||||||
* Default to false | ||||||
*/ | ||||||
readonly retryOnFail?: boolean | ((error: Error) => boolean) | ||||||
readonly retryOnFail?: (error: Error | undefined) => boolean | ||||||
} | ||||||
|
||||||
export const waitUntilDefaultTimeout = 2000 | ||||||
|
@@ -243,14 +246,9 @@ export const waitUntilDefaultInterval = 500 | |||||
* | ||||||
* @returns Result of `fn()`, or possibly `undefined` depending on the arguments. | ||||||
*/ | ||||||
export async function waitUntil<T>(fn: () => Promise<T>, options: WaitUntilOptions & { retryOnFail: true }): Promise<T> | ||||||
export async function waitUntil<T>( | ||||||
fn: () => Promise<T>, | ||||||
options: WaitUntilOptions & { retryOnFail: false } | ||||||
): Promise<T | undefined> | ||||||
export async function waitUntil<T>( | ||||||
fn: () => Promise<T>, | ||||||
options: WaitUntilOptions & { retryOnFail: (error: Error) => boolean } | ||||||
options: WaitUntilOptions & { retryOnFail: (error: Error | undefined) => boolean } | ||||||
tomcat323 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
): Promise<T> | ||||||
|
||||||
export async function waitUntil<T>( | ||||||
|
@@ -264,7 +262,7 @@ export async function waitUntil<T>(fn: () => Promise<T>, options: WaitUntilOptio | |||||
interval: waitUntilDefaultInterval, | ||||||
truthy: true, | ||||||
backoff: 1, | ||||||
retryOnFail: false, | ||||||
retryOnFail: () => false, | ||||||
...options, | ||||||
} | ||||||
|
||||||
|
@@ -273,17 +271,6 @@ export async function waitUntil<T>(fn: () => Promise<T>, options: WaitUntilOptio | |||||
let elapsed: number = 0 | ||||||
let remaining = opt.timeout | ||||||
|
||||||
// Internal helper to determine if we should retry | ||||||
function shouldRetry(error: Error | undefined): boolean { | ||||||
if (error === undefined) { | ||||||
return typeof opt.retryOnFail === 'boolean' ? opt.retryOnFail : true | ||||||
} | ||||||
if (typeof opt.retryOnFail === 'function') { | ||||||
return opt.retryOnFail(error) | ||||||
} | ||||||
return opt.retryOnFail | ||||||
} | ||||||
|
||||||
for (let i = 0; true; i++) { | ||||||
const start: number = globals.clock.Date.now() | ||||||
let result: T | ||||||
|
@@ -296,7 +283,7 @@ export async function waitUntil<T>(fn: () => Promise<T>, options: WaitUntilOptio | |||||
result = await fn() | ||||||
} | ||||||
|
||||||
if (shouldRetry(lastError) || (opt.truthy && result) || (!opt.truthy && result !== undefined)) { | ||||||
if (opt.retryOnFail(lastError) || (opt.truthy && result) || (!opt.truthy && result !== undefined)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, on line 265 |
||||||
return result | ||||||
} | ||||||
} catch (e) { | ||||||
|
@@ -305,7 +292,7 @@ export async function waitUntil<T>(fn: () => Promise<T>, options: WaitUntilOptio | |||||
throw e | ||||||
} | ||||||
|
||||||
if (!shouldRetry(e)) { | ||||||
if (!opt.retryOnFail(e)) { | ||||||
throw e | ||||||
} | ||||||
|
||||||
|
@@ -317,7 +304,7 @@ export async function waitUntil<T>(fn: () => Promise<T>, options: WaitUntilOptio | |||||
|
||||||
// If the sleep will exceed the timeout, abort early | ||||||
if (elapsed + interval >= remaining) { | ||||||
if (!shouldRetry(lastError)) { | ||||||
if (!opt.retryOnFail(lastError)) { | ||||||
return undefined | ||||||
} | ||||||
throw lastError | ||||||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.