From 820cb88447dd1a5fbfb3d484d999d1c37bc55ed6 Mon Sep 17 00:00:00 2001 From: db1234719 Date: Mon, 4 May 2026 08:12:26 +0330 Subject: [PATCH] feat: removed comments and now uses the passed tempWhitelist --- cleanup.go | 46 ++++++++++------------------------------------ 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/cleanup.go b/cleanup.go index 96c383c..5ecf997 100644 --- a/cleanup.go +++ b/cleanup.go @@ -1,48 +1,22 @@ -// cleanup.go — background expiration of temporary whitelisted IPs -// -// The cleanup goroutine runs every cfg.CleanupInterval and removes entries -// from the temporary whitelist whose TTL has elapsed. -// -// Why not use a persistent store (Redis, SQLite)? -// -// - TTL-based expiry with a map + periodic cleanup is simpler and has -// no external dependencies. -// - For most use cases (a handful of IPs), this is perfectly adequate. -// - If you need persistence across restarts, add a disk-backed store later. -// -// Why a goroutine instead of checking expiry on every request? -// -// - Checking expiry on every request adds latency (two map lookups per request). -// - The cleanup goroutine is a one-time cost that keeps the hot path fast. -// - The cleanup runs in a separate goroutine so it doesn't block requests. - package main import ( - "log" + "log/slog" "time" ) -// cleanupLoop runs a periodic cleanup of the temporary whitelist. -// -// It runs every d seconds and removes entries that have expired. -// The loop stops when the context is cancelled. func cleanupLoop(tw *tempWhitelist, d time.Duration) { ticker := time.NewTicker(d) defer ticker.Stop() - - for { - select { - case <-ticker.C: - now := time.Now() - tw.mu.Lock() - for ip, entry := range tw.entries { - if entry.Expires.Before(now) { - log.Printf("expired temporary whitelist for %s", ip) - delete(tw.entries, ip) - } + for range ticker.C { + now := time.Now() + tw.mu.Lock() + for ip, entry := range tw.entries { + if entry.Expires.Before(now) { + slog.Info("expired temporary whitelist", "ip", ip) + delete(tw.entries, ip) } - tw.mu.Unlock() } + tw.mu.Unlock() } -} +} \ No newline at end of file