Skip to content

use refetch backoff logic from convex-js #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions src/react/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ import type {
} from "./index.js";
import isNetworkError from "is-network-error";

// Retry after this much time, based on the retry number.
const RETRY_BACKOFF = [500, 2000]; // In ms
const RETRY_JITTER = 100; // In ms

export const ConvexAuthActionsContext =
createContext<ConvexAuthActionsContextType>(undefined as any);

Expand Down Expand Up @@ -174,13 +170,19 @@ export function AuthProvider({
async (
args: { code: string; verifier?: string } | { refreshToken: string },
) => {
let lastError;
// Retry the call if it fails due to a network error.
// This is especially common in mobile apps where an app is backgrounded
// while making a call and hits a network error, but will succeed with a
// retry once the app is brought to the foreground.
let retry = 0;
while (retry < RETRY_BACKOFF.length) {
const initialBackoff = 100;
const maxBackoff = 1000;
let retries = 0;

const nextBackoff = () => {
const baseBackoff = initialBackoff * Math.pow(2, retries);
retries += 1;
const actualBackoff = Math.min(baseBackoff, maxBackoff);
const jitter = actualBackoff * (Math.random() - 0.5);
return actualBackoff + jitter;
};

const fetchTokens = async () => {
try {
return await client.unauthenticatedCall(
"auth:signIn" as unknown as SignInAction,
Expand All @@ -189,19 +191,19 @@ export function AuthProvider({
: args,
);
} catch (e) {
lastError = e;
if (!isNetworkError(e)) {
break;
if (!isNetworkError(e) || retries > 10) {
throw e;
}
const wait = RETRY_BACKOFF[retry] + RETRY_JITTER * Math.random();
retry++;
const backoff = nextBackoff();
logVerbose(
`verifyCode failed with network error, retry ${retry} of ${RETRY_BACKOFF.length} in ${wait}ms`,
`verifyCode failed with network error, attempting retrying in ${backoff}ms`,
);
await new Promise((resolve) => setTimeout(resolve, wait));
await new Promise((resolve) => setTimeout(resolve, backoff));
return fetchTokens();
}
}
throw lastError;
};

return fetchTokens();
},
[client],
);
Expand Down