cleanupExpiredEntries uses the hash ([32]byte) as the key type.
(interval time.Duration)
| 214 | |
| 215 | // cleanupExpiredEntries uses the hash ([32]byte) as the key type. |
| 216 | func cleanupExpiredEntries(interval time.Duration) { |
| 217 | ticker := time.NewTicker(interval) |
| 218 | defer ticker.Stop() |
| 219 | cleanedCount := 0 |
| 220 | lastCleanupLogTime := time.Now() |
| 221 | for range ticker.C { |
| 222 | mapMutex.Lock() |
| 223 | now := time.Now() |
| 224 | currentMapSize := len(seenIDs) |
| 225 | deletedInCycle := 0 // Track deletes per cycle for more granular debug |
| 226 | for h, entry := range seenIDs { |
| 227 | if now.Sub(entry.timestamp) >= dedupWindow { |
| 228 | delete(seenIDs, h) |
| 229 | cleanedCount++ |
| 230 | deletedInCycle++ |
| 231 | } |
| 232 | } |
| 233 | mapMutex.Unlock() |
| 234 | |
| 235 | // Log hourly summary if any were cleaned in the last hour |
| 236 | if cleanedCount > 0 && time.Since(lastCleanupLogTime) >= time.Hour { |
| 237 | slog.Debug("cleaned up expired entries", |
| 238 | "count_past_hour", cleanedCount, |
| 239 | "remaining_entries", currentMapSize-deletedInCycle) // Use size before delete for consistency |
| 240 | cleanedCount = 0 // Reset hourly count |
| 241 | lastCleanupLogTime = time.Now() |
| 242 | } else if deletedInCycle > 0 { |
| 243 | // Optional: Log every cycle if debugging cleanup |
| 244 | // slog.Debug("cleanup cycle completed", "deleted", deletedInCycle, "remaining", currentMapSize-deletedInCycle) |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // connectionTracker is middleware that wraps an http.Handler to track active connections |
| 250 | func connectionTracker(next http.Handler) http.Handler { |