1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/signal/timer.go

83 lines
1.2 KiB
Go
Raw Normal View History

2017-01-31 06:42:05 -05:00
package signal
import (
"context"
2018-05-27 08:42:53 -04:00
"sync"
2017-01-31 06:42:05 -05:00
"time"
2018-05-27 08:42:53 -04:00
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/task"
2017-01-31 06:42:05 -05:00
)
type ActivityUpdater interface {
2017-04-04 04:24:38 -04:00
Update()
}
type ActivityTimer struct {
2018-05-27 08:42:53 -04:00
sync.RWMutex
updated chan struct{}
checkTask *task.Periodic
onTimeout func()
2017-01-31 06:42:05 -05:00
}
func (t *ActivityTimer) Update() {
2017-01-31 06:42:05 -05:00
select {
2018-02-08 09:39:46 -05:00
case t.updated <- struct{}{}:
2017-01-31 06:42:05 -05:00
default:
}
}
2018-05-27 08:42:53 -04:00
func (t *ActivityTimer) check() error {
2018-02-06 05:16:49 -05:00
select {
2018-05-27 08:42:53 -04:00
case <-t.updated:
default:
t.finish()
2018-02-06 05:16:49 -05:00
}
2018-05-27 08:42:53 -04:00
return nil
}
2018-05-27 08:42:53 -04:00
func (t *ActivityTimer) finish() {
t.Lock()
defer t.Unlock()
2017-12-14 11:39:58 -05:00
2018-05-27 08:42:53 -04:00
if t.onTimeout != nil {
t.onTimeout()
2018-05-28 08:02:09 -04:00
t.onTimeout = nil
2017-12-14 11:39:58 -05:00
}
2018-05-27 08:42:53 -04:00
if t.checkTask != nil {
t.checkTask.Close()
2018-05-27 08:42:53 -04:00
t.checkTask = nil
}
}
2017-12-14 11:39:58 -05:00
2018-05-27 08:42:53 -04:00
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
if timeout == 0 {
t.finish()
2018-05-28 07:55:20 -04:00
return
2018-05-27 08:42:53 -04:00
}
2017-11-23 08:58:35 -05:00
2018-06-22 05:19:33 -04:00
checkTask := &task.Periodic{
Interval: timeout,
Execute: t.check,
}
2018-05-27 08:42:53 -04:00
t.Lock()
2017-05-08 11:09:21 -04:00
2018-05-27 08:42:53 -04:00
if t.checkTask != nil {
t.checkTask.Close()
2018-05-27 08:42:53 -04:00
}
2018-06-22 05:19:33 -04:00
t.checkTask = checkTask
2018-05-27 08:42:53 -04:00
t.Unlock()
t.Update()
2018-06-22 05:19:33 -04:00
common.Must(checkTask.Start())
2017-01-31 06:42:05 -05:00
}
2017-11-14 18:36:14 -05:00
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
timer := &ActivityTimer{
2018-05-27 08:42:53 -04:00
updated: make(chan struct{}, 1),
onTimeout: cancel,
2017-01-31 06:42:05 -05:00
}
2018-05-27 08:42:53 -04:00
timer.SetTimeout(timeout)
2017-11-14 18:36:14 -05:00
return timer
2017-01-31 06:42:05 -05:00
}