UNPKG

@mrtkrcm/acp-claude-code

Version:

ACP (Agent Client Protocol) bridge for Claude Code

73 lines 3.23 kB
export class ResourceManager { concurrentOperations = new Set(); activeSessions = new Set(); monitoringInterval; limits; constructor(limits = {}) { this.limits = { maxMemoryMB: 1024, maxFileDescriptors: 1000, maxConcurrentOperations: 50, maxConcurrentSessions: 100, memoryWarningThresholdMB: 512, memoryCriticalThresholdMB: 768, ...limits }; this.monitoringInterval = setInterval(() => this.checkLimits(), 30000); } getStats() { const mem = process.memoryUsage(); return { memoryUsageMB: Math.round(mem.rss / 1024 / 1024), heapUsedMB: Math.round(mem.heapUsed / 1024 / 1024), heapTotalMB: Math.round(mem.heapTotal / 1024 / 1024), rssUsageMB: Math.round(mem.rss / 1024 / 1024), activeFileDescriptors: this.concurrentOperations.size * 2 + this.activeSessions.size + 10, concurrentOperations: this.concurrentOperations.size, activeSessions: this.activeSessions.size, uptime: Math.round(process.uptime()) }; } canStartOperation(_operationId) { const stats = this.getStats(); return stats.memoryUsageMB <= this.limits.memoryCriticalThresholdMB && stats.concurrentOperations < this.limits.maxConcurrentOperations && stats.activeFileDescriptors < this.limits.maxFileDescriptors; } startOperation(operationId) { if (!this.canStartOperation(operationId)) return false; this.concurrentOperations.add(operationId); return true; } finishOperation(operationId) { this.concurrentOperations.delete(operationId); } addSession(sessionId) { if (this.activeSessions.size >= this.limits.maxConcurrentSessions) return false; this.activeSessions.add(sessionId); return true; } removeSession(sessionId) { this.activeSessions.delete(sessionId); } checkLimits() { const stats = this.getStats(); if (stats.memoryUsageMB > this.limits.memoryCriticalThresholdMB && global.gc) global.gc(); } forceGarbageCollection() { if (global.gc) { global.gc(); return true; } return false; } getHealthStatus() { const stats = this.getStats(); if (stats.memoryUsageMB > this.limits.memoryCriticalThresholdMB || stats.concurrentOperations >= this.limits.maxConcurrentOperations || stats.activeSessions >= this.limits.maxConcurrentSessions) return 'critical'; if (stats.memoryUsageMB > this.limits.memoryWarningThresholdMB || stats.concurrentOperations > this.limits.maxConcurrentOperations * 0.8) return 'warning'; return 'healthy'; } destroy() { if (this.monitoringInterval) clearInterval(this.monitoringInterval); this.concurrentOperations.clear(); this.activeSessions.clear(); } } export const globalResourceManager = new ResourceManager(); process.once('exit', () => globalResourceManager.destroy()); process.once('SIGINT', () => globalResourceManager.destroy()); process.once('SIGTERM', () => globalResourceManager.destroy()); //# sourceMappingURL=resource-manager.js.map