You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/proc/shutdown.go

100 lines
2.3 KiB
Go

//go:build linux || darwin
4 years ago
package proc
import (
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/threading"
4 years ago
)
const (
wrapUpTime = time.Second
// why we use 5500 milliseconds is because most of our queue are blocking mode with 5 seconds
waitTime = 5500 * time.Millisecond
)
var (
wrapUpListeners = new(listenerManager)
shutdownListeners = new(listenerManager)
delayTimeBeforeForceQuit = waitTime
)
// AddShutdownListener adds fn as a shutdown listener.
// The returned func can be used to wait for fn getting called.
4 years ago
func AddShutdownListener(fn func()) (waitForCalled func()) {
return shutdownListeners.addListener(fn)
}
// AddWrapUpListener adds fn as a wrap up listener.
// The returned func can be used to wait for fn getting called.
4 years ago
func AddWrapUpListener(fn func()) (waitForCalled func()) {
return wrapUpListeners.addListener(fn)
}
// SetTimeToForceQuit sets the waiting time before force quitting.
4 years ago
func SetTimeToForceQuit(duration time.Duration) {
4 years ago
delayTimeBeforeForceQuit = duration
}
// Shutdown calls the registered shutdown listeners, only for test purpose.
func Shutdown() {
shutdownListeners.notifyListeners()
}
// WrapUp wraps up the process, only for test purpose.
func WrapUp() {
wrapUpListeners.notifyListeners()
}
4 years ago
func gracefulStop(signals chan os.Signal) {
signal.Stop(signals)
logx.Info("Got signal SIGTERM, shutting down...")
go wrapUpListeners.notifyListeners()
4 years ago
time.Sleep(wrapUpTime)
go shutdownListeners.notifyListeners()
4 years ago
time.Sleep(delayTimeBeforeForceQuit - wrapUpTime)
logx.Infof("Still alive after %v, going to force kill the process...", delayTimeBeforeForceQuit)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
}
type listenerManager struct {
lock sync.Mutex
waitGroup sync.WaitGroup
listeners []func()
}
func (lm *listenerManager) addListener(fn func()) (waitForCalled func()) {
lm.waitGroup.Add(1)
lm.lock.Lock()
lm.listeners = append(lm.listeners, func() {
defer lm.waitGroup.Done()
fn()
})
lm.lock.Unlock()
return func() {
lm.waitGroup.Wait()
}
}
func (lm *listenerManager) notifyListeners() {
lm.lock.Lock()
defer lm.lock.Unlock()
group := threading.NewRoutineGroup()
4 years ago
for _, listener := range lm.listeners {
group.RunSafe(listener)
4 years ago
}
group.Wait()
4 years ago
}