Skip to content

Commit

Permalink
Merge pull request #38 from dmccartney/daniel-parallel-reward-fetch
Browse files Browse the repository at this point in the history
feat: fetch hosted/ipfs reward trees in parallel for perf
  • Loading branch information
dmccartney authored Jan 19, 2024
2 parents b0de697 + 9f010ab commit baff82b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
23 changes: 11 additions & 12 deletions web/src/hooks/useFetchJSONZST.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ export async function fetchJSONZST(url) {

export default function useFetchJSONZST(urls, options) {
return useQueries(
urls.map(({ url, fallbackUrls }, i) => ({
queryKey: ["fetchJSONZST", url],
urls.map(({ sourceUrls }, i) => ({
queryKey: ["fetchJSONZST", sourceUrls[0]],
queryFn: async () => {
try {
return await fetchJSONZST(url);
return await Promise.any(
sourceUrls.map((url) =>
fetchJSONZST(url).catch((err) => {
console.log(`failed to fetch ${url}`, err);
throw err;
})
)
);
} catch (err) {
console.log(`error fetching ${url}`, err);
for (const fallbackUrl of fallbackUrls) {
console.log(`trying fallback ${fallbackUrl}`);
try {
return await fetchJSONZST(fallbackUrl);
} catch (err) {
console.log(`error fetching ${fallbackUrl}`, err);
}
}
console.log(`all sourceUrls failed`, sourceUrls, err);
throw err;
}
},
Expand Down
4 changes: 2 additions & 2 deletions web/src/hooks/useFetchRewardSnapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export default function useFetchRewardSnapshots({
let [ipfsBase] = useSetting("ipfs.base");
let snapshotJsons = useFetchJSONZST(
(snapshots || []).map(({ rewardIndex, merkleTreeCID }) => ({
url: `${ipfsBase}/ipfs/${merkleTreeCID}/rp-rewards-${network}-${rewardIndex}.json.zst`,
fallbackUrls: [
sourceUrls: [
`${ipfsBase}/ipfs/${merkleTreeCID}/rp-rewards-${network}-${rewardIndex}.json.zst`,
`${fallbackBase}/${network}/rp-rewards-${network}-${rewardIndex}.json`,
],
})),
Expand Down

0 comments on commit baff82b

Please sign in to comment.