Federation listing timeouts
parent
9d5f4300bc
commit
bd4c86cd33
|
@ -31,6 +31,7 @@ async function responseToFileMeta(
|
||||||
|
|
||||||
const fileListingPrefixCacheKey = `federationListCache:`;
|
const fileListingPrefixCacheKey = `federationListCache:`;
|
||||||
const listingCacheTimeout = 1000 * 30;
|
const listingCacheTimeout = 1000 * 30;
|
||||||
|
const listingFetchTimeout = 2000;
|
||||||
|
|
||||||
type FileListingCacheEntry = {
|
type FileListingCacheEntry = {
|
||||||
items: FileMeta[];
|
items: FileMeta[];
|
||||||
|
@ -52,29 +53,29 @@ export async function listFiles(): Promise<FileMeta[]> {
|
||||||
fileMetas = fileMetas.concat(cachedListing.items);
|
fileMetas = fileMetas.concat(cachedListing.items);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("Fetching from federated", config);
|
console.log("Fetching listing from federated", config);
|
||||||
const uriParts = config.uri.split("/");
|
const uriParts = config.uri.split("/");
|
||||||
const rootUri = uriParts[0];
|
const rootUri = uriParts[0];
|
||||||
const prefix = uriParts.slice(1).join("/");
|
const prefix = uriParts.slice(1).join("/");
|
||||||
const indexUrl = `${federatedPathToUrl(rootUri)}/index.json`;
|
const indexUrl = `${federatedPathToUrl(rootUri)}/index.json`;
|
||||||
try {
|
try {
|
||||||
|
const fetchController = new AbortController();
|
||||||
|
const timeout = setTimeout(
|
||||||
|
() => fetchController.abort(),
|
||||||
|
listingFetchTimeout,
|
||||||
|
);
|
||||||
|
|
||||||
const r = await nativeFetch(indexUrl, {
|
const r = await nativeFetch(indexUrl, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
},
|
},
|
||||||
|
signal: fetchController.signal,
|
||||||
});
|
});
|
||||||
|
clearTimeout(timeout);
|
||||||
|
|
||||||
if (r.status !== 200) {
|
if (r.status !== 200) {
|
||||||
console.error(
|
throw new Error(`Got status ${r.status}`);
|
||||||
`Failed to fetch ${indexUrl}. Skipping.`,
|
|
||||||
r.status,
|
|
||||||
r.statusText,
|
|
||||||
);
|
|
||||||
if (cachedListing) {
|
|
||||||
console.info("Using cached listing");
|
|
||||||
fileMetas = fileMetas.concat(cachedListing.items);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
const jsonResult = await r.json();
|
const jsonResult = await r.json();
|
||||||
const items: FileMeta[] = jsonResult.filter((meta: FileMeta) =>
|
const items: FileMeta[] = jsonResult.filter((meta: FileMeta) =>
|
||||||
|
@ -91,6 +92,10 @@ export async function listFiles(): Promise<FileMeta[]> {
|
||||||
fileMetas = fileMetas.concat(items);
|
fileMetas = fileMetas.concat(items);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error("Failed to process", indexUrl, e);
|
console.error("Failed to process", indexUrl, e);
|
||||||
|
if (cachedListing) {
|
||||||
|
console.info("Using cached listing");
|
||||||
|
fileMetas = fileMetas.concat(cachedListing.items);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue