-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[App Config] Handle throttling - do not hang - should honor abort signal #15721
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
Changes from 17 commits
5955709
16a5238
f6270b7
ee6c764
bea4617
613407c
6579a13
93a2913
fe63f7b
1ca2bb6
b294f36
d5cfcfe
596f4cc
9becefe
dc204db
4547c23
5aac533
77b0d3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| import { AbortError, AbortSignalLike } from "@azure/abort-controller"; | ||
| import { | ||
| BaseRequestPolicy, | ||
| RequestPolicy, | ||
|
|
@@ -9,9 +10,9 @@ import { | |
| WebResource, | ||
| HttpOperationResponse, | ||
| Constants, | ||
| delay, | ||
| RestError | ||
| } from "@azure/core-http"; | ||
| import { isDefined } from "../internal/typeguards"; | ||
|
|
||
| /** | ||
| * @internal | ||
|
|
@@ -24,6 +25,57 @@ export function throttlingRetryPolicy(): RequestPolicyFactory { | |
| }; | ||
| } | ||
|
|
||
| const StandardAbortMessage = "The operation was aborted."; | ||
|
|
||
| /** | ||
| * A wrapper for setTimeout that resolves a promise after t milliseconds. | ||
| * @param delayInMs - The number of milliseconds to be delayed. | ||
| * @param abortSignal - The abortSignal associated with containing operation. | ||
| * @param abortErrorMsg - The abort error message associated with containing operation. | ||
| * @returns - Resolved promise | ||
| */ | ||
| export function delay( | ||
| delayInMs: number, | ||
| abortSignal?: AbortSignalLike, | ||
| abortErrorMsg?: string | ||
| ): Promise<void> { | ||
| return new Promise((resolve, reject) => { | ||
| let timer: ReturnType<typeof setTimeout> | undefined = undefined; | ||
| let onAborted: (() => void) | undefined = undefined; | ||
|
|
||
| const rejectOnAbort = (): void => { | ||
| return reject(new AbortError(abortErrorMsg ? abortErrorMsg : StandardAbortMessage)); | ||
| }; | ||
|
|
||
| const removeListeners = (): void => { | ||
| if (abortSignal && onAborted) { | ||
| abortSignal.removeEventListener("abort", onAborted); | ||
| } | ||
| }; | ||
|
|
||
| onAborted = (): void => { | ||
| if (isDefined(timer)) { | ||
| clearTimeout(timer); | ||
| } | ||
| removeListeners(); | ||
| return rejectOnAbort(); | ||
| }; | ||
|
|
||
| if (abortSignal && abortSignal.aborted) { | ||
| return rejectOnAbort(); | ||
| } | ||
|
|
||
| timer = setTimeout(() => { | ||
| removeListeners(); | ||
| resolve(); | ||
| }, delayInMs); | ||
|
|
||
| if (abortSignal) { | ||
| abortSignal.addEventListener("abort", onAborted); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * This policy is a close copy of the ThrottlingRetryPolicy class from | ||
| * core-http with modifications to work with how AppConfig is currently | ||
|
|
@@ -37,15 +89,19 @@ export class ThrottlingRetryPolicy extends BaseRequestPolicy { | |
| } | ||
|
|
||
| public async sendRequest(httpRequest: WebResource): Promise<HttpOperationResponse> { | ||
| return this._nextPolicy.sendRequest(httpRequest.clone()).catch((err) => { | ||
| return this._nextPolicy.sendRequest(httpRequest.clone()).catch(async (err) => { | ||
| if (isRestErrorWithHeaders(err)) { | ||
| const delayInMs = getDelayInMs(err.response.headers); | ||
|
|
||
| if (delayInMs == null) { | ||
| throw err; | ||
| } | ||
|
|
||
| return delay(delayInMs).then((_: any) => this.sendRequest(httpRequest.clone())); | ||
| await delay(delayInMs, httpRequest.abortSignal, StandardAbortMessage); | ||
| if (httpRequest.abortSignal?.aborted) { | ||
|
Member
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. It's almost redundant to call abortSignal here.
Contributor
Author
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. Yeah, kept it just in case. Should I remove it?
Member
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. No, keep it. It's correct, it just looks redundant because technically 'delay' does the same check. But, being 'async', it's possible for abortSignal.aborted to finally take affect and have it only be available afterwards. (kind of like double-checked locking) |
||
| throw new AbortError(StandardAbortMessage); | ||
| } | ||
| return await this.sendRequest(httpRequest.clone()); | ||
| } else { | ||
| throw err; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.