问题背景

httpkill -9
RSTconnection refused

这些问题会造成不好的客户体验,严重的甚至影响客户业务。所以,我们需要优雅的重启我们的应用。

解决问题 -- 优雅

原理

  1. 信号机制;
  2. 子进程继承父进程的资源;

支持功能

ServerHTTPHTTPS

使用指南

添加服务器

    // http
    srv1 := &http.Server{
        Addr:    ":80",
        Handler: sc,
    }
    gracehttp.AddServer(srv1, false, "", "")

    // https

    srv2 := &http.Server{
        Addr:    ":443",
        Handler: sc,
    }
    gracehttp.AddServer(srv2, true, "../config/https.crt", "../config/https.key")

    gracehttp.Run() // 此方法会阻塞,直到所有的 HTTP 服务退出
Servergracehttp.AddServer

添加自定义日志组件

    gracehttp.SetErrorLogCallback(logger.LogConfigLoadError)
Set*
  • SetInfoLogCallback
  • SetNoticeLogCallback
  • SetErrorLogCallback