mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
修正代码,程序退出添加超时机制
This commit is contained in:
@@ -84,6 +84,25 @@ func main() {
|
||||
os.Exit(mainFunc())
|
||||
}
|
||||
|
||||
func stopMachineAndExit(m *machine.M) {
|
||||
|
||||
ch := make(chan struct{})
|
||||
go func() {
|
||||
m.Stop()
|
||||
close(ch)
|
||||
}()
|
||||
tCh := time.After(time.Second * 2)
|
||||
select {
|
||||
case <-tCh:
|
||||
log.Println("Close timeout")
|
||||
os.Exit(-1)
|
||||
case <-ch:
|
||||
break
|
||||
}
|
||||
os.Exit(0)
|
||||
|
||||
}
|
||||
|
||||
func mainFunc() (result int) {
|
||||
defer func() {
|
||||
//注意,这个recover代码并不是万能的,有时捕捉不到panic。
|
||||
@@ -107,7 +126,7 @@ func mainFunc() (result int) {
|
||||
|
||||
result = -3
|
||||
|
||||
defaultMachine.Stop()
|
||||
stopMachineAndExit(defaultMachine)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -312,8 +331,7 @@ func mainFunc() (result int) {
|
||||
case <-osSignals:
|
||||
utils.Info("Program got close signal.")
|
||||
|
||||
defaultMachine.Stop()
|
||||
os.Exit(-1)
|
||||
stopMachineAndExit(defaultMachine)
|
||||
}
|
||||
|
||||
}()
|
||||
@@ -375,7 +393,7 @@ func mainFunc() (result int) {
|
||||
|
||||
utils.Info("Program got close signal.")
|
||||
|
||||
defaultMachine.Stop()
|
||||
stopMachineAndExit(defaultMachine)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func init() {
|
||||
for i, l := range lines {
|
||||
if strings.HasPrefix(l, "IPv4 Route Table") {
|
||||
if i < len(lines)-3 && strings.HasPrefix(lines[i+3], "Network") {
|
||||
str := utils.StandardizeSpaces(lines[i+5])
|
||||
str := utils.StandardizeSpaces(lines[i+4])
|
||||
fields := strings.Split(str, " ")
|
||||
|
||||
if len(fields) > 3 {
|
||||
|
||||
Reference in New Issue
Block a user