From 227c751abce707766286093884868b1b63288ff3 Mon Sep 17 00:00:00 2001 From: kiyon Date: Fri, 17 Jul 2020 06:51:57 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=BC=20separate=20different=20logic=20o?= =?UTF-8?q?n=20each=20OS=20for=20child=20process=20to=20watch=20if=20maste?= =?UTF-8?q?r=20process=20exits?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prefork.go | 12 +++--------- prefork_dummy.go | 13 ------------- prefork_dummy_windows.go | 11 ----------- prefork_test.go | 2 +- prefork_utils.go | 28 ++++++++++++++++++++++++++++ prefork_utils_windows.go | 22 ++++++++++++++++++++++ 6 files changed, 54 insertions(+), 34 deletions(-) delete mode 100644 prefork_dummy.go delete mode 100644 prefork_dummy_windows.go create mode 100644 prefork_utils.go create mode 100644 prefork_utils_windows.go diff --git a/prefork.go b/prefork.go index cf4bb1c9..5a62a006 100644 --- a/prefork.go +++ b/prefork.go @@ -47,15 +47,9 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) { ln = tls.NewListener(ln, tlsconfig[0]) } - // kill child proc when master exits - go func() { - p, err := os.FindProcess(os.Getppid()) - if err == nil { - _, _ = p.Wait() - } else { - os.Exit(1) - } - }() + // kill current child proc when master exits + go watchMaster() + // listen for incoming connections return app.server.Serve(ln) } diff --git a/prefork_dummy.go b/prefork_dummy.go deleted file mode 100644 index afaaac4e..00000000 --- a/prefork_dummy.go +++ /dev/null @@ -1,13 +0,0 @@ -// +build !windows - -package fiber - -import ( - "os/exec" -) - -var dummyChildCmd = "go" - -func dummyCmd() *exec.Cmd { - return exec.Command(dummyChildCmd, "version") -} diff --git a/prefork_dummy_windows.go b/prefork_dummy_windows.go deleted file mode 100644 index 6a13bb60..00000000 --- a/prefork_dummy_windows.go +++ /dev/null @@ -1,11 +0,0 @@ -package fiber - -import ( - "os/exec" -) - -var dummyChildCmd = "go" - -func dummyCmd() *exec.Cmd { - return exec.Command("cmd", "/C", dummyChildCmd, "version") -} diff --git a/prefork_test.go b/prefork_test.go index 02abf4a3..078d3624 100644 --- a/prefork_test.go +++ b/prefork_test.go @@ -42,7 +42,7 @@ func Test_App_Prefork_Child_Process(t *testing.T) { utils.AssertEqual(t, nil, app.prefork("127.0.0.1:", config)) } -func Test_App_Prefork_Main_Process(t *testing.T) { +func Test_App_Prefork_Master_Process(t *testing.T) { testPreforkMaster = true app := New() diff --git a/prefork_utils.go b/prefork_utils.go new file mode 100644 index 00000000..0e44e0c8 --- /dev/null +++ b/prefork_utils.go @@ -0,0 +1,28 @@ +// +build !windows + +package fiber + +import ( + "os" + "os/exec" + "time" +) + +var ( + dummyChildCmd = "go" +) + +func dummyCmd() *exec.Cmd { + return exec.Command(dummyChildCmd, "version") +} + +// watchMaster gets ppid regularly, +// if it is equal to 1 (init process ID), +// it indicates that the master process has exited +func watchMaster() { + for range time.NewTicker(time.Millisecond * 500).C { + if os.Getppid() == 1 { + os.Exit(1) + } + } +} diff --git a/prefork_utils_windows.go b/prefork_utils_windows.go new file mode 100644 index 00000000..66af4c4f --- /dev/null +++ b/prefork_utils_windows.go @@ -0,0 +1,22 @@ +package fiber + +import ( + "os" + "os/exec" +) + +var dummyChildCmd = "go" + +func dummyCmd() *exec.Cmd { + return exec.Command("cmd", "/C", dummyChildCmd, "version") +} + +// watchMaster finds parent process, +// and waits for it to exit +func watchMaster() { + p, err := os.FindProcess(os.Getppid()) + if err == nil { + _, _ = p.Wait() + } + os.Exit(1) +}