From c650f7d19361f45bc622b65057aa3672bf87d09a Mon Sep 17 00:00:00 2001 From: fenn-cs Date: Wed, 8 May 2024 21:03:06 +0100 Subject: [PATCH] perf(deleteAction): Queue delete requests When multiple files are deleted at once, all the requests bombard the server simultaneously, causing performance issues. This commit adds queuing that limits the concurrency of these requests to 5 at a time. Signed-off-by: fenn-cs --- apps/files/src/actions/deleteAction.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/apps/files/src/actions/deleteAction.ts b/apps/files/src/actions/deleteAction.ts index 129ef0f987782..1faa77d136d9d 100644 --- a/apps/files/src/actions/deleteAction.ts +++ b/apps/files/src/actions/deleteAction.ts @@ -30,6 +30,7 @@ import NetworkOffSvg from '@mdi/svg/svg/network-off.svg?raw' import TrashCanSvg from '@mdi/svg/svg/trash-can.svg?raw' import logger from '../logger.js' +import PQueue from 'p-queue' const canUnshareOnly = (nodes: Node[]) => { return nodes.every(node => node.attributes['is-mount-root'] === true @@ -119,6 +120,8 @@ const displayName = (nodes: Node[], view: View) => { return t('files', 'Delete') } +const queue = new PQueue({ concurrency: 5 }) + export const action = new FileAction({ id: 'delete', displayName, @@ -183,7 +186,19 @@ export const action = new FileAction({ return Promise.all(nodes.map(() => false)) } - return Promise.all(nodes.map(node => this.exec(node, view, dir))) + // Map each node to a promise that resolves with the result of exec(node) + const promises = nodes.map(node => { + // Create a promise that resolves with the result of exec(node) + const promise = new Promise(resolve => { + queue.add(async () => { + const result = await this.exec(node, view, dir) + resolve(result !== null ? result : false) + }) + }) + return promise + }) + + return Promise.all(promises) }, order: 100,