Skip to content

Commit 7021832

Browse files
committed
Limit concurrent writes in upload processing to improve performance
1 parent 0e7e09c commit 7021832

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

server.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ const argPort = process.argv.find(arg => arg.startsWith("--port="));
1616
const PORT = argPort ? parseInt(argPort.split("=")[1], 10) : (process.env.PORT || DEFAULT_PORT);
1717
const version = process.env.APP_VERSION || "not_set";
1818
const slideshows = new Map();
19+
const MAX_CONCURRENT_WRITES = 5;
20+
let activeWrites = 0;
1921
const uploadQueue = [];
20-
let isWriting = false;
2122

2223
console.log(`Running version: ${version}`);
2324

@@ -69,9 +70,10 @@ const io = new Server(server, { maxHttpBufferSize: 1e8, pingTimeout: 60000 });
6970
const channels = new Map();
7071

7172
async function processUploadQueue() {
72-
if (isWriting || uploadQueue.length === 0) return;
73-
isWriting = true;
73+
if (activeWrites >= MAX_CONCURRENT_WRITES || uploadQueue.length === 0) return;
74+
activeWrites++;
7475
const { socket, data } = uploadQueue.shift();
76+
7577
try {
7678
const encryptedData = new Uint8Array(data.encrypted);
7779
const iv = new Uint8Array(data.iv);
@@ -82,9 +84,10 @@ async function processUploadQueue() {
8284
} catch (err) {
8385
console.error("Upload error:", err);
8486
socket.emit("error", { message: "Upload failed" });
87+
} finally {
88+
activeWrites--;
89+
processUploadQueue();
8590
}
86-
isWriting = false;
87-
processUploadQueue();
8891
}
8992

9093
function handleJoin(socket, docId) {

0 commit comments

Comments
 (0)