diff --git a/config/graceful_shutdown.go b/config/graceful_shutdown.go index 0b39d111503377df318b4cfbf6243126183735d9..fedb2c15ecdab62d17f0a4e83c45522f1c18acb0 100644 --- a/config/graceful_shutdown.go +++ b/config/graceful_shutdown.go @@ -21,7 +21,6 @@ import ( "os" "os/signal" "runtime/debug" - "syscall" "time" ) @@ -49,17 +48,15 @@ import ( * syscall.SIGEMT cannot be found in CI * It's seems that the Unix/Linux does not have the signal SIGSTKFLT. https://github.com/golang/go/issues/33381 * So this signal will be ignored. - * + * The signals are different on different platforms. + * We define them by using 'package build' feature https://golang.org/pkg/go/build/ */ func GracefulShutdownInit() { signals := make(chan os.Signal, 1) - signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGKILL, syscall.SIGSTOP, - syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP, - syscall.SIGABRT, syscall.SIGSYS, - ) + signal.Notify(signals, ShutdownSignals...) go func() { select { @@ -68,17 +65,18 @@ func GracefulShutdownInit() { // gracefulShutdownOnce.Do(func() { BeforeShutdown() - switch sig { // those signals' original behavior is exit with dump ths stack, so we try to keep the behavior - case syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP, - syscall.SIGABRT, syscall.SIGSYS: - debug.WriteHeapDump(os.Stdout.Fd()) - default: - time.AfterFunc(totalTimeout(), func() { - logger.Warn("Shutdown gracefully timeout, application will shutdown immediately. ") - os.Exit(0) - }) + for _, dumpSignal := range DumpHeapShutdownSignals { + if sig == dumpSignal { + debug.WriteHeapDump(os.Stdout.Fd()) + } } + + time.AfterFunc(totalTimeout(), func() { + logger.Warn("Shutdown gracefully timeout, application will shutdown immediately. ") + os.Exit(0) + }) + os.Exit(0) } }() diff --git a/config/graceful_shutdown_signal_unix.go b/config/graceful_shutdown_signal_unix.go new file mode 100644 index 0000000000000000000000000000000000000000..59c1a5d149c2e9db8e9ac981adec107cafc863ad --- /dev/null +++ b/config/graceful_shutdown_signal_unix.go @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config + +import ( + "os" + "syscall" +) + +var ShutdownSignals = []os.Signal{os.Interrupt, os.Kill, syscall.SIGKILL, syscall.SIGSTOP, + syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP, + syscall.SIGABRT, syscall.SIGSYS} + +var DumpHeapShutdownSignals = []os.Signal{syscall.SIGQUIT, syscall.SIGILL, + syscall.SIGTRAP, syscall.SIGABRT, syscall.SIGSYS} diff --git a/config/graceful_shutdown_signal_windows.go b/config/graceful_shutdown_signal_windows.go new file mode 100644 index 0000000000000000000000000000000000000000..91b2bce7c2311ecbe9a1255be3e7b7b357a9b403 --- /dev/null +++ b/config/graceful_shutdown_signal_windows.go @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config + +import ( + "os" + "syscall" +) + +var ShutdownSignals = []os.Signal{os.Interrupt, os.Kill, syscall.SIGKILL, + syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP, + syscall.SIGABRT} + +var DumpHeapShutdownSignals = []os.Signal{syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP, syscall.SIGABRT}