mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-17 20:54:36 +03:00
Some checks failed
Scheduled assets update / geodat (push) Has been cancelled
Build and Release for Windows 7 / check-assets (push) Has been cancelled
Build and Release / check-assets (push) Has been cancelled
Test / check-assets (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (386, freebsd, ) (push) Has been cancelled
Build and Release / build (386, linux, ) (push) Has been cancelled
Build and Release / build (386, openbsd, ) (push) Has been cancelled
Build and Release / build (386, windows, ) (push) Has been cancelled
Build and Release / build (amd64, android, android-amd64) (push) Has been cancelled
Build and Release / build (amd64, darwin, ) (push) Has been cancelled
Build and Release / build (amd64, freebsd, ) (push) Has been cancelled
Build and Release / build (amd64, linux, ) (push) Has been cancelled
Build and Release / build (amd64, openbsd, ) (push) Has been cancelled
Build and Release / build (amd64, windows, ) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Build and Release / build (arm, 7, openbsd) (push) Has been cancelled
Build and Release / build (arm, 7, windows) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (arm64, openbsd) (push) Has been cancelled
Build and Release / build (arm64, windows) (push) Has been cancelled
Build and Release / build (loong64, linux) (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
signal包里面SetTimeout方法并发时可能会出现task close以后执行start导致泄露
83 lines
1.2 KiB
Go
83 lines
1.2 KiB
Go
package signal
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/task"
|
|
)
|
|
|
|
type ActivityUpdater interface {
|
|
Update()
|
|
}
|
|
|
|
type ActivityTimer struct {
|
|
sync.RWMutex
|
|
updated chan struct{}
|
|
checkTask *task.Periodic
|
|
onTimeout func()
|
|
}
|
|
|
|
func (t *ActivityTimer) Update() {
|
|
select {
|
|
case t.updated <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
func (t *ActivityTimer) check() error {
|
|
select {
|
|
case <-t.updated:
|
|
default:
|
|
t.finish()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *ActivityTimer) finish() {
|
|
t.Lock()
|
|
defer t.Unlock()
|
|
|
|
if t.onTimeout != nil {
|
|
t.onTimeout()
|
|
t.onTimeout = nil
|
|
}
|
|
if t.checkTask != nil {
|
|
t.checkTask.Close()
|
|
t.checkTask = nil
|
|
}
|
|
}
|
|
|
|
func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
|
|
if timeout == 0 {
|
|
t.finish()
|
|
return
|
|
}
|
|
|
|
checkTask := &task.Periodic{
|
|
Interval: timeout,
|
|
Execute: t.check,
|
|
}
|
|
|
|
t.Lock()
|
|
|
|
if t.checkTask != nil {
|
|
t.checkTask.Close()
|
|
}
|
|
t.checkTask = checkTask
|
|
t.Update()
|
|
common.Must(checkTask.Start())
|
|
t.Unlock()
|
|
}
|
|
|
|
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
|
|
timer := &ActivityTimer{
|
|
updated: make(chan struct{}, 1),
|
|
onTimeout: cancel,
|
|
}
|
|
timer.SetTimeout(timeout)
|
|
return timer
|
|
}
|