当前位置:网站首页>golang-gin-优雅重启
golang-gin-优雅重启
2022-07-31 13:01:00 【SakuraKizuna】
当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")
运行结果:
边栏推荐
- Anaconda安装labelImg图像标注软件
- 报错:npm ERR code EPERM
- Centos7 install mysql5.7
- Double non-one into bytes!!Pure dry goods sharing
- PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer
- SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
- 0X7FFFFFFF,0X80000000「建议收藏」
- [CPU Design Practice] Simple Pipeline CPU Design
- 使用openssl命令生成证书和对应的私钥,私钥签名,公钥验签
- networkx绘制度分布
猜你喜欢
docker部署完mysql无法连接
基于verilog的CRC校验(汇总)
The 2nd activity of the TOGAF10 Standard Reading Club continues wonderfully, and the highlights will be reviewed!
IDEA连接MySQL数据库并执行SQL查询操作
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
PyQt5 rapid development and actual combat 10.1 Get city weather forecast
跨境电商小知识之跨境电商物流定义以及方式讲解
PyQt5快速开发与实战 9.7 UI层的自动化测试
C#控件 ToolStripProgressBar 用法
随机推荐
Cognitive-exercise rehabilitation medical robot application design
PyQt5 rapid development and actual combat 10.1 Get city weather forecast
CWE4.8 -- 2022年危害最大的25种软件安全问题
PyQt5快速开发与实战 10.1 获取城市天气预报
matlab as(assert dominance)
The 2nd activity of the TOGAF10 Standard Reading Club continues wonderfully, and the highlights will be reviewed!
C#中+=的用法
ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI
Use IN List Population in Your JDBC Application to Avoid Cursor Cache Contention Issues
CentOS7 —— yum安装mysql
这款悄然崛起的国产API接口管理工具,你一定要晓得
【OpenCV】-边缘检测汇总示例
JSP response对象简介说明
跨境电商小知识之跨境电商物流定义以及方式讲解
列表页优化思路
0X7FFFFFFF,0X80000000「建议收藏」
EasyMock日记1[通俗易懂]
基于生物激励神经网络的室内实时激光SLAM控制方法
基于去噪自编码器的故障隔离与识别方法
聊聊 SAP 产品 UI 上的消息显示机制