From e141b8e0987c7ac990531903a5563f62bb6aad07 Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 8 Jan 2021 00:58:58 +0800 Subject: [PATCH] shutdown server gracefully when reveiving TERM signal --- net/ghttp/ghttp_server_admin_process.go | 13 +++++++++---- net/ghttp/ghttp_server_admin_unix.go | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/net/ghttp/ghttp_server_admin_process.go b/net/ghttp/ghttp_server_admin_process.go index 18299e8b6..577a0168d 100644 --- a/net/ghttp/ghttp_server_admin_process.go +++ b/net/ghttp/ghttp_server_admin_process.go @@ -1,4 +1,4 @@ -// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -233,8 +233,13 @@ func shutdownWebServers(signal ...string) { } } -// gracefulShutdownWebServers gracefully shuts down all servers. -func gracefulShutdownWebServers() { +// shutdownWebServersGracefully gracefully shuts down all servers. +func shutdownWebServersGracefully(signal ...string) { + if len(signal) > 0 { + glog.Printf("%d: server gracefully shutting down by signal: %s", gproc.Pid(), signal[0]) + } else { + glog.Printf("%d: server gracefully shutting down by api", gproc.Pid()) + } serverMapping.RLockFunc(func(m map[string]interface{}) { for _, v := range m { for _, s := range v.(*Server).servers { @@ -262,7 +267,7 @@ func handleProcessMessage() { if msg := gproc.Receive(adminGProcCommGroup); msg != nil { if bytes.EqualFold(msg.Data, []byte("exit")) { intlog.Printf("%d: process message: exit", gproc.Pid()) - gracefulShutdownWebServers() + shutdownWebServersGracefully() allDoneChan <- struct{}{} intlog.Printf("%d: process message: exit done", gproc.Pid()) return diff --git a/net/ghttp/ghttp_server_admin_unix.go b/net/ghttp/ghttp_server_admin_unix.go index 19fff62af..de96d89e5 100644 --- a/net/ghttp/ghttp_server_admin_unix.go +++ b/net/ghttp/ghttp_server_admin_unix.go @@ -1,4 +1,4 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright 2017 gf Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -35,14 +35,22 @@ func handleProcessSignal() { sig = <-procSignalChan intlog.Printf(`signal received: %s`, sig.String()) switch sig { - // Stop the servers. - case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGABRT: + // Shutdown the servers. + case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGABRT: shutdownWebServers(sig.String()) return + // Shutdown the servers gracefully. + // Especially from K8S when running server in POD. + case syscall.SIGTERM: + shutdownWebServersGracefully(sig.String()) + return + // Restart the servers. case syscall.SIGUSR1: - restartWebServers(sig.String()) + if err := restartWebServers(sig.String()); err != nil { + intlog.Error(err) + } return default: