github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/signal/signal_posix.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 // +build linux darwin freebsd unix 14 15 package signal 16 17 import ( 18 "log" 19 "os" 20 "os/signal" 21 "runtime" 22 "syscall" 23 24 "github.com/whtcorpsinc/milevadb/soliton/logutil" 25 "go.uber.org/zap" 26 ) 27 28 // SetupSignalHandler setup signal handler for MilevaDB Server 29 func SetupSignalHandler(shutdownFunc func(bool)) { 30 usrDefSignalChan := make(chan os.Signal, 1) 31 32 signal.Notify(usrDefSignalChan, syscall.SIGUSR1) 33 go func() { 34 buf := make([]byte, 1<<16) 35 for { 36 sig := <-usrDefSignalChan 37 if sig == syscall.SIGUSR1 { 38 stackLen := runtime.Stack(buf, true) 39 log.Printf("\n=== Got signal [%s] to dump goroutine stack. ===\n%s\n=== Finished dumping goroutine stack. ===\n", sig, buf[:stackLen]) 40 } 41 } 42 }() 43 44 closeSignalChan := make(chan os.Signal, 1) 45 signal.Notify(closeSignalChan, 46 syscall.SIGHUP, 47 syscall.SIGINT, 48 syscall.SIGTERM, 49 syscall.SIGQUIT) 50 51 go func() { 52 sig := <-closeSignalChan 53 logutil.BgLogger().Info("got signal to exit", zap.Stringer("signal", sig)) 54 shutdownFunc(sig == syscall.SIGQUIT) 55 }() 56 }