Skip to content

Commit

Permalink
separate mutext for actual detection
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Pickett committed Jan 2, 2025
1 parent defd382 commit ec29e67
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions ee/presencedetection/presencedetection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const (
)

type PresenceDetector struct {
lastDetection time.Time
mutex sync.Mutex
lastDetection time.Time
lastDetectionMutex, detectionMutex sync.Mutex
// detector is an interface to allow for mocking in tests
detector detectorIface
}
Expand All @@ -33,13 +33,8 @@ func (d *detector) Detect(reason string) (bool, error) {
// DetectPresence checks if the user is present by detecting the presence of a user.
// It returns the duration since the last detection.
func (pd *PresenceDetector) DetectPresence(reason string, detectionInterval time.Duration) (time.Duration, error) {
// using try lock here because we don't want presence detections to queue up,
// in the event that the users presses cancel, if the request were queued up, it would
// request the presence detection again
if !pd.mutex.TryLock() {
return DetectionFailedDurationValue, errors.New("detection already in progress")
}
defer pd.mutex.Unlock()
pd.lastDetectionMutex.Lock()
defer pd.lastDetectionMutex.Unlock()

if pd.detector == nil {
pd.detector = &detector{}
Expand All @@ -50,6 +45,14 @@ func (pd *PresenceDetector) DetectPresence(reason string, detectionInterval time
return time.Since(pd.lastDetection), nil
}

// using try lock here because we don't want presence detections to queue up,
// in the event that the users presses cancel, if the request were queued up, it would
// request the presence detection again
if !pd.detectionMutex.TryLock() {
return DetectionFailedDurationValue, errors.New("detection already in progress")
}
defer pd.detectionMutex.Unlock()

success, err := pd.detector.Detect(reason)
if err != nil {
// if we got an error, we behave as if there have been no successful detections in the past
Expand Down

0 comments on commit ec29e67

Please sign in to comment.