Go 应用优雅重启的优雅实现
Go 1.8版本之后, http.Server 内置的 Shutdown() 方法就支持优雅地关机,关键需要监听系统的信号及 context
的处理。
监听中断
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGHUP, // kill -SIGHUP XXXX
syscall.SIGINT, // kill -SIGINT XXXX or Ctrl+c
syscall.SIGTERM, // kill -SIGTERM XXXX
syscall.SIGQUIT, // kill -SIGQUIT XXXX
)
<-signalChan
context 处理
ctx, cancel := context.WithCancel(context.Background())
httpServer := &http.Server{
Addr: ":8082",
Handler: mux,
BaseContext: func(_ net.Listener) context.Context { return ctx },
}
httpServer.RegisterOnShutdown(cancel)
完整例子
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"os"
"syscall"
"time"
"github.com/vardius/shutdown"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Hello!")
})
httpServer := &http.Server{
Addr: ":8080",
Handler: mux,
BaseContext: func(_ net.Listener) context.Context { return ctx },
}
stop := func() {
gracefulCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := httpServer.Shutdown(gracefulCtx); err != nil {
log.Printf("shutdown error: %v\n", err)
} else {
log.Printf("gracefully stopped\n")
}
}
// Run server
go func() {
if err := httpServer.ListenAndServe(); err != http.ErrServerClosed {
log.Printf("HTTP server ListenAndServe: %v", err)
os.Exit(1)
}
}()
shutdown.GracefulStop(stop) // will block until shutdown signal is received
}
0
See Also
- Go 的两个web 框架推荐
- Tetua: 又一个 Go CMS,界面简洁
- Go语言刷LeetCode集锦
- binpic: 一款Go语言实现的、基于任意文件生成图片的小工具
- 如果再开发个移动应用版,就更好了,android ios
Nearby
- 上一篇 › 请问网页下面的响应时间是如何添加的
- 下一篇 › pip 21.0 发布,完全停止支持 Python 2版本系列