-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔄 Fetch with Retry for HTML build (#1793)
- Loading branch information
Showing
4 changed files
with
76 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"myst-cli": patch | ||
--- | ||
|
||
Retry html pages build and limit initial outgoing connections. |
This file contains 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 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,45 @@ | ||
import type { RequestInfo, RequestInit, Response } from 'node-fetch'; | ||
import type { ISession } from '../session/types.js'; | ||
|
||
/** | ||
* Recursively fetch a URL with retry and exponential backoff. | ||
*/ | ||
export async function fetchWithRetry( | ||
session: Pick<ISession, 'log' | 'fetch'>, | ||
/** The URL to fetch. */ | ||
url: URL | RequestInfo, | ||
/** Options to pass to fetch (e.g., headers, method). */ | ||
options?: RequestInit, | ||
/** How many times total to attempt the fetch. */ | ||
maxRetries = 3, | ||
/** The current attempt number. */ | ||
attempt = 1, | ||
/** The current backoff duration in milliseconds. */ | ||
backoff = 250, | ||
): Promise<Response> { | ||
try { | ||
const resp = await session.fetch(url, options); | ||
if (resp.ok) { | ||
// If it's a 2xx response, we consider it a success and return it | ||
return resp; | ||
} else { | ||
// For non-2xx, we treat it as a failure that triggers a retry | ||
session.log.warn( | ||
`Fetch of ${url} failed with HTTP status ${resp.status} for URL: ${url} (Attempt #${attempt})`, | ||
); | ||
} | ||
} catch (error) { | ||
// This covers network failures and other errors that cause fetch to reject | ||
session.log.warn(`Fetch of ${url} threw an error (Attempt #${attempt})`, error); | ||
} | ||
|
||
// If we haven't reached the max retries, wait and recurse | ||
if (attempt < maxRetries) { | ||
session.log.debug(`Waiting ${backoff}ms before retry #${attempt + 1}...`); | ||
await new Promise((resolve) => setTimeout(resolve, backoff)); | ||
return fetchWithRetry(session, url, options, maxRetries, attempt + 1, backoff * 2); | ||
} | ||
|
||
// If we made it here, all retries have been exhausted | ||
throw new Error(`Failed to fetch ${url} after ${maxRetries} attempts.`); | ||
} |
This file contains 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