Concurrency Manager (concurrency-manager.js)
Examine the rendering process limiter, async queue scheduler, and cpu-bound scaling systems.
Key File Location: ./dinou/core/concurrency-manager.js💡 Overview
RSC servers compile complex component trees and load multiple database states. Under high request traffic, initiating too many render cycles concurrently can saturate CPU bounds and trigger memory out-of-memory (OOM) crashes.
The concurrency-manager.js file exposes a global queue manager that restricts concurrent page renders to a safe threshold, deferring additional incoming requests to a queue.
📊 Concurrency Queue Flow
The flowchart below traces the task scheduling lifecycle of the concurrency queue:
⚡ Resource Protection
The manager configures limit thresholds dynamically to fit the host hardware:
- CPU-Bound Scaling: Defaults the concurrency limit to
CPUs * 2. For example, on a 4-core processor, it permits up to 8 concurrent render cycles, queueing any additional operations. - Configurable Limits: Allows overriding limits via the
MAX_CONCURRENT_RENDERSenvironment variable. - Automatic Resolution: Leverages Javascript `try/finally` blocks to guarantee that queued tasks proceed even if an active rendering thread crashes.
⚙️ Complete Code Walkthrough
Below is the full, complete code of concurrency-manager.js:
class ConcurrencyManager {
constructor(maxConcurrent) {
this.maxConcurrent = maxConcurrent;
this.activeCount = 0;
this.queue = [];
}
/**
* Executes an asynchronous task respecting the concurrency limit.
* @param {Function} task - Function that returns a promise (e.g., render/fork logic)
*/
async run(task) {
// 1. If active renders exceed limit, queue a pending promise callback
if (this.activeCount >= this.maxConcurrent) {
await new Promise((resolve) => this.queue.push(resolve));
}
this.activeCount++;
try {
// 2. Execute target async task
return await task();
} finally {
this.activeCount--;
// 3. Pop and execute the next task in the queue once current task finishes
if (this.queue.length > 0) {
const nextResolve = this.queue.shift();
nextResolve(); // Resolves the promise in step 1, letting the queued task proceed
}
}
}
// Helper for server status monitoring
getStatus() {
return { active: this.activeCount, queued: this.queue.length };
}
}
// 4. Initialize global instance bound to CPU cores
const os = require("os");
const MAX_PROCESSES =
process.env.MAX_CONCURRENT_RENDERS || Math.max(1, os.cpus().length * 2);
const processLimiter = new ConcurrencyManager(MAX_PROCESSES);
module.exports = processLimiter;