From 8a642fa274257848d965df6f0e6fbf72b3e81845 Mon Sep 17 00:00:00 2001 From: Drew Lemmy Date: Tue, 29 Sep 2020 17:54:03 +0100 Subject: [PATCH] fix: too many milo parallel tasks hurting server --- milo/apis/storage.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/milo/apis/storage.lua b/milo/apis/storage.lua index c8d6642..fb086ea 100644 --- a/milo/apis/storage.lua +++ b/milo/apis/storage.lua @@ -9,6 +9,8 @@ local device = _G.device local os = _G.os local parallel = _G.parallel +local SCAN_CHUNK_SIZE = 16 + local Storage = class() local function loadOld(storage) @@ -263,7 +265,18 @@ function Storage:listItems(throttle) end if #t > 0 then - parallel.waitForAll(table.unpack(t)) + local chunk = {} + + -- Split the work into chunks to avoid spamming too many coroutines. + for i = 1, #t do + table.insert(chunk, t[i]) + + -- When we reach the chunk limit, execute them and start a new chunk + if #chunk >= SCAN_CHUNK_SIZE then + parallel.waitForAll(table.unpack(chunk)) + chunk = {} + end + end end for _, adapter in self:onlineAdapters() do -- 2.49.1