Skip to content

Commit

Permalink
feat: add thread-safe access to detection timestamps
Browse files Browse the repository at this point in the history
- Introduced `detectionMutex` to protect concurrent access to LastDogDetection and LastHumanDetection maps
- Added read and write locks in dog and human detection handling methods
- Ensured thread-safe retrieval and update of detection timestamps
- Improved concurrency safety for detection filtering and processing
  • Loading branch information
tphakala committed Jan 25, 2025
1 parent 9c8b18d commit f72740c
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/analysis/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Processor struct {
pendingMutex sync.Mutex // Mutex to protect access to pendingDetections
lastDogDetectionLog map[string]time.Time
dogDetectionMutex sync.Mutex
detectionMutex sync.RWMutex // Mutex to protect LastDogDetection and LastHumanDetection maps
controlChan chan string
}

Expand Down Expand Up @@ -277,7 +278,9 @@ func (p *Processor) handleDogDetection(item *queue.Results, speciesLowercase str
if p.Settings.Realtime.DogBarkFilter.Enabled && strings.Contains(speciesLowercase, "dog") &&
result.Confidence > p.Settings.Realtime.DogBarkFilter.Confidence {
log.Printf("Dog detected with confidence %.3f/%.3f from source %s", result.Confidence, p.Settings.Realtime.DogBarkFilter.Confidence, item.Source)
p.detectionMutex.Lock()
p.LastDogDetection[item.Source] = item.StartTime
p.detectionMutex.Unlock()
}
}

Expand All @@ -289,7 +292,9 @@ func (p *Processor) handleHumanDetection(item *queue.Results, speciesLowercase s
log.Printf("Human detected with confidence %.3f/%.3f from source %s", result.Confidence, p.Settings.Realtime.PrivacyFilter.Confidence, item.Source)
// put human detection timestamp into LastHumanDetection map. This is used to discard
// bird detections if a human vocalization is detected after the first detection
p.detectionMutex.Lock()
p.LastHumanDetection[item.Source] = item.StartTime
p.detectionMutex.Unlock()
}
}

Expand Down Expand Up @@ -345,7 +350,9 @@ func (p *Processor) shouldDiscardDetection(item *PendingDetection, minDetections

// Check privacy filter
if p.Settings.Realtime.PrivacyFilter.Enabled {
p.detectionMutex.RLock()
lastHumanDetection, exists := p.LastHumanDetection[item.Source]
p.detectionMutex.RUnlock()
if exists && lastHumanDetection.After(item.FirstDetected) {
return true, "privacy filter"
}
Expand All @@ -354,10 +361,15 @@ func (p *Processor) shouldDiscardDetection(item *PendingDetection, minDetections
// Check dog bark filter
if p.Settings.Realtime.DogBarkFilter.Enabled {
if p.Settings.Realtime.DogBarkFilter.Debug {
p.detectionMutex.RLock()
log.Printf("Last dog detection: %s\n", p.LastDogDetection)
p.detectionMutex.RUnlock()
}
if p.CheckDogBarkFilter(item.Detection.Note.CommonName, p.LastDogDetection[item.Source]) ||
p.CheckDogBarkFilter(item.Detection.Note.ScientificName, p.LastDogDetection[item.Source]) {
p.detectionMutex.RLock()
lastDogDetection := p.LastDogDetection[item.Source]
p.detectionMutex.RUnlock()
if p.CheckDogBarkFilter(item.Detection.Note.CommonName, lastDogDetection) ||
p.CheckDogBarkFilter(item.Detection.Note.ScientificName, lastDogDetection) {
return true, "recent dog bark"
}
}
Expand Down

0 comments on commit f72740c

Please sign in to comment.