// Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
// signal
package utils
import (
"os"
"os/signal"
"time"
)
const (
defaultServiceExitSeconds = 3
)
var onlyOneSignalHandler = make(chan struct{})
// SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned
// which is closed on one of these signals. If a second signal is caught, the program
// is terminated with exit code 1.
func SetupSignalHandler() (stopCh <-chan struct{}) {
close(onlyOneSignalHandler) // panics when called twice
stop := make(chan struct{})
c := make(chan os.Signal)
signal.Notify(c, shutdownSignals...)
go func() {
<-c
close(stop)
time.Sleep(defaultServiceExitSeconds * time.Second)
os.Exit(1) // second signal. Exit directly.
}()
return stop
}