1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-12-27 20:45:28 -05:00

no-op timer

This commit is contained in:
Darien Raymond
2017-04-04 10:24:38 +02:00
parent 55ecd92064
commit 18b0b87c52
4 changed files with 23 additions and 13 deletions

View File

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