- 授权协议: GPL
- 开发语言: Google Go
- 操作系统: 跨平台
- 软件首页: https://github.com/zhangpeihao/shutdown
- 软件文档: https://godoc.org/github.com/zhangpeihao/shutdown
软件介绍
Go 语言在 1.7 版本中增加了 Context 库,优雅退出的实现有了一个统一的实现方式。
Go 语言 1.8 版本中 HTTP 服务使用 Context 实现了优雅退出。
使用此库可以方便地实现优雅退出:
// Generate a new context
ctx := NewContext()
// Run service with this context
go func(ctx context.Context) {
if err := ExitWaitGroupAdd(ctx, 1); err != nil {
return
}
defer ExitWaitGroupDone(ctx)
otherEvent := make(chan struct{})
FOR_LOOP:
for {
select {
case <-ctx.Done():
break FOR_LOOP
case <-otherEvent:
// ...
}
}
// Some close processes
}(ctx)
// Wait interrupt signal
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt)
<-sig
if err := Shutdown(ctx, time.Second*5); err != nil {
log.Println("Shutdown error:", err)
return
}
