Skip to content

feat(clients): adding throwable waiters WaitUntil[operationState] #2302

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

Merged
merged 6 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions packages/util-waiter/src/createWaiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ export const createWaiter = async <Client, Input>(
if (options.abortController) {
exitConditions.push(abortTimeout(options.abortController.signal));
}

if (options.abortSignal) {
exitConditions.push(abortTimeout(options.abortSignal));
}

return Promise.race(exitConditions);
};
4 changes: 2 additions & 2 deletions packages/util-waiter/src/poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const randomInRange = (min: number, max: number) => min + Math.random() * (max -
* @param stateChecker function that checks the acceptor states on each poll.
*/
export const runPolling = async <Client, Input>(
{ minDelay, maxDelay, maxWaitTime, abortController, client }: WaiterOptions<Client>,
{ minDelay, maxDelay, maxWaitTime, abortController, client, abortSignal }: WaiterOptions<Client>,
input: Input,
acceptorChecks: (client: Client, input: Input) => Promise<WaiterResult>
): Promise<WaiterResult> => {
Expand All @@ -36,7 +36,7 @@ export const runPolling = async <Client, Input>(
// Pre-compute this number to avoid Number type overflow.
const attemptCeiling = Math.log(maxDelay / minDelay) / Math.log(2) + 1;
while (true) {
if (abortController?.signal?.aborted) {
if (abortController?.signal?.aborted || abortSignal?.aborted) {
return { state: WaiterState.ABORTED };
}
const delay = exponentialBackoffWithJitter(minDelay, maxDelay, attemptCeiling, currentAttempt);
Expand Down
30 changes: 30 additions & 0 deletions packages/util-waiter/src/waiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ export interface WaiterConfiguration<Client> {
maxWaitTime: number;

/**
* @deprecated Use abortSignal
* Abort controller. Used for ending the waiter early.
*/
abortController?: AbortController;

/**
* Abort Signal. Used for ending the waiter early.
*/
abortSignal?: AbortController["signal"];

/**
* The minimum amount of time to delay between retries in seconds. This is the
* floor of the exponential backoff. This value defaults to service default
Expand Down Expand Up @@ -55,4 +61,28 @@ export enum WaiterState {

export type WaiterResult = {
state: WaiterState;

/**
* (optional) Indicates a reason for why a waiter has reached its state.
*/
reason?: any;
};

/**
* Handles and throws exceptions resulting from the waiterResult
* @param result WaiterResult
*/
export const checkExceptions = (result: WaiterResult): WaiterResult => {
if (result.state === WaiterState.ABORTED) {
throw new Error(
`${JSON.stringify({
...result,
name: "AbortError",
reason: "Request was aborted",
})}`
);
} else if (result.state !== WaiterState.SUCCESS) {
throw new Error(`${JSON.stringify({ result })}`);
}
return result;
};