feat(memory-management): Introduce MEMORY_CONFIG for session and DNS management, including session TTL, cleanup intervals, and proxy dispatcher limits.

This commit is contained in:
decolua
2026-03-12 15:57:21 +07:00
parent 372b985ee9
commit 8223c87988
4 changed files with 41 additions and 17 deletions
+22 -13
View File
@@ -9,11 +9,24 @@
*/
import crypto from "crypto";
import { MEMORY_CONFIG } from "../config/constants.js";
// Runtime storage for session IDs (per connection/account)
// Key: connectionId (email or identifier), Value: sessionId
// Runtime storage: Key = connectionId, Value = { sessionId, lastUsed }
const runtimeSessionStore = new Map();
// Periodically evict entries that haven't been used within TTL
const cleanupInterval = setInterval(() => {
const now = Date.now();
for (const [key, entry] of runtimeSessionStore) {
if (now - entry.lastUsed > MEMORY_CONFIG.sessionTtlMs) {
runtimeSessionStore.delete(key);
}
}
}, MEMORY_CONFIG.sessionCleanupIntervalMs);
// Allow Node.js to exit even if interval is still active
if (cleanupInterval.unref) cleanupInterval.unref();
/**
* Get or create a session ID for the given connection.
*
@@ -30,22 +43,18 @@ const runtimeSessionStore = new Map();
*/
export function deriveSessionId(connectionId) {
if (!connectionId) {
// Fallback for requests without a connection identifier
return generateBinaryStyleId();
}
// Check if we already have a session ID for this connection in this process run
if (runtimeSessionStore.has(connectionId)) {
return runtimeSessionStore.get(connectionId);
const existing = runtimeSessionStore.get(connectionId);
if (existing) {
existing.lastUsed = Date.now();
return existing.sessionId;
}
// Generate a new ID using the binary's exact logic
const newSessionId = generateBinaryStyleId();
// Store it for future requests from this connection
runtimeSessionStore.set(connectionId, newSessionId);
return newSessionId;
const sessionId = generateBinaryStyleId();
runtimeSessionStore.set(connectionId, { sessionId, lastUsed: Date.now() });
return sessionId;
}
/**