Browse Source

tweak: add limit to deleteUser file select

I wasn't able to reprod any memory leak from a user having a large
number of files locally, but it's showing up in logs and maybe could add
up with lots of temp users getting auto-removed. It's worth a shot.
KernelDeimos 1 tháng trước cách đây
mục cha
commit
8ff2e93e66
1 tập tin đã thay đổi với 22 bổ sung18 xóa
  1. 22 18
      src/backend/src/helpers.js

+ 22 - 18
src/backend/src/helpers.js

@@ -1088,24 +1088,28 @@ async function deleteUser(user_id){
     /** @type BaseDatabaseAccessService */
     const db = services.get('database').get(DB_READ, 'filesystem');
 
-    // get a list of all files owned by this user
-    let files = await db.read(
-        `SELECT uuid, bucket, bucket_region FROM fsentries WHERE user_id = ? AND is_dir = 0`,
-        [user_id]
-    );
-
-    // delete all files from S3
-    if(files !== null && files.length > 0){
-        for(let i=0; i<files.length; i++){
-            // init S3 SDK
-            const svc_fs = Context.get('services').get('filesystem');
-            const svc_mountpoint =
-                Context.get('services').get('mountpoint');
-            const storage = svc_mountpoint.get_storage();
-            const op_delete = storage.create_delete();
-            await op_delete.run({
-                node: await svc_fs.node(new NodeUIDSelector(files[i].uuid))
-            });
+    // get a list of up to 5000 files owned by this user
+    for ( let offset=0; true; offset += 5000 ) {
+        let files = await db.read(
+            `SELECT uuid, bucket, bucket_region FROM fsentries WHERE user_id = ? AND is_dir = 0 LIMIT 5000 OFFSET `+offset,
+            [user_id]
+        );
+        
+        if ( !files || files.length == 0 ) break;
+
+        // delete all files from S3
+        if(files !== null && files.length > 0){
+            for(let i=0; i<files.length; i++){
+                // init S3 SDK
+                const svc_fs = Context.get('services').get('filesystem');
+                const svc_mountpoint =
+                    Context.get('services').get('mountpoint');
+                const storage = svc_mountpoint.get_storage();
+                const op_delete = storage.create_delete();
+                await op_delete.run({
+                    node: await svc_fs.node(new NodeUIDSelector(files[i].uuid))
+                });
+            }
         }
     }