Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Bound contacts warm-up retries

Neeraj Gupta89ba3aa95196
diff --git a/web/packages/contacts/index.test.ts b/web/packages/contacts/index.test.ts
index 65caf6584c..a2787c8c92 100644
--- a/web/packages/contacts/index.test.ts
+++ b/web/packages/contacts/index.test.ts
@@ -304,7 +304,7 @@ describe("profile picture loading", () => {
});
describe("retry after warm-up failure", () => {
- test("retries with the last ready input after a transient failure", async () => {
+ test("recovers from a transient failure with bounded background retry", async () => {
vi.useFakeTimers();
const { contacts, get_diff } = await setupContactsModule();
get_diff.mockReset();
@@ -323,15 +323,37 @@ describe("retry after warm-up failure", () => {
])
.mockResolvedValueOnce([]);
- await expect(
+ const ready = contacts.ensureContactsReady({
+ userID: 101,
+ masterKeyB64: "ignored",
+ });
+ await vi.advanceTimersByTimeAsync(10_001);
+ await expect(ready).resolves.toBeUndefined();
+
+ expect(get_diff).toHaveBeenCalledTimes(3);
+ });
+
+ test("stops after bounded background retries keep failing", async () => {
+ vi.useFakeTimers();
+ const { contacts, get_diff } = await setupContactsModule();
+ get_diff.mockReset();
+ get_diff.mockRejectedValue(new Error("down"));
+
+ const ready = expect(
contacts.ensureContactsReady({
userID: 101,
masterKeyB64: "ignored",
}),
- ).rejects.toThrow("transient");
- await vi.advanceTimersByTimeAsync(5_001);
+ ).rejects.toThrow("down");
- expect(get_diff).toHaveBeenCalledTimes(3);
+ await vi.advanceTimersByTimeAsync(10_001);
+ await vi.advanceTimersByTimeAsync(30_001);
+ await vi.advanceTimersByTimeAsync(120_001);
+ await ready;
+
+ expect(get_diff).toHaveBeenCalledTimes(4);
+ await vi.advanceTimersByTimeAsync(300_000);
+ expect(get_diff).toHaveBeenCalledTimes(4);
});
});
diff --git a/web/packages/contacts/index.ts b/web/packages/contacts/index.ts
index 957653a460..1db768927d 100644
--- a/web/packages/contacts/index.ts
+++ b/web/packages/contacts/index.ts
@@ -4,6 +4,7 @@ import { masterKeyFromSession } from "ente-accounts-rs/services/session-storage"
import { ensureLocalUser } from "ente-accounts-rs/services/user";
import { clientPackageName, desktopAppVersion, isDesktop } from "ente-base/app";
import { ensureArrayBufferBacked } from "ente-base/bytes";
+import { retryAsyncOperation } from "ente-base/http";
import log from "ente-base/log";
import { apiOrigin } from "ente-base/origins";
import { savedAuthToken } from "ente-base/token";
@@ -53,7 +54,6 @@ export type {
const CONTACT_DIFF_LIMIT = 500;
const AVATAR_FAILURE_TTL_MS = 60_000;
-const READY_RETRY_COOLDOWN_MS = 5_000;
const CONTACTS_CACHE_SCHEMA_VERSION = 2;
interface RemoteContactRecord {
@@ -123,8 +123,6 @@ interface ContactsState {
avatarLoadsByContactID: Map<string, Promise<void>>;
avatarFailureUntilByContactID: Map<string, number>;
avatarListenersByContactID: Map<string, Set<() => void>>;
- lastReadyInput: ContactsReadyInput | undefined;
- retryTimer: ReturnType<typeof setTimeout> | undefined;
}
const emptySnapshot = (): ContactsDisplaySnapshot => ({
@@ -149,8 +147,6 @@ const state: ContactsState = {
avatarLoadsByContactID: new Map(),
avatarFailureUntilByContactID: new Map(),
avatarListenersByContactID: new Map(),
- lastReadyInput: undefined,
- retryTimer: undefined,
};
const buildSessionKey = (baseURL: string, userID: number) =>
@@ -196,10 +192,6 @@ const clearInMemoryState = () => {
state.avatarURLByContactID = new Map();
state.avatarLoadsByContactID = new Map();
state.avatarFailureUntilByContactID = new Map();
- if (state.retryTimer) {
- clearTimeout(state.retryTimer);
- state.retryTimer = undefined;
- }
};
const emitSnapshot = (isHydrated = true) => {
@@ -479,7 +471,6 @@ export const ensureContactsReady = async ({
userID,
masterKeyB64,
}: ContactsReadyInput) => {
- state.lastReadyInput = { userID, masterKeyB64 };
const authToken = await savedAuthToken();
if (!authToken) {
state.sessionGeneration += 1;
@@ -500,37 +491,21 @@ export const ensureContactsReady = async ({
return state.readyPromise;
}
- const readyPromise = syncContacts({
- sessionKey,
- baseURL,
- authToken,
- userID,
- masterKeyB64,
- })
- .then(() => {
- if (state.retryTimer) {
- clearTimeout(state.retryTimer);
- state.retryTimer = undefined;
- }
- })
- .catch((error: unknown) => {
- if (state.retryTimer) {
- clearTimeout(state.retryTimer);
- }
- const retryInput = state.lastReadyInput;
- state.retryTimer = setTimeout(() => {
- state.retryTimer = undefined;
- if (retryInput) {
- void ensureContactsReady(retryInput).catch(() => undefined);
- }
- }, READY_RETRY_COOLDOWN_MS);
- throw error;
- })
- .finally(() => {
- if (state.readyPromise === readyPromise) {
- state.readyPromise = undefined;
- }
- });
+ const readyPromise = retryAsyncOperation(
+ () =>
+ syncContacts({
+ sessionKey,
+ baseURL,
+ authToken,
+ userID,
+ masterKeyB64,
+ }),
+ { retryProfile: "background" },
+ ).finally(() => {
+ if (state.readyPromise === readyPromise) {
+ state.readyPromise = undefined;
+ }
+ });
state.readyPromise = readyPromise;