-
Notifications
You must be signed in to change notification settings - Fork 0
/
idle.go
56 lines (50 loc) · 1.21 KB
/
idle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package idle
import (
"context"
"errors"
"sync"
"time"
)
var ErrTimedOut = errors.New("timed out due to no activity")
// IdleTimer is a dead-mans switch to detect inactivity. Create with New.
type IdleTimer struct {
timeout time.Duration
done chan struct{}
lastActive time.Time
mu sync.RWMutex // protects lastActive
}
// New creates an IdleTimer that will return errors if the there is no activity
// seen for the given duration.
func New(d time.Duration) *IdleTimer {
return &IdleTimer{
timeout: d,
done: make(chan struct{}),
}
}
// SetActive marks this timer as still active.
func (t *IdleTimer) SetActive() {
t.mu.Lock()
t.lastActive = time.Now()
t.mu.Unlock()
}
// Run runs until the context is cancelled or no-one calls SetActive for more
// than the timer's duration. Always returns an error, either the context's or
// ErrTimedOut.
func (t *IdleTimer) Run(ctx context.Context) error {
ticker := time.NewTicker(t.timeout)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case now := <-ticker.C:
tooOld := now.Add(-t.timeout)
t.mu.RLock()
isIdle := t.lastActive.Before(tooOld)
t.mu.RUnlock()
if isIdle {
return ErrTimedOut
}
}
}
}