Refactor: simplify parallel cached fetching

This commit is contained in:
SukkaW 2024-10-10 13:58:56 +08:00
parent 36d2b1102d
commit a92a0f6f1a

View File

@ -305,40 +305,9 @@ export class Cache<S = string> {
const previouslyCached = this.get(cachedKey); const previouslyCached = this.get(cachedKey);
const primaryETag = this.get(getETagKey(primaryUrl));
const fetchMainPromise = fetchWithRetry(
primaryUrl,
{
signal: controller.signal,
...defaultRequestInit,
headers: (typeof primaryETag === 'string' && primaryETag.length > 0)
? mergeHeaders(
defaultRequestInit.headers,
{ 'If-None-Match': primaryETag }
)
: defaultRequestInit.headers
}
).then(r => {
if (r.headers.has('etag')) {
this.set(getETagKey(primaryUrl), r.headers.get('etag')!, TTL.ONE_WEEK_STATIC);
// If we do not have a cached value, we ignore 304
if (r.status === 304 && typeof previouslyCached === 'string') {
controller.abort();
throw new Custom304NotModifiedError(primaryUrl, previouslyCached);
}
} else if (!primaryETag && typeof previouslyCached === 'string') {
throw new CustomNoETagFallbackError(previouslyCached);
}
return r.text();
}).then(text => {
controller.abort();
return text;
});
const createFetchFallbackPromise = async (url: string, index: number) => { const createFetchFallbackPromise = async (url: string, index: number) => {
// Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 500ms before downloading from the fallback URL. // Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 500ms before downloading from the fallback URL.
if (index > 0) {
try { try {
await sleepWithAbort(300 + (index + 1) * 10, controller.signal); await sleepWithAbort(300 + (index + 1) * 10, controller.signal);
} catch { } catch {
@ -349,6 +318,7 @@ export class Cache<S = string> {
console.log(picocolors.gray('[fetch cancelled]'), picocolors.gray(url)); console.log(picocolors.gray('[fetch cancelled]'), picocolors.gray(url));
throw new CustomAbortError(); throw new CustomAbortError();
} }
}
const etag = this.get(getETagKey(url)); const etag = this.get(getETagKey(url));
const res = await fetchWithRetry( const res = await fetchWithRetry(
@ -373,11 +343,13 @@ export class Cache<S = string> {
controller.abort(); controller.abort();
throw new Custom304NotModifiedError(url, previouslyCached); throw new Custom304NotModifiedError(url, previouslyCached);
} }
} else if (!primaryETag && typeof previouslyCached === 'string') { } else if (!this.get(getETagKey(primaryUrl)) && typeof previouslyCached === 'string') {
controller.abort(); controller.abort();
throw new CustomNoETagFallbackError(previouslyCached); throw new CustomNoETagFallbackError(previouslyCached);
} }
// either no etag and not cached
// or has etag but not 304
const text = await res.text(); const text = await res.text();
controller.abort(); controller.abort();
return text; return text;
@ -385,7 +357,7 @@ export class Cache<S = string> {
try { try {
const text = await Promise.any([ const text = await Promise.any([
fetchMainPromise, createFetchFallbackPromise(primaryUrl, -1),
...mirrorUrls.map(createFetchFallbackPromise) ...mirrorUrls.map(createFetchFallbackPromise)
]); ]);
@ -405,7 +377,7 @@ export class Cache<S = string> {
if (error instanceof Custom304NotModifiedError) { if (error instanceof Custom304NotModifiedError) {
console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl)); console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl));
this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC); this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC);
return deserializer(previouslyCached); return deserializer(error.data);
} }
if (error instanceof CustomNoETagFallbackError) { if (error instanceof CustomNoETagFallbackError) {
console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl)); console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl));