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

use async-retry to not handle/redo the poll-timing logic #398

Merged
merged 8 commits into from
Aug 3, 2021

Conversation

louis-bompart
Copy link
Collaborator

imho, this is a solved problem in the field, and we'd reinvent the wheel by not using a package to do it (it would be kinda an anti-pattern of how NPM works too)

@louis-bompart louis-bompart requested a review from y-lakhdar August 2, 2021 20:42
@louis-bompart louis-bompart requested a review from olamothe as a code owner August 2, 2021 20:42
@louis-bompart louis-bompart changed the base branch from master to CDX-506 August 2, 2021 20:42
iteratee = (_report: ResourceSnapshotsReportModel) => {}
public waitUntilDone(
operationToWaitFor: ResourceSnapshotsReportType | null,
options: Partial<WaitUntilDoneOptions> = {},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the spread l145, we can provide a Partial. This allow the caller to only set the options he want

return retry(
this.waitUntilDoneRetryFunction(operationToWaitFor, onRetryCb),
{
retries: Infinity,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't care about the number of attempt, so Infinity

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it supports Infinity? And that you don't have to use Number.MAX_SAFE_INTEGER

Comment on lines +154 to +155
minTimeout: toMilliseconds(opts.waitInterval),
maxTimeout: toMilliseconds(opts.waitInterval),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to retry every opts.waitInterval seconds.
Setting both limits of the Timeout ensure that this
Math.min(random * minTimeout * Math.pow(factor, attempt), maxTimeout) (see node-retry) will always be eq to opts.waitInterval
given that:

random=1 // i.e. no random
factor=2 // default value
attempt>=0 // 

=> Math.pow(factor,attempt)>=1
=> lower bound of the Math.min>=opts.waitInterval.
=> Math.min === opts.waitInterval

Comment on lines 161 to 181
private waitUntilDoneRetryFunction(
operationToWaitFor: ResourceSnapshotsReportType | null,
onRetryCb: (report: ResourceSnapshotsReportModel) => void
): () => Promise<void> {
return (async () => {
await this.refreshSnapshotData();

const isExpectedOperation =
!operationToWaitFor || this.latestReport.type === operationToWaitFor;
const isExpectedOperation =
!operationToWaitFor || this.latestReport.type === operationToWaitFor;

const isOngoing = Snapshot.ongoingReportStatuses.includes(
this.latestReport.status
);
const isOngoing = Snapshot.ongoingReportStatuses.includes(
this.latestReport.status
);

iteratee(this.latestReport);
onRetryCb(this.latestReport);

if (!isOngoing && isExpectedOperation) {
clearTimeout(timeout);
clearInterval(interval);
resolve();
}
}, toMilliseconds(opts.waitInterval));
});
if (isOngoing || !isExpectedOperation) {
throw new SnapshotOperationTimeoutError(this);
}
return;
}).bind(this);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted the function in a factory to keep things flat and simple tor ead

}
}, toMilliseconds(opts.waitInterval));
});
if (isOngoing || !isExpectedOperation) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reversed the condition using De Morgan Law to keep it simple. No return needed in the end

reject(err);
}, toMilliseconds(opts.wait));

return retry(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can return the retry straight away and thus remove the async from waitUntilDone and just use the promise created by async-retry

Copy link
Contributor

@y-lakhdar y-lakhdar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean

return retry(
this.waitUntilDoneRetryFunction(operationToWaitFor, onRetryCb),
{
retries: Infinity,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it supports Infinity? And that you don't have to use Number.MAX_SAFE_INTEGER

if (isOngoing || !isExpectedOperation) {
throw new SnapshotOperationTimeoutError(this);
}
}).bind(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why bind(this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants