feat: removed comments and now uses the passed tempWhitelist

This commit is contained in:
2026-05-04 08:12:26 +03:30
parent 4e949912d6
commit 820cb88447

View File

@@ -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 package main
import ( import (
"log" "log/slog"
"time" "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) { func cleanupLoop(tw *tempWhitelist, d time.Duration) {
ticker := time.NewTicker(d) ticker := time.NewTicker(d)
defer ticker.Stop() defer ticker.Stop()
for range ticker.C {
for {
select {
case <-ticker.C:
now := time.Now() now := time.Now()
tw.mu.Lock() tw.mu.Lock()
for ip, entry := range tw.entries { for ip, entry := range tw.entries {
if entry.Expires.Before(now) { if entry.Expires.Before(now) {
log.Printf("expired temporary whitelist for %s", ip) slog.Info("expired temporary whitelist", "ip", ip)
delete(tw.entries, ip) delete(tw.entries, ip)
} }
} }
tw.mu.Unlock() tw.mu.Unlock()
} }
} }
}