cleanup removes circuit breakers that haven't been accessed recently
()
| 293 | |
| 294 | // cleanup removes circuit breakers that haven't been accessed recently |
| 295 | func (cbm *CircuitBreakerManager) cleanup() { |
| 296 | // Prevent concurrent cleanups |
| 297 | if !cbm.cleanupMu.TryLock() { |
| 298 | return |
| 299 | } |
| 300 | defer cbm.cleanupMu.Unlock() |
| 301 | |
| 302 | now := time.Now() |
| 303 | cutoff := now.Add(-30 * time.Minute).Unix() // 30 minute TTL |
| 304 | |
| 305 | var toDelete []string |
| 306 | count := 0 |
| 307 | |
| 308 | cbm.breakers.Range(func(key, value interface{}) bool { |
| 309 | endpoint := key.(string) |
| 310 | entry := value.(*CircuitBreakerEntry) |
| 311 | |
| 312 | count++ |
| 313 | |
| 314 | // Remove if not accessed recently |
| 315 | if entry.lastAccess.Load() < cutoff { |
| 316 | toDelete = append(toDelete, endpoint) |
| 317 | } |
| 318 | |
| 319 | return true |
| 320 | }) |
| 321 | |
| 322 | // Delete expired entries |
| 323 | for _, endpoint := range toDelete { |
| 324 | cbm.breakers.Delete(endpoint) |
| 325 | } |
| 326 | |
| 327 | // Log cleanup results |
| 328 | if len(toDelete) > 0 && internal.LogLevel.InfoOrAbove() { |
| 329 | internal.Logger.Printf(context.Background(), logs.CircuitBreakerCleanup(len(toDelete), count)) |
| 330 | } |
| 331 | |
| 332 | cbm.lastCleanup.Store(now.Unix()) |
| 333 | } |
| 334 | |
| 335 | // Shutdown stops the cleanup goroutine |
| 336 | func (cbm *CircuitBreakerManager) Shutdown() { |
no test coverage detected