Skip to content

Commit

Permalink
[dashboard] retry useUserQuery if error code is not 401 – WEB-560 (#1…
Browse files Browse the repository at this point in the history
…8033)

* [dashboard] retry useUserQuery if error code is not 401

* [dashboard] Add doRetryUserLoader feature flag

---------

Co-authored-by: Alex Tugarev <[email protected]>
  • Loading branch information
geropl and AlexTugarev authored Jun 23, 2023
1 parent ce63317 commit 41abfc1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions components/dashboard/src/data/featureflag-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const featureFlags = {
enableDedicatedOnboardingFlow: false,
usageDownload: false,
phoneVerificationByCall: false,
doRetryUserLoader: true,
};

export const useFeatureFlag = (featureFlag: keyof typeof featureFlags) => {
Expand Down
14 changes: 12 additions & 2 deletions components/dashboard/src/hooks/use-user-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import { trackLocation } from "../Analytics";
import { refreshSearchData } from "../components/RepositoryFinder";
import { useQuery } from "@tanstack/react-query";
import { noPersistence } from "../data/setup";
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
import { useFeatureFlag } from "../data/featureflag-query";

export const useUserLoader = () => {
const { user, setUser } = useContext(UserContext);
const doRetryUserLoader = useFeatureFlag("doRetryUserLoader");

// For now, we're using the user context to store the user, but letting react-query handle the loading
// In the future, we should remove the user context and use react-query to access the user
Expand All @@ -27,8 +30,15 @@ export const useUserLoader = () => {
// We'll let an ErrorBoundary catch the error
useErrorBoundary: true,
// It's important we don't retry as we want to show the login screen as quickly as possible if a 401
// TODO: In the future we can consider retrying for non 401 errors
retry: false,
retry: (_failureCount: number, error: Error & { code?: number }) => {
if (!doRetryUserLoader) {
return false;
}
return error.code !== ErrorCodes.NOT_AUTHENTICATED;
},
// docs: https://tanstack.com/query/v4/docs/react/guides/query-retries
// backoff by doubling, max. 10s
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 10000),
cacheTime: 1000 * 60 * 60 * 1, // 1 hour
staleTime: 1000 * 60 * 60 * 1, // 1 hour
onSuccess: (loadedUser) => {
Expand Down

0 comments on commit 41abfc1

Please sign in to comment.