mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-07-13 15:18:51 +00:00
Refactor Timer
This commit is contained in:
+21
-45
@@ -5,9 +5,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common"
|
|
||||||
"github.com/xtls/xray-core/common/task"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ActivityUpdater interface {
|
type ActivityUpdater interface {
|
||||||
@@ -15,45 +12,28 @@ type ActivityUpdater interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ActivityTimer struct {
|
type ActivityTimer struct {
|
||||||
mu sync.RWMutex
|
mu sync.Mutex
|
||||||
updated chan struct{}
|
timer atomic.Pointer[time.Timer]
|
||||||
checkTask *task.Periodic
|
timeout time.Duration
|
||||||
onTimeout func()
|
onTimeout func()
|
||||||
consumed atomic.Bool
|
|
||||||
once sync.Once
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) Update() {
|
func (t *ActivityTimer) Update() {
|
||||||
select {
|
t.mu.Lock()
|
||||||
case t.updated <- struct{}{}:
|
defer t.mu.Unlock()
|
||||||
default:
|
if timer := t.timer.Load(); timer != nil {
|
||||||
|
timer.Reset(t.timeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) check() error {
|
|
||||||
select {
|
|
||||||
case <-t.updated:
|
|
||||||
default:
|
|
||||||
t.finish()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *ActivityTimer) finish() {
|
func (t *ActivityTimer) finish() {
|
||||||
t.once.Do(func() {
|
if timer := t.timer.Swap(nil); timer != nil {
|
||||||
t.consumed.Store(true)
|
timer.Stop()
|
||||||
t.mu.Lock()
|
|
||||||
defer t.mu.Unlock()
|
|
||||||
|
|
||||||
common.CloseIfExists(t.checkTask)
|
|
||||||
t.onTimeout()
|
t.onTimeout()
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
||||||
if t.consumed.Load() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
t.finish()
|
t.finish()
|
||||||
return
|
return
|
||||||
@@ -61,25 +41,21 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
|||||||
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
// double check, just in case
|
if timer := t.timer.Load(); timer != nil {
|
||||||
if t.consumed.Load() {
|
t.timeout = timeout
|
||||||
return
|
timer.Reset(timeout)
|
||||||
}
|
}
|
||||||
newCheckTask := &task.Periodic{
|
|
||||||
Interval: timeout,
|
|
||||||
Execute: t.check,
|
|
||||||
}
|
|
||||||
common.CloseIfExists(t.checkTask)
|
|
||||||
t.checkTask = newCheckTask
|
|
||||||
t.Update()
|
|
||||||
common.Must(newCheckTask.Start())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
|
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
|
||||||
timer := &ActivityTimer{
|
activityTimer := &ActivityTimer{
|
||||||
updated: make(chan struct{}, 1),
|
timeout: timeout,
|
||||||
onTimeout: cancel,
|
onTimeout: cancel,
|
||||||
}
|
}
|
||||||
timer.SetTimeout(timeout)
|
if timeout == 0 {
|
||||||
return timer
|
cancel()
|
||||||
|
return activityTimer
|
||||||
|
}
|
||||||
|
activityTimer.timer.Store(time.AfterFunc(timeout, activityTimer.finish))
|
||||||
|
return activityTimer
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user