-
Notifications
You must be signed in to change notification settings - Fork 127
Update retry logic for audio and images #13768
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
80ee91d
Add 5s pending playback timeout
dylanjeffers 24a757b
Fix mobile
dylanjeffers 45ca1eb
Fix lint
dylanjeffers 62fbf44
Add backoff starting with 2 seconds
dylanjeffers 67fe00e
Revert back to simpler approach
dylanjeffers 9ba6ce0
Update resolveStreamUrl to use resolveUrlWithCascadingTimeout
dylanjeffers e4c64e9
Address comments
dylanjeffers b0d3e1d
include primary in retries
dylanjeffers 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { resolveUrlWithCascadingTimeout } from './resolveUrlWithCascadingTimeout' | ||
|
|
||
| type StreamObject = { url?: string; mirrors?: string[] } | ||
|
|
||
| /** | ||
| * Resolves a working stream URL from a stream object using cascading timeouts: | ||
| * try primary (2s) → mirrors (2s) → mirrors (5s) → mirrors (30s) | ||
| * | ||
| * @param streamObj - The stream or preview object with url and mirrors | ||
| * @param skipCount - Number of URLs to skip (for retries after playback error) | ||
| */ | ||
| export const resolveStreamUrl = async ( | ||
| streamObj: StreamObject | null | undefined, | ||
| skipCount = 0 | ||
| ): Promise<string | null> => { | ||
| if (!streamObj?.url) { | ||
| return null | ||
| } | ||
|
|
||
| const urlsToTry: string[] = [streamObj.url] | ||
| if (streamObj.mirrors) { | ||
| for (const mirror of streamObj.mirrors) { | ||
| try { | ||
| const mirrorUrl = new URL(streamObj.url) | ||
| mirrorUrl.hostname = new URL(mirror).hostname | ||
| urlsToTry.push(mirrorUrl.toString()) | ||
| } catch { | ||
| // no-op | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return resolveUrlWithCascadingTimeout(urlsToTry, skipCount) | ||
| } |
69 changes: 69 additions & 0 deletions
69
packages/common/src/utils/resolveUrlWithCascadingTimeout.ts
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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * Cascading timeout phases for URL resolution. | ||
| * Each phase tries primary + all mirrors with the given timeout: | ||
| * - Phase 1: 2s | ||
| * - Phase 2: 5s | ||
| * - Phase 3: 30s | ||
| */ | ||
| export const CASCADING_TIMEOUTS_MS = [2000, 5000, 30000] as const | ||
|
|
||
| const tryUrlWithTimeout = async ( | ||
| url: string, | ||
| timeoutMs: number | ||
| ): Promise<boolean> => { | ||
| try { | ||
| const controller = new AbortController() | ||
| const timeoutId = setTimeout(() => controller.abort(), timeoutMs) | ||
| const response = await fetch(url, { | ||
| method: 'HEAD', | ||
| signal: controller.signal | ||
| }) | ||
| clearTimeout(timeoutId) | ||
| return response.ok | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| const tryUrlsWithTimeout = async ( | ||
| urls: string[], | ||
| timeoutMs: number | ||
| ): Promise<string | null> => { | ||
| for (const url of urls) { | ||
| if (await tryUrlWithTimeout(url, timeoutMs)) { | ||
| return url | ||
| } | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a working URL using cascading timeouts. | ||
| * Each phase tries primary + all mirrors with progressively longer timeouts: | ||
| * - Phase 1: all urls with 2s | ||
| * - Phase 2: all urls with 5s | ||
| * - Phase 3: all urls with 30s | ||
| * | ||
| * @param urls - [primary, ...mirrors] | ||
| * @param skipCount - Number of URLs to skip from the start (for retries after error) | ||
| */ | ||
| export const resolveUrlWithCascadingTimeout = async ( | ||
| urls: string[], | ||
| skipCount = 0 | ||
| ): Promise<string | null> => { | ||
| const urlsToTry = urls.slice(skipCount) | ||
| if (urlsToTry.length === 0) { | ||
| return null | ||
| } | ||
|
|
||
| const primaryFallback = urlsToTry[0] ?? null | ||
|
|
||
| for (const timeoutMs of CASCADING_TIMEOUTS_MS) { | ||
| const workingUrl = await tryUrlsWithTimeout(urlsToTry, timeoutMs) | ||
| if (workingUrl) { | ||
| return workingUrl | ||
| } | ||
| } | ||
|
|
||
| return primaryFallback | ||
| } |
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
Oops, something went wrong.
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.
why is this changing?