mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-01 22:46:38 +08:00
b3a55d4ab5
- Concurrency: Added lock protection to WebSocket writes, fixed timer leaks, and prevented config cache listener context leaks. - Performance: Added memory cache in ObservabilityBufferStore, periodic cleaning in CH Deduplicator, and buffered ZIP batch download writes. - Design: Introduced Redis caching for OAuth session/tokens, sanitized raw DB error messages, segregated handlers and logics, and standard CAP response envelopes.
51 lines
976 B
Go
51 lines
976 B
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package chwriter
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const dedupTTL = 2 * time.Minute
|
|
|
|
type dedupSet struct {
|
|
mu sync.Mutex
|
|
keys map[string]time.Time
|
|
lastCleanup time.Time
|
|
}
|
|
|
|
func newDedupSet() *dedupSet {
|
|
return &dedupSet{
|
|
keys: make(map[string]time.Time),
|
|
lastCleanup: time.Now(),
|
|
}
|
|
}
|
|
|
|
// markIfNew records key when it has not been seen within dedupTTL.
|
|
func (s *dedupSet) markIfNew(key string) bool {
|
|
if key == "" {
|
|
return false
|
|
}
|
|
|
|
now := time.Now()
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
// Periodically clean up all expired keys (e.g., every 30 seconds)
|
|
if now.Sub(s.lastCleanup) >= 30*time.Second {
|
|
for existing, expiresAt := range s.keys {
|
|
if now.After(expiresAt) {
|
|
delete(s.keys, existing)
|
|
}
|
|
}
|
|
s.lastCleanup = now
|
|
}
|
|
|
|
if expiresAt, exists := s.keys[key]; exists && now.Before(expiresAt) {
|
|
return false
|
|
}
|
|
s.keys[key] = now.Add(dedupTTL)
|
|
return true
|
|
} |