@@ -7,6 +7,17 @@ import {runCommand} from './run-command.js'
77import { execSync } from "node:child_process" ;
88
99const BRANCH = 'main'
10+ const MAX_RETRIES = 3
11+ const INITIAL_RETRY_DELAY_MS = 5000 // 5 seconds
12+
13+ /**
14+ * Sleep for the specified number of milliseconds
15+ * @param {number } ms
16+ * @returns {Promise<void> }
17+ */
18+ function sleep ( ms ) {
19+ return new Promise ( resolve => setTimeout ( resolve , ms ) )
20+ }
1021
1122/**
1223 * @typedef {Object } Schema
@@ -91,15 +102,15 @@ const schemas = [
91102/**
92103 * @param {Schema } schema
93104 * @param {import('@octokit/rest').Octokit } octokit
105+ * @param {number } retryCount
94106 * @returns {Promise<boolean> }
95107 */
96- async function fetchFileForSchema ( schema , octokit ) {
97- try {
98- // Fetch the file content from the repository
99- const branch = schema . branch ?? BRANCH
100- const owner = schema . owner
101- const repoName = schema . repo
108+ async function fetchFileForSchema ( schema , octokit , retryCount = 0 ) {
109+ const branch = schema . branch ?? BRANCH
110+ const owner = schema . owner
111+ const repoName = schema . repo
102112
113+ try {
103114 let content = ''
104115 if ( schema . usesLfs ) {
105116 console . log ( `\nFetching LFS file ${ owner } /${ repoName } #${ branch } : ${ schema . pathToFile } ...` )
@@ -137,6 +148,17 @@ async function fetchFileForSchema(schema, octokit) {
137148 console . log ( `File downloaded successfully to ${ localFilePath } ` )
138149 return true
139150 } catch ( error ) {
151+ // Handle rate limiting (429) with exponential backoff
152+ if ( error . status === 429 && retryCount < MAX_RETRIES ) {
153+ const retryAfter = error . response ?. headers ?. [ 'retry-after' ]
154+ const delayMs = retryAfter ? parseInt ( retryAfter , 10 ) * 1000 : INITIAL_RETRY_DELAY_MS * Math . pow ( 2 , retryCount )
155+ const delaySeconds = Math . round ( delayMs / 1000 )
156+
157+ console . warn ( `Rate limited for ${ owner } /${ repoName } . Retrying after ${ delaySeconds } seconds (attempt ${ retryCount + 1 } /${ MAX_RETRIES } )...` )
158+ await sleep ( delayMs )
159+ return fetchFileForSchema ( schema , octokit , retryCount + 1 )
160+ }
161+
140162 console . error ( `Error fetching file: ${ error . message } ` )
141163 return false
142164 }
0 commit comments