1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-06 01:05:35 -05:00

cancel sessions after inactivity

This commit is contained in:
Darien Raymond
2017-01-31 12:42:05 +01:00
parent 75b5a62c11
commit c462e35aad
11 changed files with 159 additions and 46 deletions

45
common/signal/timer.go Normal file
View File

@@ -0,0 +1,45 @@
package signal
import (
"context"
"time"
)
type ActivityTimer struct {
updated chan bool
timeout time.Duration
ctx context.Context
cancel context.CancelFunc
}
func (t *ActivityTimer) UpdateActivity() {
select {
case t.updated <- true:
default:
}
}
func (t *ActivityTimer) run() {
for {
time.Sleep(t.timeout)
select {
case <-t.ctx.Done():
return
case <-t.updated:
default:
t.cancel()
return
}
}
}
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
timer := &ActivityTimer{
ctx: ctx,
cancel: cancel,
timeout: timeout,
updated: make(chan bool, 1),
}
go timer.run()
return timer
}