本文共 1504 字,大约阅读时间需要 5 分钟。
作为一个web服务,升级操作是一个必不可少的过程,但是在升级之前还有个操作,那就是:退出服务(关机),当然我们可以暴力的终止程序然后启动新服务,但是这是基于业务不敏感的情况下,正常我们需要让用户的一次请求完成之后才终止程序,幸运的是Go在1.8+上面增加shutdown方法可以很简单的实现此过程。
先说明一下Shutdown工作的机制:当程序检测到中断信号时,我们调用http.server种的shutdown方法,该方法将阻止新的请求进来,同时保持当前的连接,知道当前连接完成则终止程序!
代码如下:
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/