博客
关于我
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/

你可能感兴趣的文章
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‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>