DNS Cache verbesser

This commit is contained in:
ecki
2026-04-24 11:48:07 +02:00
parent d8a13f1961
commit 7d47ec40ff
+46 -11
View File
@@ -2,42 +2,77 @@
DNS CACHE (mit TTL) DNS CACHE (mit TTL)
========================= */ ========================= */
// =========================
// DNS CACHE (minimal + robust)
// =========================
const DNS_CACHE_TTL = 300000 // 5 min für erfolgreiche Antworten
const DNS_ERROR_TTL = 5000 // 5 sek für Fehler
const dnsCache = {} const dnsCache = {}
const pending = {}
function normalizeDomain(domain) {
return domain.trim().toLowerCase()
}
async function getDNS(domain){ async function getDNS(domain){
const now = Date.now() if(!domain) return []
const TTL = 30000 // 30 Sekunden
if(dnsCache[domain] && (now - dnsCache[domain].time < TTL)){ const key = normalizeDomain(domain)
return dnsCache[domain].ips const now = Date.now()
const cached = dnsCache[key]
// ✅ gültiger Cache
if(cached && (now - cached.time < cached.ttl)){
return cached.ips
} }
// ✅ laufende Anfrage wiederverwenden (dedupe)
if(pending[key]){
return pending[key]
}
pending[key] = (async () => {
try{ try{
const res = await api(
API + "/dns/" + encodeURIComponent(key),
{},
{ showError: false }
)
const res = await api(API + "/dns/" + domain, {}, { showError: false }) const ips = res?.ips || []
const ips = res.ips || []
dnsCache[domain] = { dnsCache[key] = {
ips: ips, ips,
time: now time: Date.now(),
ttl: DNS_CACHE_TTL
} }
return ips return ips
}catch(e){ }catch(e){
dnsCache[domain] = { // ❗ Fehler nur kurz cachen
dnsCache[key] = {
ips: [], ips: [],
time: now time: Date.now(),
ttl: DNS_ERROR_TTL
} }
return [] return []
}finally{
delete pending[key]
} }
})()
return pending[key]
} }
function dnsStatusMarkup(expectedIp, ips){ function dnsStatusMarkup(expectedIp, ips){
if(!ips.length){ if(!ips.length){