1 Star 0 Fork 0

xiaofangjian/gopkg

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
grace.go 631 Bytes
一键复制 编辑 原始数据 按行查看 历史
kzzhr 提交于 2022-04-21 22:01 +08:00 . grace
package grace
import (
"context"
"os"
"os/signal"
"sync"
)
type Grace struct {
process context.Context
cancel func()
wg sync.WaitGroup
}
func New() *Grace {
c, cancelFunc := context.WithCancel(context.Background())
g := &Grace{
process: c,
cancel: cancelFunc,
}
return g
}
func (g *Grace) Notify(signals ...os.Signal) {
g.process, g.cancel = signal.NotifyContext(g.process, signals...)
}
func (g *Grace) Start(f func(context.Context)) {
g.wg.Add(1)
go func() {
defer g.wg.Done()
f(g.process)
}()
}
func (g *Grace) StopAndWait() {
g.cancel()
g.Wait()
}
func (g *Grace) Wait() {
g.wg.Wait()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xiaofangjian/gopkg.git
git@gitee.com:xiaofangjian/gopkg.git
xiaofangjian
gopkg
gopkg
e3c99588edf8

搜索帮助