From f6df5f936cc102d69e6b294a9244b4f87adffa89 Mon Sep 17 00:00:00 2001 From: claude-bot Date: Sun, 7 Jun 2026 23:36:30 +0900 Subject: [PATCH] installer-rp: retry image download on HTTP 429/5xx with backoff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit i.ytimg.com 썸네일 서버가 연속 요청을 속도제한(HTTP 429)하면 사진 다운로드가 즉시 실패해 전체 설치가 중단됐다. 일시적 상태코드 (408/425/429/5xx)와 네트워크 오류를 Retry-After 우선 + 지수 백오프(jitter)로 최대 5회 재시도하도록 fetchBuffer 를 보강. v0.3.9. Co-Authored-By: Claude Opus 4 --- package.json | 2 +- src/installer-rp/images.ts | 52 +++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 3134068..2d3deb5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minecraft-music-quiz-installer", - "version": "0.3.8", + "version": "0.3.9", "description": "마인크래프트 음악퀴즈 간편설치기 + 관리 사이트", "main": "dist/installer/main.js", "scripts": { diff --git a/src/installer-rp/images.ts b/src/installer-rp/images.ts index 8b59c43..3c954dc 100644 --- a/src/installer-rp/images.ts +++ b/src/installer-rp/images.ts @@ -29,8 +29,35 @@ export function ytIdFromUrl(url: string): string { } } -/** 단순 HTTP/HTTPS GET (302 따라감, 4xx/5xx 는 reject). */ -function fetchBuffer(url: string, redirects = 0): Promise { +/** + * 일시적(transient) 으로 보고 재시도할 HTTP 상태코드. + * 429 = Too Many Requests (i.ytimg.com 썸네일 서버가 연속 요청을 속도제한). + * 5xx 게이트웨이 계열도 잠깐 뒤 다시 받으면 성공하는 경우가 많다. + */ +const TRANSIENT_CODES = new Set([408, 425, 429, 500, 502, 503, 504]) +const MAX_RETRIES = 5 +/** 백오프 상한(ms). Retry-After 헤더가 비정상적으로 커도 이 이상은 기다리지 않는다. */ +const MAX_BACKOFF_MS = 60000 + +/** Retry-After 헤더(초 또는 HTTP-date) → 대기 ms. 못 읽으면 null. */ +function parseRetryAfter(h: string | string[] | undefined): number | null { + if (!h) return null + const v = Array.isArray(h) ? h[0] : h + const secs = Number(v) + if (Number.isFinite(secs)) return Math.min(MAX_BACKOFF_MS, Math.max(0, secs * 1000)) + const date = Date.parse(v) + if (!Number.isNaN(date)) return Math.min(MAX_BACKOFF_MS, Math.max(0, date - Date.now())) + return null +} + +const sleep = (ms: number): Promise => new Promise((r) => setTimeout(r, ms)) + +/** + * 단순 HTTP/HTTPS GET (302 따라감). + * 429/5xx 등 일시적 오류는 지수 백오프(+jitter, Retry-After 우선)로 최대 + * MAX_RETRIES 회 재시도한다. 그 외 4xx 나 재시도 소진 시 reject. + */ +function fetchBuffer(url: string, redirects = 0, attempt = 0): Promise { return new Promise((resolve, reject) => { if (redirects > 8) { reject(new Error(t('common.tooManyRedirects'))) @@ -38,6 +65,11 @@ function fetchBuffer(url: string, redirects = 0): Promise { } const target = new URL(url) const lib = target.protocol === 'https:' ? https : http + const retryLater = (headerDelay: number | null): void => { + const backoff = Math.min(MAX_BACKOFF_MS, 1000 * 2 ** attempt) + Math.floor(Math.random() * 500) + const delay = headerDelay ?? backoff + sleep(delay).then(() => fetchBuffer(url, redirects, attempt + 1).then(resolve, reject)) + } const req = lib.get(target, { timeout: 30000, headers: { 'user-agent': 'mc-music-quiz-rp-installer' } @@ -45,10 +77,15 @@ function fetchBuffer(url: string, redirects = 0): Promise { const code = res.statusCode || 0 if (code >= 300 && code < 400 && res.headers.location) { res.resume() - fetchBuffer(new URL(res.headers.location, target).toString(), redirects + 1) + fetchBuffer(new URL(res.headers.location, target).toString(), redirects + 1, attempt) .then(resolve, reject) return } + if (TRANSIENT_CODES.has(code) && attempt < MAX_RETRIES) { + res.resume() + retryLater(parseRetryAfter(res.headers['retry-after'])) + return + } if (code !== 200) { res.resume() reject(new Error(`HTTP ${code}`)) @@ -58,7 +95,14 @@ function fetchBuffer(url: string, redirects = 0): Promise { res.on('data', (c: Buffer) => chunks.push(c)) res.on('end', () => resolve(Buffer.concat(chunks))) }) - req.on('error', reject) + req.on('error', (err) => { + // 연결 끊김/리셋 등 네트워크 오류도 몇 번은 재시도. + if (attempt < MAX_RETRIES) { + retryLater(null) + return + } + reject(err) + }) req.on('timeout', () => req.destroy(new Error(t('common.requestTimeout')))) }) }