Skip to content

armgit / ente

Public Imported from GitHub
0

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

retry enhancements

Manav Rathid17296216cbf
diff --git a/desktop/src/main/services/ffmpeg-worker.ts b/desktop/src/main/services/ffmpeg-worker.ts
index ed5ad9e144..4d6e018dde 100644
--- a/desktop/src/main/services/ffmpeg-worker.ts
+++ b/desktop/src/main/services/ffmpeg-worker.ts
@@ -787,7 +787,7 @@ const pseudoFFProbeVideo = async (inputFilePath: string) => {
* Upload the file at the given {@link videoFilePath} to the provided presigned
* URL(s) using a HTTP PUT request.
*
- * In case on non-HTTP-4xx errors, retry up to 3 times with exponential backoff.
+ * All HTTP requests are retried up to 3 times with exponential backoff.
*
* See: [Note: Upload HLS video segment from node side].
*
@@ -799,25 +799,6 @@ const pseudoFFProbeVideo = async (inputFilePath: string) => {
* @param uploadURLs A pre-signed URL(s) to upload the file. For singular
* uploads this'll be a singleton array, otherwise will contain multipart URLs
* and completion URL.
- *
- * ---
- *
- * This is an inlined but bespoke reimplementation of `retryEnsuringHTTPOkOr4xx`
- * from `web/packages/base/http.ts`
- *
- * - We don't have the rest of the scaffolding used by that function, which is
- * why it is intially inlined bespoked.
- *
- * - It handles the specific use case of uploading videos since generating the
- * HLS stream is a fairly expensive operation, so a retry to discount
- * transient network issues is called for. There are only 2 retries for a
- * total of 3 attempts, and the retry gaps are more spaced out.
- *
- * - Later it was discovered that net.fetch is much slower than node's native
- * fetch, so this implementation has further diverged.
- *
- * - This also moved to a utility process, where we also have a more restricted
- * ability to import electron API.
*/
const uploadVideoSegments = async (
videoFilePath: string,
@@ -874,6 +855,9 @@ const uploadVideoSegmentsSingle = (
);
/**
+ * Retry a async operation on failure up to 3 times (1 original + 2 retries)
+ * with exponential backoff.
+ *
* This is an inlined but bespoke reimplementation of `retryEnsuringHTTPOk`
* from `web/packages/base/http.ts`
*
@@ -892,7 +876,7 @@ const uploadVideoSegmentsSingle = (
* ability to import electron API.
*/
const retryEnsuringHTTPOk = async (request: () => Promise<Response>) => {
- const waitTimeBeforeNextTry = [5000, 20000];
+ const waitTimeBeforeNextTry = [10000, 30000];
while (true) {
try {
diff --git a/web/packages/base/http.ts b/web/packages/base/http.ts
index e21b6f97c6..6b61c4811e 100644
--- a/web/packages/base/http.ts
+++ b/web/packages/base/http.ts
@@ -168,16 +168,36 @@ export const isMuseumHTTPError = async (
return false;
};
+interface RetryAsyncOperationOpts {
+ /**
+ * An optional modification to the default wait times between retries.
+ *
+ * - default 1 orig + 3 retries (2s, 5s, 10s)
+ * - "background" 1 orig + 3 retries (5s, 25s, 120s)
+ *
+ * default is fine for most operations, including interactive ones where we
+ * don't want to wait too long before giving up. "background" is suitable
+ * for non-interactive operations where we can wait for longer (thus better
+ * handle remote hiccups) without degrading the user's experience.
+ */
+ retryProfile?: "background";
+ /**
+ * An optional function that is called with the corresponding error whenever
+ * {@link op} rejects. It should throw the error if the retries should
+ * immediately be aborted.
+ */
+ abortIfNeeded?: (error: unknown) => void;
+}
+
/**
- * Retry a async operation like a HTTP request 3 (+ 1 original) times with
- * exponential backoff.
+ * Retry a async operation on failure up to 4 times (1 original + 3 retries)
+ * with exponential backoff.
*
* @param op A function that performs the operation, returning the promise for
* its completion.
*
- * @param abortIfNeeded An optional function that is called with the
- * corresponding error whenever {@link op} rejects. It should throw the error if
- * the retries should immediately be aborted.
+ * @param opts Optional tweaks to the default implementation. See
+ * {@link RetryAsyncOperationOpts}.
*
* @returns A promise that fulfills with to the result of a first successfully
* fulfilled promise of the 4 (1 + 3) attempts, or rejects with the error
@@ -186,9 +206,13 @@ export const isMuseumHTTPError = async (
*/
export const retryAsyncOperation = async <T>(
op: () => Promise<T>,
- abortIfNeeded?: (error: unknown) => void,
+ opts?: RetryAsyncOperationOpts,
): Promise<T> => {
- const waitTimeBeforeNextTry = [2000, 5000, 10000];
+ const { retryProfile, abortIfNeeded } = opts ?? {};
+ const waitTimeBeforeNextTry =
+ retryProfile == "background"
+ ? [5000, 25000, 120000]
+ : [2000, 5000, 10000];
while (true) {
try {
@@ -219,9 +243,8 @@ export type HTTPRequestRetrier = (
/**
* A helper function to adapt {@link retryAsyncOperation} for HTTP fetches.
*
- * This will ensure that the HTTP operation returning a non-200 OK status (as
- * matched by {@link ensureOk}) is also counted as an error when considering if
- * a request should be retried.
+ * This extends {@link retryAsyncOperation} by treating any non-200 OK status
+ * (as matched by {@link ensureOk}) as an error that should be retried.
*/
export const retryEnsuringHTTPOk: HTTPRequestRetrier = (
request: () => Promise<Response>,
@@ -233,8 +256,8 @@ export const retryEnsuringHTTPOk: HTTPRequestRetrier = (
});
/**
- * A helper function to {@link retryAsyncOperation} for HTTP fetches, but
- * considering any 4xx HTTP responses as irrecoverable failures.
+ * A helper function to adapt {@link retryAsyncOperation} for HTTP fetches, but
+ * treating any 4xx HTTP responses as irrecoverable failures.
*
* This is similar to {@link retryEnsuringHTTPOk}, except it stops retrying if
* remote responds with a 4xx HTTP status.
@@ -248,7 +271,9 @@ export const retryEnsuringHTTPOkOr4xx: HTTPRequestRetrier = (
ensureOk(r);
return r;
},
- (e) => {
- if (isHTTP4xxError(e)) throw e;
+ {
+ abortIfNeeded(e) {
+ if (isHTTP4xxError(e)) throw e;
+ },
},
);
diff --git a/web/packages/gallery/services/upload/remote.ts b/web/packages/gallery/services/upload/remote.ts
index 247ecff8e9..31e3c61e27 100644
--- a/web/packages/gallery/services/upload/remote.ts
+++ b/web/packages/gallery/services/upload/remote.ts
@@ -430,7 +430,7 @@ export class PhotosUploadHTTPClient {
null,
headers,
),
- handleUploadError,
+ { abortIfNeeded: handleUploadError },
);
return response.data;
} catch (e) {
@@ -460,7 +460,7 @@ export class PublicAlbumsUploadHTTPClient {
null,
authenticatedPublicAlbumsRequestHeaders(credentials),
),
- handleUploadError,
+ { abortIfNeeded: handleUploadError },
);
return response.data;
} catch (e) {
diff --git a/web/packages/gallery/services/upload/upload-service.ts b/web/packages/gallery/services/upload/upload-service.ts
index a554468411..96adc7c48b 100644
--- a/web/packages/gallery/services/upload/upload-service.ts
+++ b/web/packages/gallery/services/upload/upload-service.ts
@@ -1590,12 +1590,14 @@ const createAbortableRetryEnsuringHTTPOk =
ensureOk(r);
return r;
},
- (e) => {
- if (
- e instanceof Error &&
- e.message == CustomError.UPLOAD_CANCELLED
- )
- throw e;
+ {
+ abortIfNeeded(e) {
+ if (
+ e instanceof Error &&
+ e.message == CustomError.UPLOAD_CANCELLED
+ )
+ throw e;
+ },
},
);
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index 341ab3db5c..b9babc971b 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -1069,8 +1069,9 @@ const processQueueItem = async ({
});
try {
- await retryAsyncOperation(() =>
- putVideoData(file, playlistData, objectID, videoSize),
+ await retryAsyncOperation(
+ () => putVideoData(file, playlistData, objectID, videoSize),
+ { retryProfile: "background" },
);
} catch (e) {
if (isHTTP4xxError(e)) await markFailedVideoFile(file);