golang-gin-优雅重启

当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")

运行结果:
在这里插入图片描述在这里插入图片描述

优化

感谢 weixin_33870209 老哥的建议
在这里插入图片描述

	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, 1)
	// 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 ...")

	quickExit := make(chan struct{},1)

	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil {
		log.Fatal("Server Shutdown:", err)
	}else {
		quickExit <- struct{}{}
	}
	// catching ctx.Done(). timeout of 3 seconds.
	select {
	case <-ctx.Done():
		//log.Println("timeout of 3 seconds.")
	case <- quickExit:
	}
	//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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值