improve direct upload stall feedback

This commit is contained in:
134 2026-07-08 21:28:43 +08:00
parent 9575873efb
commit b49519ec56

View File

@ -10,6 +10,7 @@ const CLIENT_UPLOAD_ID_FIELD = "clientUploadId";
const DIRECT_UPLOAD_INITIATE_TIMEOUT_MS = 3 * 1000; const DIRECT_UPLOAD_INITIATE_TIMEOUT_MS = 3 * 1000;
const DIRECT_UPLOAD_ACTION_TIMEOUT_MS = 15 * 1000; const DIRECT_UPLOAD_ACTION_TIMEOUT_MS = 15 * 1000;
const DIRECT_UPLOAD_ABORT_TIMEOUT_MS = 8 * 1000; const DIRECT_UPLOAD_ABORT_TIMEOUT_MS = 8 * 1000;
const DIRECT_UPLOAD_STALL_TIMEOUT_MS = 10 * 1000;
const XHR_UPLOAD_TOTAL_TIMEOUT_MS = 6 * 60 * 1000; const XHR_UPLOAD_TOTAL_TIMEOUT_MS = 6 * 60 * 1000;
const XHR_UPLOAD_STALL_TIMEOUT_MS = 30 * 1000; const XHR_UPLOAD_STALL_TIMEOUT_MS = 30 * 1000;
@ -336,6 +337,7 @@ async function fetchWithControlTimeout(
const controller = new AbortController(); const controller = new AbortController();
let timeout: ReturnType<typeof setTimeout> | null = null; let timeout: ReturnType<typeof setTimeout> | null = null;
let timedOut = false; let timedOut = false;
let settled = false;
const onAbort = () => { const onAbort = () => {
controller.abort(); controller.abort();
@ -344,30 +346,44 @@ async function fetchWithControlTimeout(
signal?.addEventListener("abort", onAbort, { once: true }); signal?.addEventListener("abort", onAbort, { once: true });
try { try {
timeout = setTimeout(() => { const request = fetch(input, {
timedOut = true;
controller.abort();
}, timeoutMs);
return await fetch(input, {
...init, ...init,
signal: controller.signal, signal: controller.signal,
}).catch((error) => {
if (signal?.aborted) {
throw createAbortError();
}
if (timedOut) {
throw createTimeoutError(
`Direct upload control request timed out after ${Math.round(
timeoutMs / 1000
)}s`
);
}
throw error;
}); });
} catch (error) {
if (signal?.aborted) {
throw createAbortError();
}
if (timedOut) { const timeoutPromise = new Promise<never>((_, reject) => {
throw createTimeoutError( timeout = setTimeout(() => {
`Direct upload control request timed out after ${Math.round( timedOut = true;
timeoutMs / 1000 controller.abort();
)}s` if (!settled) {
); reject(
} createTimeoutError(
`Direct upload control request timed out after ${Math.round(
timeoutMs / 1000
)}s`
)
);
}
}, timeoutMs);
});
throw error; return await Promise.race([request, timeoutPromise]);
} finally { } finally {
settled = true;
if (timeout) { if (timeout) {
clearTimeout(timeout); clearTimeout(timeout);
} }
@ -547,7 +563,8 @@ async function fetchDirectPartWithRetry(
}, },
maxRetries: number, maxRetries: number,
signal?: AbortSignal, signal?: AbortSignal,
onProgress?: (loaded: number, total: number) => void onProgress?: (loaded: number, total: number) => void,
options?: { stallTimeoutMs?: number }
) { ) {
for (let attempt = 0; ; attempt += 1) { for (let attempt = 0; ; attempt += 1) {
try { try {
@ -557,6 +574,7 @@ async function fetchDirectPartWithRetry(
{ {
onProgress, onProgress,
signal, signal,
stallTimeoutMs: options?.stallTimeoutMs,
} }
); );
@ -667,7 +685,8 @@ async function uploadMultipartParts(params: {
params.signal, params.signal,
(loaded, total) => { (loaded, total) => {
reportPartProgress(part.partNumber, loaded, total || body.size); reportPartProgress(part.partNumber, loaded, total || body.size);
} },
{ stallTimeoutMs: DIRECT_UPLOAD_STALL_TIMEOUT_MS }
); );
if (!response.ok) { if (!response.ok) {
@ -731,6 +750,9 @@ async function fetchDirectUpload(
); );
if (upload.mode === "single") { if (upload.mode === "single") {
emitUploadProgress(onProgress, 0, file.blob.size, "direct", {
percent: 2,
});
const response = await fetchDirectPartWithRetry( const response = await fetchDirectPartWithRetry(
upload.url, upload.url,
{ {
@ -744,7 +766,8 @@ async function fetchDirectUpload(
signal, signal,
(loaded, total) => { (loaded, total) => {
emitUploadProgress(onProgress, loaded, total || file.blob.size, "direct"); emitUploadProgress(onProgress, loaded, total || file.blob.size, "direct");
} },
{ stallTimeoutMs: DIRECT_UPLOAD_STALL_TIMEOUT_MS }
); );
if (!response.ok) { if (!response.ok) {
@ -774,6 +797,9 @@ async function fetchDirectUpload(
} }
multipartUpload = upload; multipartUpload = upload;
emitUploadProgress(onProgress, 0, file.blob.size, "multipart", {
percent: 2,
});
const parts = await uploadMultipartParts({ const parts = await uploadMultipartParts({
file, file,
upload, upload,