common/signal/timer.go: Refator to use sync.Once (#5052)

Fixes https://github.com/XTLS/Xray-core/issues/5051
pull/5065/head^2
风扇滑翔翼 2025-08-27 17:28:53 +08:00 committed by GitHub
parent 11f0513bce
commit 2ee372e758
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 20 deletions

View File

@ -7,6 +7,7 @@ import (
"go/build" "go/build"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings" "strings"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
@ -153,3 +154,14 @@ func GetModuleName(pathToProjectRoot string) (string, error) {
} }
return moduleName, fmt.Errorf("no `go.mod` file in every parent directory of `%s`", pathToProjectRoot) return moduleName, fmt.Errorf("no `go.mod` file in every parent directory of `%s`", pathToProjectRoot)
} }
// CloseIfExists call obj.Close() if obj is not nil.
func CloseIfExists(obj any) error {
if obj != nil {
v := reflect.ValueOf(obj)
if !v.IsNil() {
return Close(obj)
}
}
return nil
}

View File

@ -3,6 +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"
@ -14,10 +15,12 @@ type ActivityUpdater interface {
} }
type ActivityTimer struct { type ActivityTimer struct {
sync.RWMutex mu sync.RWMutex
updated chan struct{} updated chan struct{}
checkTask *task.Periodic checkTask *task.Periodic
onTimeout func() onTimeout func()
consumed atomic.Bool
once sync.Once
} }
func (t *ActivityTimer) Update() { func (t *ActivityTimer) Update() {
@ -37,39 +40,39 @@ func (t *ActivityTimer) check() error {
} }
func (t *ActivityTimer) finish() { func (t *ActivityTimer) finish() {
t.Lock() t.once.Do(func() {
defer t.Unlock() t.consumed.Store(true)
t.mu.Lock()
defer t.mu.Unlock()
if t.onTimeout != nil { common.CloseIfExists(t.checkTask)
t.onTimeout() t.onTimeout()
t.onTimeout = nil })
}
if t.checkTask != nil {
t.checkTask.Close()
t.checkTask = 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
} }
checkTask := &task.Periodic{ t.mu.Lock()
defer t.mu.Unlock()
// double check, just in case
if t.consumed.Load() {
return
}
newCheckTask := &task.Periodic{
Interval: timeout, Interval: timeout,
Execute: t.check, Execute: t.check,
} }
common.CloseIfExists(t.checkTask)
t.Lock() t.checkTask = newCheckTask
if t.checkTask != nil {
t.checkTask.Close()
}
t.checkTask = checkTask
t.Update() t.Update()
common.Must(checkTask.Start()) common.Must(newCheckTask.Start())
t.Unlock()
} }
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer { func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {