当前位置:网站首页>golang-gin - graceful restart
golang-gin - graceful restart
2022-07-31 13:08:00 【SakuraKizuna】
当ginWhen the framework deploys and runs,If instant restart or shutdown,It may cause incomplete execution of the process or function in it,形成脏数据,Causes individual functions of the system to operate abnormally.So graceful restarts and shutdowns are necessary
原理:关闭http serverShut down the system after a delay of several seconds,Wait for each function to finish running
此为https的请求,调用ListenAndServeTLS
And add the certificate location to enabletls.
如果为httpthen just change toListenAndServe
即可
The code is set atmain.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() {
// 基于httpsThe service connection is open
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")
运行结果:
边栏推荐
- Three-Phase PWM Rectifier Predictive Direct Power Control
- IDEA找不到Database解决方法
- 基本语法(二)
- 深入浅出边缘云 | 4. 生命周期管理
- Indoor real-time laser SLAM control method based on biological excitation neural network
- 小试牛刀—猜数字游戏
- 报错IDEA Terminated with exit code 1
- ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
- WPF中报错:“未将对象引用设置到对象的实例。”
- 中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
猜你喜欢
随机推荐
vivado里那些看不懂的原语
通过斐波那契数再谈函数递归2.0
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
NameNode故障处理的两种方法
模拟量差分和单端(iou计算方法)
立一个flag
Anaconda安装labelImg图像标注软件
Cognitive-exercise rehabilitation medical robot application design
AMBA APB学习记录(AMBA 2.0)
/run/NetworkManager占用空间过大
PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer
求一份常见Oracle故障模拟场景
聊聊 SAP 产品 UI 上的消息显示机制
[CPU Design Practice] Simple Pipeline CPU Design
SAP 电商云 Spartacus SSR Optimization Engine 几处 timeout 的执行顺序
函数的参数
Double non-one into bytes!!Pure dry goods sharing
使用openssl命令生成证书和对应的私钥,私钥签名,公钥验签
一文吃透哈希表
SAP message TK 248 solved