Go进阶训练营
2021-11-25
1分钟阅读时长
// https://grisha.org/blog/2014/06/03/graceful-restart-in-golang/
// https://github.com/douglarek/zerodown/blob/master/zerodown.go
// https://github.com/fvbock/endless/blob/master/endless.go
f, err := g.listener.(*net.TCPListener).File()
if err != nil {
g.err = err
return g
}
defer f.Close()
var args []string
if len(os.Args) > 1 {
args = append(args, os.Args[1:]...)
}
cmd := exec.Command(os.Args[0], args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), graceEnv)
cmd.ExtraFiles = []*os.File{f}
g.err = cmd.Start()
// https://github.com/Scalingo/go-graceful-restart-example/blob/master/ping.go
os.Setenv("_GRACEFUL_RESTART", "true")
execSpec := &syscall.ProcAttr{
Env: os.Environ(),
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd(), listenerFD},
}
// Fork exec the new version of your server
fork, err := syscall.ForkExec(os.Args[0], os.Args, execSpec)
process, err := os.StartProcess(exePath, os.Args, &os.ProcAttr{
Env: newEnvs,
Files: files,
})