2 回答
TA贡献1993条经验 获得超5个赞
首先,正如您所说,当服务器“崩溃”时执行任务是相当复杂的,因为崩溃可能意味着很多事情,并且当服务器中出现严重问题时,没有什么可以保证清理功能的执行。
从工程角度来看(如果在故障时将用户设置为离线如此重要),最好是在另一台服务器上有一个辅助服务,该服务接收用户连接和断开连接事件以及 ping 事件(如果它在某个时间段内没有收到任何更新)设置超时该服务会认为您的服务器已关闭并继续将每个用户设置为离线。
回到你的问题,使用 defer 和等待终止信号应该覆盖 99% 的情况。我对代码进行了注释以解释逻辑。
// AllUsersOffline is called when the program is terminated, it takes a *sync.Once to make sure this function is performed only
// one time, since it might be called from different goroutines.
func AllUsersOffline(once *sync.Once) {
once.Do(func() {
fmt.Print("setting all users offline...")
// logic to set all users offline
})
}
// CatchSigs catches termination signals and executes f function at the end
func CatchSigs(f func()) {
cSig := make(chan os.Signal, 1)
// watch for these signals
signal.Notify(cSig, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGHUP) // these are the termination signals in GNU => https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
// wait for them
sig := <- cSig
fmt.Printf("received signal: %s", sig)
// execute f
f()
}
func main() {
/* code */
// the once is used to make sure AllUsersOffline is performed ONE TIME.
usersOfflineOnce := &sync.Once{}
// catch termination signals
go CatchSigs(func() {
// when a termination signal is caught execute AllUsersOffline function
AllUsersOffline(usersOfflineOnce)
})
// deferred functions are called even in case of panic events, although execution is not to take for granted (OOM errors etc)
defer AllUsersOffline(usersOfflineOnce)
/* code */
// run server
err := server.Run()
if err != nil {
// error logic here
}
// bla bla bla
}
- 2 回答
- 0 关注
- 114 浏览
添加回答
举报