当前位置:网站首页>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")
运行结果:
边栏推荐
- NameNode故障处理的两种方法
- Two methods of NameNode failure handling
- Use IN List Population in Your JDBC Application to Avoid Cursor Cache Contention Issues
- Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
- 模拟量差分和单端(iou计算方法)
- 函数的参数
- C#中+=的用法
- [Shader] Shader official example [easy to understand]
- NameNode (NN) and SecondaryNameNode (2NN) working mechanism
- 五种数据提交方式的优化
猜你喜欢
随机推荐
0X7FFFFFFF,0X80000000「建议收藏」
求一份常见Oracle故障模拟场景
matlab as(assert dominance)
列表页优化思路
sqlalchemy 判断一个array 类型的字段是否和一个array有至少一个一致的数据
深入浅出边缘云 | 4. 生命周期管理
【OpenCV】-边缘检测汇总示例
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
IDEA找不到Database解决方法
[CPU Design Practice] Simple Pipeline CPU Design
小试牛刀—猜数字游戏
Use IN List Population in Your JDBC Application to Avoid Cursor Cache Contention Issues
战略进攻能力的重要性,要远远高于战略防守能力
networkx绘制度分布
Flutter键盘可见性
golang中使用泛型
NameNode故障处理的两种方法
C#使用NumericUpDown控件
基于verilog的CRC校验(汇总)
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)