fix: cache 0-prefix validate results only 90s to prevent stale data from RIPE Stat blips

When RIPE Stat briefly returns empty prefixes, the validateResultCache
was storing this bad result for 15 minutes. Now 0-prefix results expire
after 90 seconds so the next request gets a fresh fetch.

Adds optional ttlMs parameter to resultCacheSet/resultCacheGet for
per-entry TTL control.
This commit is contained in:
Rene Fichtmueller 2026-04-29 22:14:01 +02:00
parent d3611a8169
commit 4cf1673e21

View File

@ -553,12 +553,13 @@ const validateResultCache = new Map();
const RESULT_CACHE_TTL = 15 * 60 * 1000; // 15 minutes const RESULT_CACHE_TTL = 15 * 60 * 1000; // 15 minutes
function resultCacheGet(map, key) { function resultCacheGet(map, key) {
const e = map.get(String(key)); const e = map.get(String(key));
if (e && (Date.now() - e.ts) < RESULT_CACHE_TTL) return e.data; const ttl = e && e.ttl ? e.ttl : RESULT_CACHE_TTL;
if (e && (Date.now() - e.ts) < ttl) return e.data;
return undefined; return undefined;
} }
function resultCacheSet(map, key, data) { function resultCacheSet(map, key, data, ttlMs) {
if (map.size > 2000) map.delete(map.keys().next().value); if (map.size > 2000) map.delete(map.keys().next().value);
map.set(String(key), { data, ts: Date.now() }); map.set(String(key), { data, ts: Date.now(), ttl: ttlMs || RESULT_CACHE_TTL });
} }
// ============================================================ // ============================================================
@ -3372,7 +3373,10 @@ const server = http.createServer(async (req, res) => {
note: "left=upstream providers, right=downstream customers, uncertain=peers. Sorted by power score.", note: "left=upstream providers, right=downstream customers, uncertain=peers. Sorted by power score.",
}, },
}; };
resultCacheSet(validateResultCache, rawAsn, validateResult); // Cache 0-prefix results only briefly (90s) — they may be due to temporary API failures
// Full results with prefixes are cached for the standard 15 minutes
const validateCacheTTL = allPrefixes.length === 0 ? 90 * 1000 : RESULT_CACHE_TTL;
resultCacheSet(validateResultCache, rawAsn, validateResult, validateCacheTTL);
return res.end(JSON.stringify(validateResult, null, 2)); return res.end(JSON.stringify(validateResult, null, 2));
} catch (err) { } catch (err) {
res.writeHead(500); res.writeHead(500);