Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move retry to service

Manav Rathi057df349b70d
diff --git a/web/packages/base/http.ts b/web/packages/base/http.ts
index 6b43bb4e9a..394712d97a 100644
--- a/web/packages/base/http.ts
+++ b/web/packages/base/http.ts
@@ -202,6 +202,10 @@ export const retryAsyncOperation = async <T>(
}
};
+export type HTTPRequestRetrier = (
+ request: () => Promise<Response>,
+) => Promise<Response>;
+
/**
* A helper function to adapt {@link retryAsyncOperation} for HTTP fetches.
*
@@ -209,7 +213,9 @@ export const retryAsyncOperation = async <T>(
* matched by {@link ensureOk}) is also counted as an error when considering if
* a request should be retried.
*/
-export const retryEnsuringHTTPOk = (request: () => Promise<Response>) =>
+export const retryEnsuringHTTPOk: HTTPRequestRetrier = (
+ request: () => Promise<Response>,
+) =>
retryAsyncOperation(async () => {
const r = await request();
ensureOk(r);
@@ -223,7 +229,9 @@ export const retryEnsuringHTTPOk = (request: () => Promise<Response>) =>
* This is similar to {@link retryEnsuringHTTPOk}, except it stops retrying if
* remote responds with a 4xx HTTP status.
*/
-export const retryEnsuringHTTPOkOr4xx = (request: () => Promise<Response>) =>
+export const retryEnsuringHTTPOkOr4xx: HTTPRequestRetrier = (
+ request: () => Promise<Response>,
+) =>
retryAsyncOperation(
async () => {
const r = await request();
diff --git a/web/packages/gallery/services/upload/remote.ts b/web/packages/gallery/services/upload/remote.ts
index 935bfab9fe..768494a691 100644
--- a/web/packages/gallery/services/upload/remote.ts
+++ b/web/packages/gallery/services/upload/remote.ts
@@ -20,38 +20,6 @@ import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod";
import type { MultipartUploadURLs, UploadFile } from "./upload-service";
-/**
- * A variant of {@link retryEnsuringHTTPOk} that understands the cancellation
- * mechanism used by the upload subsystem.
- *
- * @param abortIfCancelled A function that aborts the operation by throwing a
- * error with the message set to {@link CustomError.UPLOAD_CANCELLED} if the
- * user has cancelled the upload.
- *
- * @param request A function that returns a new promise for a request that we
- * want to retry.
- *
- * This function will retry requests (obtained afresh each time by calling
- * {@link request}) in the same manner as {@link retryEnsuringHTTPOk}.
- * Additionally, it will call {@link abortIfCancelled} before each attempt, and
- * also bypass the retries when the abort happens on such cancellations.
- */
-export const retryEnsuringHTTPOkUnlessCanclelled = (
- abortIfCancelled: () => void,
- request: () => Promise<Response>,
-) =>
- retryAsyncOperation(
- async () => {
- const r = await request();
- ensureOk(r);
- return r;
- },
- (e) => {
- if (e instanceof Error && e.message == CustomError.UPLOAD_CANCELLED)
- throw e;
- },
- );
-
/**
* A pre-signed URL alongwith the associated object key that is later used to
* refer to file contents (the "object") that were uploaded to this URL.
@@ -452,13 +420,11 @@ export const completeMultipartUpload = (
completionURL: string,
completedParts: MultipartCompletedPart[],
) =>
- retryEnsuringHTTPOk(() =>
- fetch(completionURL, {
- method: "POST",
- headers: { ...publicRequestHeaders(), "Content-Type": "text/xml" },
- body: createMultipartUploadRequestBody(completedParts),
- }),
- );
+ fetch(completionURL, {
+ method: "POST",
+ headers: { ...publicRequestHeaders(), "Content-Type": "text/xml" },
+ body: createMultipartUploadRequestBody(completedParts),
+ });
/**
* Variant of {@link completeMultipartUpload} that uses the CF worker.
@@ -466,20 +432,16 @@ export const completeMultipartUpload = (
export const completeMultipartUploadViaWorker = async (
completionURL: string,
completedParts: MultipartCompletedPart[],
-) => {
- const origin = await uploaderOrigin();
- return retryEnsuringHTTPOk(() =>
- fetch(`${origin}/multipart-complete`, {
- method: "POST",
- headers: {
- ...publicRequestHeaders(),
- "Content-Type": "text/xml",
- "UPLOAD-URL": completionURL,
- },
- body: createMultipartUploadRequestBody(completedParts),
- }),
- );
-};
+) =>
+ fetch(`${await uploaderOrigin()}/multipart-complete`, {
+ method: "POST",
+ headers: {
+ ...publicRequestHeaders(),
+ "Content-Type": "text/xml",
+ "UPLOAD-URL": completionURL,
+ },
+ body: createMultipartUploadRequestBody(completedParts),
+ });
/**
* Lowest layer for file upload related HTTP operations when we're running in
diff --git a/web/packages/gallery/services/upload/upload-service.ts b/web/packages/gallery/services/upload/upload-service.ts
index 8be36b6299..a528626750 100644
--- a/web/packages/gallery/services/upload/upload-service.ts
+++ b/web/packages/gallery/services/upload/upload-service.ts
@@ -6,7 +6,12 @@ import type { BytesOrB64 } from "ente-base/crypto/types";
import { type CryptoWorker } from "ente-base/crypto/worker";
import { ensureElectron } from "ente-base/electron";
import { basename, nameAndExtension } from "ente-base/file-name";
-import type { PublicAlbumsCredentials } from "ente-base/http";
+import {
+ ensureOk,
+ retryAsyncOperation,
+ type HTTPRequestRetrier,
+ type PublicAlbumsCredentials,
+} from "ente-base/http";
import log from "ente-base/log";
import { extractExif } from "ente-gallery/services/exif";
import {
@@ -1543,6 +1548,41 @@ const uploadToBucket = async (
}
};
+/**
+ * A factory method that returns a function which will act like variant of
+ * {@link retryEnsuringHTTPOk} and also understands the cancellation mechanism
+ * used by the upload subsystem.
+ *
+ * @param abortIfCancelled A function that aborts the operation by throwing a
+ * error with the message set to {@link CustomError.UPLOAD_CANCELLED} if the
+ * user has cancelled the upload.
+ *
+ * @return A function of type {@link HTTPRequestRetrier} that can be used to
+ * retry requests. This function will retry requests (obtained afresh each time
+ * by calling the provided {@link request} function) in the same manner as
+ * {@link retryEnsuringHTTPOk}. Additionally, it will call
+ * {@link abortIfCancelled} before each attempt, and also bypass the retries
+ * when the abort happens on such cancellations.
+ */
+const createAbortableRetryEnsuringHTTPOk =
+ (abortIfCancelled: () => void): HTTPRequestRetrier =>
+ (request: () => Promise<Response>) =>
+ retryAsyncOperation(
+ async () => {
+ abortIfCancelled();
+ const r = await request();
+ ensureOk(r);
+ return r;
+ },
+ (e) => {
+ if (
+ e instanceof Error &&
+ e.message == CustomError.UPLOAD_CANCELLED
+ )
+ throw e;
+ },
+ );
+
async function uploadStreamUsingMultipart(
fileLocalID: number,
dataStream: EncryptedFileStream,
@@ -1550,6 +1590,9 @@ async function uploadStreamUsingMultipart(
isCFUploadProxyDisabled: boolean,
abortIfCancelled: () => void,
) {
+ const abortableRetryEnsuringHTTPOk =
+ createAbortableRetryEnsuringHTTPOk(abortIfCancelled);
+
const uploadPartCount = Math.ceil(
dataStream.chunkCount / multipartChunksPerPart,
);
@@ -1604,9 +1647,13 @@ async function uploadStreamUsingMultipart(
const completionURL = multipartUploadURLs.completeURL;
if (!isCFUploadProxyDisabled) {
- await completeMultipartUploadViaWorker(completionURL, completedParts);
+ await abortableRetryEnsuringHTTPOk(() =>
+ completeMultipartUploadViaWorker(completionURL, completedParts),
+ );
} else {
- await completeMultipartUpload(completionURL, completedParts);
+ await abortableRetryEnsuringHTTPOk(() =>
+ completeMultipartUpload(completionURL, completedParts),
+ );
}
return { objectKey: multipartUploadURLs.objectKey, fileSize };