修正代码,程序退出添加超时机制

This commit is contained in:
e1732a364fed
2000-01-01 00:00:00 +00:00
parent 4bb9051308
commit e5e0d080a6
2 changed files with 23 additions and 5 deletions

View File

@@ -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
}

View File

@@ -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 {