Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dont queue up presence detections #2022

Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions ee/presencedetection/presencedetection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package presencedetection

import (
"errors"
"fmt"
"sync"
"time"
Expand All @@ -13,7 +14,7 @@ const (

type PresenceDetector struct {
lastDetection time.Time
mutext sync.Mutex
mutex sync.Mutex
// detector is an interface to allow for mocking in tests
detector detectorIface
}
Expand All @@ -32,8 +33,13 @@ 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) {
pd.mutext.Lock()
defer pd.mutext.Unlock()
// 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() {
Copy link
Contributor Author

@James-Pickett James-Pickett Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, this seems racy, may need 2 mutexes here, one for the lastDetection value and one for actually prompting the user then just try lock the actual prompting

return DetectionFailedDurationValue, errors.New("detection already in progress")
}
defer pd.mutex.Unlock()

if pd.detector == nil {
pd.detector = &detector{}
Expand Down
Loading