当前位置:网站首页>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")
运行结果:

边栏推荐
- CWE4.8 -- 2022年危害最大的25种软件安全问题
- STM32——软件SPI控制AD7705[通俗易懂]
- 行业案例 | 全面防护 赛宁助力能源工控安全建设
- ASM外部冗余是否可以替换磁盘
- Hybrid brain-computer interface system based on steady-state visual evoked potentials and attentional EEG
- CentOS7 安装MySQL 图文详细教程
- 深入浅出边缘云 | 4. 生命周期管理
- 2022年最新重庆建筑安全员模拟题库及答案
- 基于生物激励神经网络的室内实时激光SLAM控制方法
- [RPI]树莓派监控温度及报警关机保护「建议收藏」
猜你喜欢

How does the SAP ABAP OData service support the $filter (filter) operation trial version
尚硅谷–MySQL–基础篇(P1~P95)

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

IDEA版Postman插件Restful Fast Request,细节到位,功能好用

ECCV2022:在Transformer上进行递归,不增参数,计算量还少!

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

PyQt5快速开发与实战10.2 复利计算 && 10.3 刷新博客点击量

跨境电商小知识之跨境电商物流定义以及方式讲解

IDEA找不到Database解决方法

PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer
随机推荐
IDEA找不到Database解决方法
SAP message TK 248 solved
Hard disk partition, expand disk C, no reshipment system, not heavy D dish of software full tutorial.
TensorRT安装及使用教程「建议收藏」
Use docker to build mysql master-slave
【CPU设计实战】简单流水线CPU设计
NameNode (NN) and SecondaryNameNode (2NN) working mechanism
求一份常见Oracle故障模拟场景
networkx绘制度分布
Two methods of NameNode failure handling
SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
SAP message TK 248 solved
基于verilog的CRC校验(汇总)
NameNode故障处理的两种方法
[CPU Design Practice] Simple Pipeline CPU Design
SAP e-commerce cloud Spartacus SSR Optimization Engine execution sequence of several timeouts
带有对称约束切换线性系统的结构可控性
PyQt5 rapid development and actual combat 10.1 Get city weather forecast
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
硬盘分区,拓展C盘,不重装系统,不重装D盘软件的全教程。