Merge pull request #115 from useblacksmith/buildx-ratelimit

src: add a retry with backoff to combat 429s when downloading buildkit
This commit is contained in:
Aditya Maru 2025-05-18 16:29:42 -04:00 committed by GitHub
commit e09a08878e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 3 deletions

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -27,13 +27,34 @@ const buildxVersion = 'v0.17.0';
const mountPoint = '/var/lib/buildkit';
const execAsync = promisify(exec);
async function retryWithBackoff<T>(operation: () => Promise<T>, maxRetries: number = 5, initialBackoffMs: number = 200): Promise<T> {
let lastError: Error = new Error('No error occurred');
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await operation();
} catch (error) {
lastError = error;
if (error.message?.includes('429') || error.status === 429) {
if (attempt < maxRetries - 1) {
const backoffMs = initialBackoffMs * Math.pow(2, attempt);
core.info(`Rate limited (429). Retrying in ${backoffMs}ms...`);
await new Promise(resolve => setTimeout(resolve, backoffMs));
continue;
}
}
throw error;
}
}
throw lastError;
}
async function setupBuildx(version: string, toolkit: Toolkit): Promise<void> {
let toolPath;
const standalone = await toolkit.buildx.isStandalone();
if (!(await toolkit.buildx.isAvailable()) || version) {
await core.group(`Download buildx from GitHub Releases`, async () => {
toolPath = await toolkit.buildxInstall.download(version || 'latest', true);
toolPath = await retryWithBackoff(() => toolkit.buildxInstall.download(version || 'latest', true));
});
}