mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-09-24 23:57:59 +03:00
Compare commits
2
Commits
fix-custom
...
timer
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b92aa0358a | ||
|
|
316bcd6343 |
+28
-45
@@ -3,11 +3,7 @@ package signal
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
"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 +11,35 @@ type ActivityUpdater interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ActivityTimer struct {
|
type ActivityTimer struct {
|
||||||
mu sync.RWMutex
|
mu sync.Mutex
|
||||||
updated chan struct{}
|
// timer will be nil if this timer is already finished
|
||||||
checkTask *task.Periodic
|
timer *time.Timer
|
||||||
|
timeout time.Duration
|
||||||
onTimeout func()
|
onTimeout func()
|
||||||
consumed atomic.Bool
|
|
||||||
once sync.Once
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) Update() {
|
func (t *ActivityTimer) Update() {
|
||||||
select {
|
// someone already called Update or closing, just return
|
||||||
case t.updated <- struct{}{}:
|
if !t.mu.TryLock() {
|
||||||
default:
|
return
|
||||||
}
|
}
|
||||||
}
|
defer t.mu.Unlock()
|
||||||
|
if t.timer != nil {
|
||||||
func (t *ActivityTimer) check() error {
|
t.timer.Reset(t.timeout)
|
||||||
select {
|
|
||||||
case <-t.updated:
|
|
||||||
default:
|
|
||||||
t.finish()
|
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ActivityTimer) finish() {
|
func (t *ActivityTimer) finish() {
|
||||||
t.once.Do(func() {
|
t.mu.Lock()
|
||||||
t.consumed.Store(true)
|
defer t.mu.Unlock()
|
||||||
t.mu.Lock()
|
if t.timer != nil {
|
||||||
defer t.mu.Unlock()
|
t.timer.Stop()
|
||||||
|
|
||||||
common.CloseIfExists(t.checkTask)
|
|
||||||
t.onTimeout()
|
t.onTimeout()
|
||||||
})
|
t.timer = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 +47,22 @@ 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 t.timer != nil {
|
||||||
if t.consumed.Load() {
|
t.timeout = timeout
|
||||||
return
|
t.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)
|
// strange situation
|
||||||
return timer
|
if timeout == 0 {
|
||||||
|
cancel()
|
||||||
|
return activityTimer
|
||||||
|
}
|
||||||
|
activityTimer.timer = time.AfterFunc(timeout, activityTimer.finish)
|
||||||
|
return activityTimer
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user