Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.

Commit 7e43294

Browse files
committed
fix: add size limit and stale entry cleanup to nodeCache to prevent memory leak
1 parent 7bfca31 commit 7e43294

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

services/pyrodactyl.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PTERO_URL, PTERO_API_KEY, SERVER_LIMITS, FEATURE_LIMITS, DEPLOY_LOCATIO
22

33
const FETCH_TIMEOUT = 15000;
44
const CACHE_TTL = 5 * 60 * 1000;
5+
const CACHE_MAX_SIZE = 50;
56
const nodeCache = new Map();
67

78
async function fetchWithTimeout(url, options = {}, timeout = FETCH_TIMEOUT) {
@@ -81,7 +82,17 @@ async function getNode(nodeId) {
8182
}
8283
const data = await pteroFetch(`/nodes/${nodeId}`);
8384
const node = data.attributes;
85+
if (nodeCache.size >= CACHE_MAX_SIZE) {
86+
const oldest = nodeCache.keys().next().value;
87+
nodeCache.delete(oldest);
88+
}
8489
nodeCache.set(nodeId, { data: node, ts: Date.now() });
90+
if (nodeCache.size % 10 === 0) {
91+
const cutoff = Date.now() - CACHE_TTL;
92+
for (const [id, entry] of nodeCache) {
93+
if (entry.ts < cutoff) nodeCache.delete(id);
94+
}
95+
}
8596
return node;
8697
}
8798

0 commit comments

Comments
 (0)