-
Notifications
You must be signed in to change notification settings - Fork 580
fix(routerlicious-driver): retry transient network errors in restWrapper #27631
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
arafat-java
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
arafat-java:fix/routerlicious-driver-retry-transient-network-errors
base: main
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.
+194
−18
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
|
|
||
| import { performanceNow } from "@fluid-internal/client-utils"; | ||
| import type { ITelemetryBaseProperties } from "@fluidframework/core-interfaces"; | ||
| import { assert } from "@fluidframework/core-utils/internal"; | ||
| import { assert, delay } from "@fluidframework/core-utils/internal"; | ||
| import type { RateLimiter } from "@fluidframework/driver-utils/internal"; | ||
| import { GenericNetworkError, NonRetryableError } from "@fluidframework/driver-utils/internal"; | ||
| import { | ||
|
|
@@ -48,6 +48,9 @@ const buildRequestInitConfig = (requestConfig: RequestConfig): RequestInit => { | |
| return requestInit; | ||
| }; | ||
|
|
||
| // Transport-level failures that are safe to retry on a fresh socket. | ||
| const transientNetworkErrorPattern = /socket hang up|ECONNRESET|EPIPE/i; | ||
|
|
||
| export interface IR11sResponse<T> { | ||
| content: T; | ||
| headers: Map<string, string>; | ||
|
|
@@ -117,6 +120,14 @@ class RouterliciousRestWrapper extends RestWrapper { | |
| */ | ||
| private readonly retryCounter = new Map<string, number>(); | ||
|
|
||
| /** | ||
| * Maximum number of attempts (1 initial + retries) for a single fetch that fails with a transient network error. | ||
| */ | ||
| private static readonly maxNetworkErrorAttempts = 3; | ||
|
|
||
| /** Delay between transient network-error retries. */ | ||
| private static readonly networkErrorRetryDelayMs = 250; | ||
|
|
||
| constructor( | ||
| logger: TelemetryLoggerExt, | ||
| private readonly rateLimiter: RateLimiter, | ||
|
|
@@ -169,9 +180,17 @@ class RouterliciousRestWrapper extends RestWrapper { | |
| const fetchRequestConfig = buildRequestInitConfig(translatedConfig); | ||
|
|
||
| const res = await this.rateLimiter.schedule(async () => { | ||
| const perfStart = performanceNow(); | ||
| const result = await fetch(completeRequestUrl, fetchRequestConfig).catch( | ||
| async (error) => { | ||
| // Retry transient transport failures (e.g. a pooled socket closed by the peer while | ||
| // this process's execution context was frozen) on a fresh socket before surfacing the error. | ||
| for (let attempt = 1; ; attempt++) { | ||
| const perfStart = performanceNow(); | ||
| try { | ||
| const result = await fetch(completeRequestUrl, fetchRequestConfig); | ||
| return { | ||
| response: result, | ||
| duration: performanceNow() - perfStart, | ||
| }; | ||
| } catch (error: any) { | ||
|
Contributor
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. for improved type safety, type errors as |
||
| // on failure, add the request entry into the retryCounter map to count the subsequent retries, if any | ||
| this.retryCounter.set(requestKey, requestRetryCount ? requestRetryCount + 1 : 1); | ||
|
|
||
|
|
@@ -187,12 +206,34 @@ class RouterliciousRestWrapper extends RestWrapper { | |
| const errorMessage = isNetworkError | ||
| ? `NetworkError: ${error.message}` | ||
| : safeStringify(error); | ||
|
|
||
| // A self-signed-cert failure is permanent, never transient — exclude it from retry. | ||
| const isSelfSignedCertError = errorMessage.includes( | ||
| "failed, reason: self signed certificate", | ||
| ); | ||
| const errorCode: unknown = error?.code ?? error?.cause?.code; | ||
| const isTransientNetworkError = | ||
| !isSelfSignedCertError && | ||
| (isNetworkError || | ||
| (typeof errorCode === "string" && | ||
| transientNetworkErrorPattern.test(errorCode)) || | ||
| transientNetworkErrorPattern.test(error?.message ?? "") || | ||
| transientNetworkErrorPattern.test(error?.cause?.message ?? "")); | ||
| if ( | ||
| isTransientNetworkError && | ||
| canRetry && | ||
| attempt < RouterliciousRestWrapper.maxNetworkErrorAttempts | ||
| ) { | ||
| await delay(RouterliciousRestWrapper.networkErrorRetryDelayMs); | ||
| continue; | ||
| } | ||
|
|
||
| // If a service is temporarily down or a browser resource limit is reached, RestWrapper will throw | ||
| // a network error with no status code (e.g. err:ERR_CONN_REFUSED or err:ERR_FAILED) and | ||
| // the error message will start with NetworkError as defined in restWrapper.ts | ||
| // If there exists a self-signed SSL certificates error, throw a NonRetryableError | ||
| // TODO: instead of relying on string matching, filter error based on the error code like we do for websocket connections | ||
| const err = errorMessage.includes("failed, reason: self signed certificate") | ||
| const err = isSelfSignedCertError | ||
| ? new NonRetryableError( | ||
| errorMessage, | ||
| RouterliciousErrorTypes.sslCertError, | ||
|
|
@@ -204,12 +245,8 @@ class RouterliciousRestWrapper extends RestWrapper { | |
| telemetryProps, | ||
| ); | ||
| throw err; | ||
| }, | ||
| ); | ||
| return { | ||
| response: result, | ||
| duration: performanceNow() - perfStart, | ||
| }; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| const response = res.response; | ||
|
|
||
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.
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.
In networking, I generally expect https://en.wikipedia.org/wiki/Exponential_backoff unless there is some known category of issue this timeout is selected to be relevant to. Might be good to document why this specific duration makes sense.
Might also be good to document the consequences of setting this or the retry count higher (I assume it can result in something taking longer before failing, or maybe hiding flakey service issues?).
Anyway, this isn't an area I work in, so I'm no expert here, those are just my thoughts.