Skip to content
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

feat: Exponential Backoff on sync #101

Merged
merged 1 commit into from
Jun 20, 2024
Merged
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
9 changes: 8 additions & 1 deletion utils/xmtpRN/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export const getXmtpClient = async (
throw new Error(`[XmtpRN] No client found for ${account}`);
};

const INITIAL_BACKOFF = 1000; // Initial backoff interval in ms
const MAX_BACKOFF = 60000; // Maximum backoff interval in ms
let currentBackoff = INITIAL_BACKOFF;

const onSyncLost = async (account: string, error: any) => {
console.log(
`[XmtpRN] An error occured while syncing for ${account}: ${error}`
Expand All @@ -71,7 +75,9 @@ const onSyncLost = async (account: string, error: any) => {
// If there is an error let's show it
getChatStore(account).getState().setReconnecting(true);
// Wait a bit before reco
await new Promise((r) => setTimeout(r, 1000));
console.log(`[XmtpRN] Reconnecting in ${currentBackoff}ms`);
await new Promise((r) => setTimeout(r, currentBackoff));
currentBackoff = Math.min(currentBackoff * 2, MAX_BACKOFF);
// Now let's reload !
syncXmtpClient(account);
};
Expand Down Expand Up @@ -161,6 +167,7 @@ export const syncXmtpClient = async (account: string) => {
...groupTopicsToQuery,
]);
}
currentBackoff = INITIAL_BACKOFF;
console.log(`[XmtpRN] Finished syncing ${account}`);
} catch (e) {
console.log("main sync error");
Expand Down
Loading