diff --git a/g/net/ghttp/ghttp_server_comm.go b/g/net/ghttp/ghttp_server_comm.go index 0203a6892..67af23d84 100644 --- a/g/net/ghttp/ghttp_server_comm.go +++ b/g/net/ghttp/ghttp_server_comm.go @@ -26,9 +26,9 @@ const ( gMSG_HEARTBEAT = 60 gPROC_HEARTBEAT_INTERVAL = 1000 // (毫秒)进程间心跳间隔 - gPROC_HEARTBEAT_TIMEOUT = 3000 // (毫秒)进程间心跳超时时间,如果子进程在这段内没有接收到任何心跳,那么自动退出,防止可能出现的僵尸子进程 - gPROC_MULTI_CHILD_CLEAR_INTERVAL = 1000 // (毫秒)检测间隔,当存在多个子进程时(往往是重启间隔非常短且频繁造成),需要进行清理,最终留下一个最新的子进程 - gPROC_MULTI_CHILD_CLEAR_MIN_EXPIRE = 3000 // (毫秒)当多个子进程存在时,允许子进程进程至少运行的最小时间,超过该时间则清理 + gPROC_HEARTBEAT_TIMEOUT = 30000 // (毫秒)进程间心跳超时时间,如果子进程在这段内没有接收到任何心跳,那么自动退出,防止可能出现的僵尸子进程 + //gPROC_MULTI_CHILD_CLEAR_INTERVAL = 1000 // (毫秒)检测间隔,当存在多个子进程时(往往是重启间隔非常短且频繁造成),需要进行清理,最终留下一个最新的子进程 + //gPROC_MULTI_CHILD_CLEAR_MIN_EXPIRE = 30000 // (毫秒)当多个子进程存在时,允许子进程进程至少运行的最小时间,超过该时间则清理 ) // 进程信号量监听消息队列 @@ -44,7 +44,7 @@ func handleProcessMsgAndSignal() { go handleChildProcessHeartbeat() } else { go handleMainProcessHeartbeat() - go handleMainProcessChildClear() + //go handleMainProcessChildClear() } handleProcessMsg() } diff --git a/g/net/ghttp/ghttp_server_comm_child.go b/g/net/ghttp/ghttp_server_comm_child.go index e1150576c..f567dc7d2 100644 --- a/g/net/ghttp/ghttp_server_comm_child.go +++ b/g/net/ghttp/ghttp_server_comm_child.go @@ -79,6 +79,7 @@ func onCommChildShutdown(pid int, data []byte) { if runtime.GOOS != "windows" { shutdownWebServers() } + os.Exit(0) } // 更新上一次主进程主动与子进程通信的时间 diff --git a/g/net/ghttp/ghttp_server_comm_main.go b/g/net/ghttp/ghttp_server_comm_main.go index c92f9a880..ec0bb608d 100644 --- a/g/net/ghttp/ghttp_server_comm_main.go +++ b/g/net/ghttp/ghttp_server_comm_main.go @@ -11,8 +11,6 @@ package ghttp import ( "os" "time" - "gitee.com/johng/gf/g/os/glog" - "gitee.com/johng/gf/g/os/gproc" "gitee.com/johng/gf/g/os/gtime" "gitee.com/johng/gf/g/container/gmap" ) @@ -82,22 +80,22 @@ func handleMainProcessHeartbeat() { } // 清理多余的子进程 -func handleMainProcessChildClear() { - for { - time.Sleep(gPROC_MULTI_CHILD_CLEAR_INTERVAL*time.Millisecond) - if procManager.Size() > 1 { - minPid := 0 - minTime := int(gtime.Millisecond()) - for _, pid := range procManager.Pids() { - if t := procFirstTimeMap.Get(pid); t < minTime { - minPid = pid - minTime = t - } - } - if minPid > 0 && procUpdateTimeMap.Get(minPid) - procFirstTimeMap.Get(minPid) > gPROC_MULTI_CHILD_CLEAR_MIN_EXPIRE { - sendProcessMsg(minPid, gMSG_SHUTDOWN, nil) - glog.Printfln("%d: multi child occurred, shutdown %d", gproc.Pid(), minPid) - } - } - } -} \ No newline at end of file +//func handleMainProcessChildClear() { +// for { +// time.Sleep(gPROC_MULTI_CHILD_CLEAR_INTERVAL*time.Millisecond) +// if procManager.Size() > 1 { +// minPid := 0 +// minTime := int(gtime.Millisecond()) +// for _, pid := range procManager.Pids() { +// if t := procFirstTimeMap.Get(pid); t < minTime { +// minPid = pid +// minTime = t +// } +// } +// if minPid > 0 && procUpdateTimeMap.Get(minPid) - procFirstTimeMap.Get(minPid) > gPROC_MULTI_CHILD_CLEAR_MIN_EXPIRE { +// sendProcessMsg(minPid, gMSG_SHUTDOWN, nil) +// glog.Printfln("%d: multi child occurred, shutdown %d", gproc.Pid(), minPid) +// } +// } +// } +//} \ No newline at end of file diff --git a/g/net/ghttp/ghttp_server_graceful.go b/g/net/ghttp/ghttp_server_graceful.go index d6a6a9f94..d6d2160be 100644 --- a/g/net/ghttp/ghttp_server_graceful.go +++ b/g/net/ghttp/ghttp_server_graceful.go @@ -15,6 +15,7 @@ import ( "crypto/tls" "gitee.com/johng/gf/g/os/glog" "gitee.com/johng/gf/g/os/gproc" + "time" ) // 优雅的Web Server对象封装 @@ -155,10 +156,12 @@ func (s *gracefulServer) getNetListener(addr string) (net.Listener, error) { // 执行请求优雅关闭 func (s *gracefulServer) shutdown() { - if err := s.httpServer.Shutdown(context.Background()); err != nil { - //glog.Errorfln("%d: %s server [%s] shutdown error: %v", gproc.Pid(), s.getProto(), s.addr, err) + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second)) + defer cancel() + if err := s.httpServer.Shutdown(ctx); err != nil { + glog.Errorfln("%d: %s server [%s] shutdown error: %v", gproc.Pid(), s.getProto(), s.addr, err) } else { - //glog.Printfln("%d: %s server [%s] shutdown smoothly", gproc.Pid(), s.getProto(), s.addr) + glog.Printfln("%d: %s server [%s] shutdown smoothly", gproc.Pid(), s.getProto(), s.addr) s.shutdownChan <- true } }