You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Go 信号通知通过在Channel上发送 os.Signal 信号值来工作。通过make(chan os.Signal)创建一个接收系统信号的通道,signal.Notify将创建的通道进行注册,让其能够接收到后面参数指定的类型的系统信号。
下面是一个通过监听SIGTERM信号,优雅关停 gRPC Server的例子:
funcmain() {
// ...errChan:=make(chanerror)
stopChan:=make(chan os.Signal)
// bind OS events to the signal channelsignal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT)
// run blocking call in a separate goroutine, report errors via channelgofunc() {
iferr:=grpcServer.Serve(lis); err!=nil {
errChan<-err
}
}()
// terminate your environment gracefully before leaving main functiondeferfunc() {
closeDbConnections()
}()
// block until either OS signal, or server fatal errorselect {
caseerr:=<-errChan:
log.Printf("Fatal error: %v\n", err)
case<-stopChan:
server.GracefulStop()
}
The text was updated successfully, but these errors were encountered:
有时,我们希望 Go 程序能够智能地处理 Unix 系统信号。例如,我们可能希望服务器在收到 SIGTERM 时做清理操作,处理完已接收的请求后正常关闭,或者让命令行工具在收到 SIGINT 信号时停止处理输入。下面会演示在 Go 程序里通过 channel中处理信号的方法。
Go 信号通知通过在Channel上发送 os.Signal 信号值来工作。通过
make(chan os.Signal)
创建一个接收系统信号的通道,signal.Notify
将创建的通道进行注册,让其能够接收到后面参数指定的类型的系统信号。下面是一个通过监听SIGTERM信号,优雅关停 gRPC Server的例子:
The text was updated successfully, but these errors were encountered: