chore(studio): Implement auto retry logic for renaming folders in Storage Explorer (#43086)

## 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)
<img width="362" height="98" alt="image"
src="https://github.com/user-attachments/assets/52ba685a-2f35-42f1-8fcf-98351184fd56"
/>
- 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
This commit is contained in:
Joshen Lim authored and GitHub committed 2026-02-26 15:31:18 +08:00
1 parent 8d811b9837
commit ff4f58d725
2 files changed
+75 -36

No files matched your search

+72 -35
View File
@@ -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<void>(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<void>((resolve) => {
let seconds = retrySeconds
const interval = setInterval(() => {
toast(
<SonnerProgress
progress={Math.min(progress * 100, 100)}
message={`Renaming folder to ${newName}`}
description={`${isRateLimited ? 'API rate limited' : 'Error moving file'} - retrying in ${seconds} seconds (${attempt}/3)`}
/>,
{ 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(
<SonnerProgress progress={progress * 100} message={`Renaming folder to ${newName}`} />,
{ id: toastId, closeButton: false, position: 'top-right' }
<SonnerProgress
progress={Math.min(progress * 100, 100)}
message={`Renaming folder to ${newName}`}
/>,
{ 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(
<div>
<p>
Renamed folder to {newName} with {failedFiles} error{failedFiles > 1 ? 's' : ''}
</p>
<p className="text-foreground-light">
You may try again to rename the folder {originalName} to {newName}
</p>
</div>,
{
id: toastId,
closeButton: true,
duration: Infinity,
}
)
}
if (state.openedFolders[columnIndex]?.name === folder.name) {
@@ -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
}) => (
<div className="flex gap-3 w-full">
<Loader2 className="animate-spin text-foreground-muted mt-0.5" size={16} />
@@ -36,7 +38,7 @@ export const SonnerProgress = ({
</div>
<Progress value={progress} className="w-full" />
<div className="flex flex-row gap-2 items-center justify-between">
<small className="text-foreground-lighter text-xs">Please do not close the browser</small>
<small className="text-foreground-lighter text-xs">{description}</small>
{action}
</div>
</div>