|
@@ -5,21 +5,25 @@ import (
|
|
|
"time"
|
|
"time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-type ActivityTimer struct {
|
|
|
|
|
|
|
+type ActivityTimer interface {
|
|
|
|
|
+ Update()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type realActivityTimer struct {
|
|
|
updated chan bool
|
|
updated chan bool
|
|
|
timeout time.Duration
|
|
timeout time.Duration
|
|
|
ctx context.Context
|
|
ctx context.Context
|
|
|
cancel context.CancelFunc
|
|
cancel context.CancelFunc
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (t *ActivityTimer) Update() {
|
|
|
|
|
|
|
+func (t *realActivityTimer) Update() {
|
|
|
select {
|
|
select {
|
|
|
case t.updated <- true:
|
|
case t.updated <- true:
|
|
|
default:
|
|
default:
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (t *ActivityTimer) run() {
|
|
|
|
|
|
|
+func (t *realActivityTimer) run() {
|
|
|
for {
|
|
for {
|
|
|
select {
|
|
select {
|
|
|
case <-time.After(t.timeout):
|
|
case <-time.After(t.timeout):
|
|
@@ -37,9 +41,9 @@ func (t *ActivityTimer) run() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func CancelAfterInactivity(ctx context.Context, timeout time.Duration) (context.Context, *ActivityTimer) {
|
|
|
|
|
|
|
+func CancelAfterInactivity(ctx context.Context, timeout time.Duration) (context.Context, ActivityTimer) {
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
- timer := &ActivityTimer{
|
|
|
|
|
|
|
+ timer := &realActivityTimer{
|
|
|
ctx: ctx,
|
|
ctx: ctx,
|
|
|
cancel: cancel,
|
|
cancel: cancel,
|
|
|
timeout: timeout,
|
|
timeout: timeout,
|
|
@@ -48,3 +52,11 @@ func CancelAfterInactivity(ctx context.Context, timeout time.Duration) (context.
|
|
|
go timer.run()
|
|
go timer.run()
|
|
|
return ctx, timer
|
|
return ctx, timer
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+type noOpActivityTimer struct{}
|
|
|
|
|
+
|
|
|
|
|
+func (noOpActivityTimer) Update() {}
|
|
|
|
|
+
|
|
|
|
|
+func BackgroundTimer() ActivityTimer {
|
|
|
|
|
+ return noOpActivityTimer{}
|
|
|
|
|
+}
|