Skip to content

Commit

Permalink
fix(api): add retry logic when fetching updated file (#918)
Browse files Browse the repository at this point in the history
* fix(api): add retry logic when fetching updated file

* refactor: remove unneccessary if branch

* Create perfect-garlics-do.md
  • Loading branch information
ayuhito committed Dec 15, 2023
1 parent f94565c commit 1dd9def
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-garlics-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cdn": patch
---

fix(api): add retry logic when fetching updated file
20 changes: 8 additions & 12 deletions api/cdn/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ router.get('/fonts/:tag/:file', withParams, async (request, env, ctx) => {
const cacheKey = new Request(url, request.clone());
const cache = caches.default;

const response = await cache.match(cacheKey);
let response = await cache.match(cacheKey);
if (response) {
return response;
}
Expand Down Expand Up @@ -116,18 +116,14 @@ router.get('/fonts/:tag/:file', withParams, async (request, env, ctx) => {
} else {
item = await updateFile(fullTag, file, env);
}
if (item !== null) {
headers.set('ETag', item.etag);
const response = new Response(item.body, {
status: 200,
headers,
});
ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;
}

// If file does not exist, return 404
throw new StatusError(404, 'Not Found. File does not exist.');
headers.set('ETag', item.etag);
response = new Response(item.body, {
status: 200,
headers,
});
ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;
});

router.get('/css/:tag/:file', withParams, async (request, env, ctx) => {
Expand Down
49 changes: 44 additions & 5 deletions api/cdn/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,22 @@ export const updateZip = async (tag: string, env: Env) => {
}

// Check again if download.zip exists in bucket
const zip = await env.FONTS.get(`${tag}/download.zip`);
let retries = 0;
let zip = await env.FONTS.get(`${tag}/download.zip`);
while (!zip && retries < 3) {
zip = await env.FONTS.get(`${tag}/download.zip`);
retries++;
// Exponential backoff
await new Promise((resolve) => setTimeout(resolve, retries * 300));
}

if (!zip) {
throw new StatusError(
500,
'Internal Server Error. Failed to update zip file.',
);
}

return zip;
};

Expand All @@ -41,7 +56,7 @@ export const updateFile = async (tag: string, file: string, env: Env) => {
});

const resp = await fetch(req);
if (!resp.ok) {
if (resp.status !== 201) {
if (resp.status === 404) {
throw new StatusError(resp.status, 'Not Found. File does not exist.');
}
Expand All @@ -53,7 +68,19 @@ export const updateFile = async (tag: string, file: string, env: Env) => {
);
}
// Check again if file exists in bucket
const font = await env.FONTS.get(`${tag}/${file}`);
let retries = 0;
let font = await env.FONTS.get(`${tag}/${file}`);
while (!font && retries < 3) {
font = await env.FONTS.get(`${tag}/${file}`);
retries++;
// Exponential backoff
await new Promise((resolve) => setTimeout(resolve, retries * 300));
}

if (!font) {
throw new StatusError(500, 'Internal Server Error. Failed to update file.');
}

return font;
};

Expand All @@ -71,7 +98,7 @@ export const updateVariableFile = async (
});

const resp = await fetch(req);
if (!resp.ok) {
if (resp.status !== 201) {
if (resp.status === 404) {
throw new StatusError(resp.status, 'Not Found. File does not exist.');
}
Expand All @@ -83,6 +110,18 @@ export const updateVariableFile = async (
);
}
// Check again if file exists in bucket
const font = await env.FONTS.get(`${tag}/variable/${file}`);
let retries = 0;
let font = await env.FONTS.get(`${tag}/variable/${file}`);
while (!font && retries < 3) {
font = await env.FONTS.get(`${tag}/variable/${file}`);
retries++;
// Exponential backoff
await new Promise((resolve) => setTimeout(resolve, retries * 300));
}

if (!font) {
throw new StatusError(500, 'Internal Server Error. Failed to update file.');
}

return font;
};

0 comments on commit 1dd9def

Please sign in to comment.