博客
关于我
Golang 平滑重启之优雅关机
阅读量:641 次
发布时间:2019-03-13

本文共 1504 字,大约阅读时间需要 5 分钟。

前言

作为一个web服务,升级操作是一个必不可少的过程,但是在升级之前还有个操作,那就是:退出服务(关机),当然我们可以暴力的终止程序然后启动新服务,但是这是基于业务不敏感的情况下,正常我们需要让用户的一次请求完成之后才终止程序,幸运的是Go在1.8+上面增加shutdown方法可以很简单的实现此过程。


一、shutdown机制

先说明一下Shutdown工作的机制:当程序检测到中断信号时,我们调用http.server种的shutdown方法,该方法将阻止新的请求进来,同时保持当前的连接,知道当前连接完成则终止程序!

二、使用步骤

通过Gin提供的一个案例来解释说明

代码如下:

var i = 0func main() {   	router := gin.Default()	//创建两个接口,一个延迟9秒钟返回信息	router.GET("/", func(c *gin.Context) {   		time.Sleep(9 * time.Second)		i++		c.JSON(http.StatusOK,gin.H{   			"num":i,		})	})	//一个立刻返回信息	router.GET("/a", func(c *gin.Context) {   		i++		c.JSON(http.StatusOK,gin.H{   			"num":i,		})	})	srv := &http.Server{   		Addr:    ":8080",		Handler: router,	}	// Initializing the server in a goroutine so that	// it won't block the graceful shutdown handling below	go func() {   		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {   			log.Fatalf("listen: %s\n", err)		}	}()		//创建一个信号监听通道	quit := make(chan os.Signal, 1)	//监听 syscall.SIGINT 跟 syscall.SIGTERM信号	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)	si :=<-quit	log.Println("Shutting down server...",si)	//shutdown方法需要传入一个上下文参数,这里就设计到两种用法	//1.WithCancel带时间,表示接收到信号之后,过完该断时间不管当前请求是否完成,强制断开	ctx, cancel := context.WithTimeout(context.Background(), 9*time.Second)	//2.不带时间,表示等待当前请求全部完成再断开	ctx, cancel := context.WithCancel(context.Background())	defer cancel()	if err := srv.Shutdown(ctx); err != nil {   		//当请求还在的时候强制断开了连接将产生错误,err不为空		log.Fatal("Server forced to shutdown:", err)	}	log.Println("Server exiting")}

转载地址:http://dwyoz.baihongyu.com/

你可能感兴趣的文章
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>