Skip to content

Commit

Permalink
Fix infinite rapid polling in useKeyedCache
Browse files Browse the repository at this point in the history
See the comment for an explanation as to why this is necessary.
Fixes
https://sentry.galaxyproject.org/share/issue/e68b539766464e76a627c80333430c82/,
which caused 250K events in less than 2 days.

To reproduce try editing a collection you don't own via
`<galaxy_url>/collection/<hdca_id>/edit`.
  • Loading branch information
mvdbeek committed Sep 1, 2024
1 parent cd2f1f8 commit ca81c91
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions client/src/composables/keyedCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MaybeRefOrGetter, toValue } from "@vueuse/core";
import { MaybeRefOrGetter, refWithControl, toValue } from "@vueuse/core";
import { computed, del, type Ref, ref, set, unref } from "vue";

import type { ApiResponse } from "@/api/schema";
Expand Down Expand Up @@ -44,7 +44,7 @@ export function useKeyedCache<T>(
shouldFetchHandler?: MaybeRefOrGetter<ShouldFetchHandler<T>>
) {
const storedItems = ref<{ [key: string]: T }>({});
const loadingItem = ref<{ [key: string]: boolean }>({});
const loadingItem = refWithControl<{ [key: string]: boolean }>({});

const getItemById = computed(() => {
return (id: string) => {
Expand All @@ -71,7 +71,14 @@ export function useKeyedCache<T>(

async function fetchItemById(params: FetchParams) {
const itemId = params.id;
const isAlreadyLoading = loadingItem.value[itemId] ?? false;
/*
* we use .peek() (https://vueuse.org/shared/refWithControl/)
* so we don't retrigger dependency changes when we modify loadingItem in the finally handler.
* If we would re-evaluate fetchItemById when loadingItem changes
* we would not stop calling the function until we succeed in fetching the value,
* which might be never in case of 400 / 403 / 404 responses.
*/
const isAlreadyLoading = loadingItem.peek()[itemId] ?? false;
if (isAlreadyLoading) {
return;
}
Expand Down

0 comments on commit ca81c91

Please sign in to comment.