From ff4f58d725ce32ff0df505c375ef5c75dc30a57e Mon Sep 17 00:00:00 2001 From: Joshen Lim Date: Thu, 26 Feb 2026 15:31:18 +0800 Subject: [PATCH] chore(studio): Implement auto retry logic for renaming folders in Storage Explorer (#43086) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Context Renaming folders on the Storage Explorer has a tendency to be flaky as we're using the `POST /objects/move` storage API to move files to the renamed folder which handles one file per request. For a big folder (e.g 100+ files), users will easily hit rate limits and run into 429s with the API, in which the current behaviour just shows toast errors as the error handler, leaving an odd state upon running through every file in the folder where some files are in the old folder, and others in the new one ## Changes involved Main solution is to implement an automatic retry logic for `renameFolder`, but changes involved are: - Remove batching logic for renaming folders (Which fires `n` requests per batch) - The batch logic was negligible anyways since our batch size was only 2 😅 - Instead we now handle moving of files sequentially - If a file runs into an error during the `POST` request, retry the request after `n` seconds - If the error was a 429, retry after 60 seconds, otherwise just 5 seconds - Will attempt the request for a total of 3 times - Toast progress description will also be updated to show the message (timer will also count down to keep the UI interactive and not look stuck) image - After the whole process is completed, if any file ran into an error, toast description will add a CTA to retry the rename ## Demo https://github.com/user-attachments/assets/d65b3c22-f9f8-4984-9521-6fc83a69f82e ## To test Have a folder in your project's storage bucket with 100+ files (can stress test with nested folders) - [ ] Verify that you can rename the folder, and the dashboard automatically handles errors --- apps/studio/state/storage-explorer.tsx | 107 ++++++++++++------ .../components/shadcn/ui/sonner-progress.tsx | 4 +- 2 files changed, 75 insertions(+), 36 deletions(-) diff --git a/apps/studio/state/storage-explorer.tsx b/apps/studio/state/storage-explorer.tsx index 4e98579275d..fa1a9590e69 100644 --- a/apps/studio/state/storage-explorer.tsx +++ b/apps/studio/state/storage-explorer.tsx @@ -62,7 +62,8 @@ type UploadProgress = { const LIMIT = 200 const OFFSET = 0 -const BATCH_SIZE = 2 +const DEFAULT_RETRY_SECONDS = 5 +const RATE_LIMIT_RETRY_SECONDS = 60 const DEFAULT_PREFERENCES = { view: STORAGE_VIEWS.COLUMNS, @@ -660,10 +661,10 @@ function createStorageExplorerState({ const files = await state.getAllItemsAlongFolder(folder) let progress = 0 - let hasErrors = false + let failedFiles = 0 + let retrySeconds = DEFAULT_RETRY_SECONDS - // Make this batched promises into a reusable function for storage, i think this will be super helpful - const promises = files.map((file) => { + for (const file of files) { const fromPath = `${file.prefix}/${file.name}` const pathSegments = fromPath.split('/') const toPath = pathSegments @@ -671,50 +672,86 @@ function createStorageExplorerState({ .concat(newName) .concat(pathSegments.slice(columnIndex + 1)) .join('/') - return () => { - return new Promise(async (resolve) => { - progress = progress + 1 / files.length - try { - await moveStorageObject({ - projectRef: state.projectRef, - bucketId: state.selectedBucket.id, - from: fromPath, - to: toPath, + + let success = false + let isRateLimited = false + + for (let attempt = 0; attempt < 3 && !success; attempt++) { + try { + if (attempt > 0) { + await new Promise((resolve) => { + let seconds = retrySeconds + const interval = setInterval(() => { + toast( + , + { id: toastId, closeButton: false, position: 'top-right', duration: Infinity } + ) + + seconds-- + if (seconds <= 0) { + clearInterval(interval) + resolve() + } + }, 1000) }) - } catch (error) { - hasErrors = true - toast.error(`Failed to move ${fromPath} to the new folder`) } - resolve() - }) + + await moveStorageObject({ + projectRef: state.projectRef, + bucketId: state.selectedBucket.id, + from: fromPath, + to: toPath, + }) + success = true + } catch (error) { + if ((error as ResponseError).code === 429) { + isRateLimited = true + retrySeconds = RATE_LIMIT_RETRY_SECONDS + } else { + isRateLimited = false + retrySeconds = DEFAULT_RETRY_SECONDS + } + + if (attempt === 2) failedFiles += 1 + } } - }) - const batchedPromises = chunk(promises, BATCH_SIZE) - // [Joshen] I realised this can be simplified with just a vanilla for loop, no need for reduce - // Just take note, but if it's working fine, then it's okay - - await batchedPromises.reduce(async (previousPromise, nextBatch) => { - await previousPromise - await Promise.all(nextBatch.map((batch) => batch())) + progress += 1 / files.length toast( - , - { id: toastId, closeButton: false, position: 'top-right' } + , + { id: toastId, closeButton: false, position: 'top-right', duration: Infinity } ) - }, Promise.resolve()) + } - if (!hasErrors) { + if (failedFiles === 0) { toast.success(`Successfully renamed folder to ${newName}`, { id: toastId, closeButton: true, duration: SONNER_DEFAULT_DURATION, }) } else { - toast.error(`Renamed folder to ${newName} with some errors`, { - id: toastId, - closeButton: true, - duration: SONNER_DEFAULT_DURATION, - }) + toast.error( +
+

+ Renamed folder to {newName} with {failedFiles} error{failedFiles > 1 ? 's' : ''} +

+

+ You may try again to rename the folder {originalName} to {newName} +

+
, + { + id: toastId, + closeButton: true, + duration: Infinity, + } + ) } if (state.openedFolders[columnIndex]?.name === folder.name) { diff --git a/packages/ui/src/components/shadcn/ui/sonner-progress.tsx b/packages/ui/src/components/shadcn/ui/sonner-progress.tsx index d349947ac03..643d06f92f9 100644 --- a/packages/ui/src/components/shadcn/ui/sonner-progress.tsx +++ b/packages/ui/src/components/shadcn/ui/sonner-progress.tsx @@ -18,11 +18,13 @@ export const SonnerProgress = ({ progressPrefix, action, message, + description = 'Please do not close the browser', }: { progress: number progressPrefix?: string action?: React.ReactNode message: string + description?: string }) => (
@@ -36,7 +38,7 @@ export const SonnerProgress = ({
- Please do not close the browser + {description} {action}