MCPcopy Create free account
hub / github.com/alibaba/open-code-review / withRetry

Function withRetry

scripts/github-actions/post-review-comments.js:1180–1206  ·  view source on GitHub ↗
(tag, fn, log)

Source from the content-addressed store, hash-verified

1178// so rate-limit headers (retry-after / x-ratelimit-*) are honored uniformly.
1179// Throws on final failure so the caller can decide how to degrade.
1180async function withRetry(tag, fn, log) {
1181 const MAX_RETRIES = parseNonNegInt(process.env.OCR_MAX_RETRIES, 3);
1182 let lastErr;
1183 for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
1184 try {
1185 return await fn();
1186 } catch (e) {
1187 lastErr = e;
1188 const retryInfo = computeRetryDelayMs(e, attempt);
1189 const willRetry = retryInfo != null && attempt < MAX_RETRIES;
1190 if (willRetry) {
1191 const secs = (retryInfo.delayMs / 1000).toFixed(1);
1192 log(
1193 `[${tag}] transient/rate-limited (HTTP ${e.status}, attempt ${attempt + 1}/${MAX_RETRIES}). ` +
1194 `Waiting ${secs}s via '${retryInfo.source}' (${retryInfo.detail}). ${e.message}`
1195 );
1196 await sleep(retryInfo.delayMs);
1197 } else {
1198 log(`[${tag}] failed after ${attempt + 1} attempts: ${e.message}`);
1199 throw e;
1200 }
1201 }
1202 }
1203 throw lastErr != null
1204 ? lastErr
1205 : new Error(`withRetry(${tag}): exhausted retries with no error captured`);
1206}
1207
1208// Read API wrapper with retry + proactive pacing. Read requests are cheaper
1209// than writes but still consume the primary rate limit and can trigger the

Callers 1

readWithPacingFunction · 0.85

Calls 4

parseNonNegIntFunction · 0.85
computeRetryDelayMsFunction · 0.85
sleepFunction · 0.85
logFunction · 0.70

Tested by

no test coverage detected