当gin框架部署以及运行的时候,如果瞬间重启或关闭,可能会造成其中的进程或者函数执行不完整,形成脏数据,导致系统个别功能运行异常。因此优雅的重启以及关闭是非常有必要的
原理:关闭http server后延迟几秒关闭系统,待各个函数运行完毕
此为https的请求,调用ListenAndServeTLS
并且加入证书位置来开启tls。
如果为http则只需改为ListenAndServe
即可
代码设置在main.go函数
//开启的端口号
//err = r.RunTLS(config.TLSConfig.Addr, config.TLSConfig.CertFile, config.TLSConfig.KeyFile)
//if err != nil {
// panic(err)
//}
srv := &http.Server{
Addr: config.TLSConfig.Addr,
Handler: r,
}
go func() {
// 基于https的服务连接开启
if err := srv.ListenAndServeTLS(config.TLSConfig.CertFile, config.TLSConfig.KeyFile) ; err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 3 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println(time.Now().Format("2006-01-02 15:04:05")+"Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
// catching ctx.Done(). timeout of 3 seconds.
select {
case <-ctx.Done():
log.Println("timeout of 3 seconds.")
}
log.Println(time.Now().Format("2006-01-02 15:04:05")+" Server Done")
fmt.Println(time.Now().Format("2006-01-02 15:04:05")+" Server Done")
运行结果:
优化
感谢 weixin_33870209 老哥的建议
srv := &http.Server{
Addr: config.TLSConfig.Addr,
Handler: r,
}
go func() {
// 基于https的服务连接开启
if err := srv.ListenAndServeTLS(config.TLSConfig.CertFile, config.TLSConfig.KeyFile); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 3 seconds.
quit := make(chan os.Signal, 1)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
//log.Println(time.Now().Format("2006-01-02 15:04:05")+"Shutdown Server ...")
quickExit := make(chan struct{},1)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}else {
quickExit <- struct{}{}
}
// catching ctx.Done(). timeout of 3 seconds.
select {
case <-ctx.Done():
//log.Println("timeout of 3 seconds.")
case <- quickExit:
}
//log.Println(time.Now().Format("2006-01-02 15:04:05")+" Server Done")
fmt.Println(time.Now().Format("2006-01-02 15:04:05") + " Server Done")