hotgo/hotgo-server/app/utils/signal_util.go
2022-02-25 17:11:17 +08:00

77 lines
1.5 KiB
Go
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2022 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package utils
import (
"sync"
)
// 信号类
var Signal = new(signal)
type signal struct{}
type StopSignal int32
type exitWait struct {
mutex sync.Mutex
wg *sync.WaitGroup
deferFuns []func()
stopSignList []chan StopSignal
}
var exitWaitHandler *exitWait
func init() {
exitWaitHandler = &exitWait{
wg: &sync.WaitGroup{},
}
}
//
//  @Title  退出后等待处理完成
//  @Description 
//  @Author  Ms <133814250@qq.com>
//  @Param   doFun
//
func (util *signal) ExitWaitFunDo(doFun func()) {
exitWaitHandler.wg.Add(1)
defer exitWaitHandler.wg.Done()
if doFun != nil {
doFun()
}
}
//
//  @Title  应用退出后置操作
//  @Description 
//  @Author  Ms <133814250@qq.com>
//  @Param   deferFun
//
func (util *signal) AppDefer(deferFun ...func()) {
exitWaitHandler.mutex.Lock()
defer exitWaitHandler.mutex.Unlock()
for _, funcItem := range deferFun {
if funcItem != nil {
exitWaitHandler.deferFuns = append(exitWaitHandler.deferFuns, funcItem)
}
}
}
//
//  @Title  订阅app退出信号
//  @Description 
//  @Author  Ms <133814250@qq.com>
//  @Param   stopSig
//
func (util *signal) ListenStop(stopSig chan StopSignal) {
exitWaitHandler.mutex.Lock()
defer exitWaitHandler.mutex.Unlock()
exitWaitHandler.stopSignList = append(exitWaitHandler.stopSignList, stopSig)
}