mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user