mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-09-16 14:30:28 +00:00
Compare commits
2
Commits
close-trace
..
timer
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b92aa0358a | ||
|
|
316bcd6343 |
+28
-45
@@ -3,11 +3,7 @@ package signal
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/task"
|
||||
)
|
||||
|
||||
type ActivityUpdater interface {
|
||||
@@ -15,45 +11,35 @@ type ActivityUpdater interface {
|
||||
}
|
||||
|
||||
type ActivityTimer struct {
|
||||
mu sync.RWMutex
|
||||
updated chan struct{}
|
||||
checkTask *task.Periodic
|
||||
mu sync.Mutex
|
||||
// timer will be nil if this timer is already finished
|
||||
timer *time.Timer
|
||||
timeout time.Duration
|
||||
onTimeout func()
|
||||
consumed atomic.Bool
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) Update() {
|
||||
select {
|
||||
case t.updated <- struct{}{}:
|
||||
default:
|
||||
// someone already called Update or closing, just return
|
||||
if !t.mu.TryLock() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) check() error {
|
||||
select {
|
||||
case <-t.updated:
|
||||
default:
|
||||
t.finish()
|
||||
defer t.mu.Unlock()
|
||||
if t.timer != nil {
|
||||
t.timer.Reset(t.timeout)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) finish() {
|
||||
t.once.Do(func() {
|
||||
t.consumed.Store(true)
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
common.CloseIfExists(t.checkTask)
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
if t.timer != nil {
|
||||
t.timer.Stop()
|
||||
t.onTimeout()
|
||||
})
|
||||
t.timer = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
||||
if t.consumed.Load() {
|
||||
return
|
||||
}
|
||||
if timeout == 0 {
|
||||
t.finish()
|
||||
return
|
||||
@@ -61,25 +47,22 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
||||
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
// double check, just in case
|
||||
if t.consumed.Load() {
|
||||
return
|
||||
if t.timer != nil {
|
||||
t.timeout = timeout
|
||||
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 {
|
||||
timer := &ActivityTimer{
|
||||
updated: make(chan struct{}, 1),
|
||||
activityTimer := &ActivityTimer{
|
||||
timeout: timeout,
|
||||
onTimeout: cancel,
|
||||
}
|
||||
timer.SetTimeout(timeout)
|
||||
return timer
|
||||
// strange situation
|
||||
if timeout == 0 {
|
||||
cancel()
|
||||
return activityTimer
|
||||
}
|
||||
activityTimer.timer = time.AfterFunc(timeout, activityTimer.finish)
|
||||
return activityTimer
|
||||
}
|
||||
|
||||
+1
-22
@@ -91,28 +91,7 @@ func executeRun(cmd *base.Command, args []string) {
|
||||
fmt.Println("Failed to start:", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
defer func() {
|
||||
closeErrCh := make(chan error, 1)
|
||||
go func() {
|
||||
closeErrCh <- server.Close()
|
||||
}()
|
||||
select {
|
||||
case err := <-closeErrCh:
|
||||
if err != nil {
|
||||
fmt.Println("Failed to close server:", err)
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
fmt.Println("Timeout when closing, printing traces:")
|
||||
buf := make([]byte, 1<<20)
|
||||
n := runtime.Stack(buf, true)
|
||||
blocks := strings.Split(string(buf[:n]), "\n\n")
|
||||
for _, block := range blocks {
|
||||
if strings.Contains(block, "github.com/xtls/xray-core/core.(*Instance).Close") {
|
||||
fmt.Println(block)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
defer server.Close()
|
||||
|
||||
// Explicitly triggering GC to remove garbage from config loading.
|
||||
runtime.GC()
|
||||
|
||||
Reference in New Issue
Block a user