From f7913a0aca050aa39c06c177e285b2e597ddfa57 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 17 May 2023 16:36:19 -0400 Subject: [PATCH] Fetch with retries --- src/version.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/version.js b/src/version.js index 3e7af0c..fba9240 100644 --- a/src/version.js +++ b/src/version.js @@ -48,7 +48,7 @@ function parseVersionRange(version) { async function resolveVersion({ range, isCanary }) { if (isCanary) { if (range === "latest") { - const res = await fetch("https://dl.deno.land/canary-latest.txt"); + const res = await fetchWithRetries("https://dl.deno.land/canary-latest.txt"); if (res.status !== 200) { throw new Error( "Failed to fetch canary version info from dl.deno.land. Please try again later.", @@ -60,10 +60,10 @@ async function resolveVersion({ range, isCanary }) { return { version: range, isCanary: true }; } - const res = await fetch("https://deno.com/versions.json"); + const res = await fetchWithRetries("https://deno.com/versions.json"); if (res.status !== 200) { throw new Error( - "Failed to fetch stable version info from raw.githubusercontent.com. Please try again later.", + "Failed to fetch stable version info from deno.com/versions.json. Please try again later.", ); } const versionJson = await res.json(); @@ -88,6 +88,28 @@ async function resolveVersion({ range, isCanary }) { return { version, isCanary: false }; } +/** @param {string} url */ +async function fetchWithRetries(url, maxRetries = 5) { + let sleepMs = 250; + let iterationCount = 0; + while (true) { + iterationCount++; + try { + const res = await fetch(url); + if (res.status === 200 || iterationCount > maxRetries) { + return res; + } + } catch (err) { + if (iterationCount > maxRetries) { + throw err; + } + } + console.warn(`Failed fetching. Retrying in ${sleepMs}ms...`); + await new Promise((resolve) => setTimeout(resolve, sleepMs)); + sleepMs = Math.min(sleepMs * 2, 10_000); + } +} + module.exports = { parseVersionRange, resolveVersion,