当前位置:网站首页>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的请求,调用ListenAndServeTLSAnd 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")
运行结果:

边栏推荐
- 战略进攻能力的重要性,要远远高于战略防守能力
- 【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
- CentOS7 - yum install mysql
- 【CPU设计实战】简单流水线CPU设计
- JSP response对象简介说明
- The cluster of safe mode
- FastAPI 封装一个通用的response
- Exploring Plain Vision Transformer Backbones for Object Detection Paper Reading Notes
- 模拟量差分和单端(iou计算方法)
- ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
猜你喜欢

IDEA连接MySQL数据库并执行SQL查询操作

docker部署完mysql无法连接

EXCEL如何快速拆分合并单元格数据

系统集成项目管理工程师(软考中级)知识点总结【挣值分析】【关键路径】

小试牛刀—猜数字游戏

Invalid bound statement (not found)出现的原因和解决方法

Centos7 install mysql5.7 steps (graphical version)

C#控件ListView用法

365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal

深入浅出边缘云 | 4. 生命周期管理
随机推荐
基于verilog的CRC校验(汇总)
Google Chrome(谷歌浏览器)安装使用
Indoor real-time laser SLAM control method based on biological excitation neural network
PyQt5快速开发与实战 10.1 获取城市天气预报
查看Oracle数据库的用户名和密码
ECCV2022:在Transformer上进行递归,不增参数,计算量还少!
NameNode (NN) and SecondaryNameNode (2NN) working mechanism
alert(1) (haozi.me)靶场练习
IDEA的database使用教程(使用mysql数据库)
golang八股文整理(持续搬运)
PyQt5快速开发与实战10.2 复利计算 && 10.3 刷新博客点击量
Using SQL Server FOR XML and FOR JSON syntax on other RDBMSs with jOOQ
Three-Phase PWM Rectifier Predictive Direct Power Control
NameNode故障处理的两种方法
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
抓住金三银四的尾巴,解锁程序员面试《刷题神器》
Banyan Tree Loan GPU Hardware Architecture
IDEA如何运行web程序
WPF中TabControl动态获取当前选中的TabItem
Use docker to build mysql master-slave