1
mirror of https://github.com/XTLS/Xray-core.git synced 2025-12-13 05:14:17 +04:00

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

Fixes https://github.com/XTLS/Xray-core/issues/5051
This commit is contained in:
风扇滑翔翼
2025-08-27 17:28:53 +08:00
committed by GitHub
parent 11f0513bce
commit 2ee372e758
2 changed files with 35 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ import (
"go/build"
"os"
"path/filepath"
"reflect"
"strings"
"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)
}
// 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
}