优雅关闭
优雅关闭会让正在处理的请求在进程退出前完成。最简单的方法是把可取消的上下文传给
StartConfig.Start,并设置 GracefulTimeout。当上下文被中断信号取消时,Echo 会停止接受新连接,
并最多等待到超时时间,让活跃请求完成。
package main
import ( "context" "errors" "net/http" "os" "os/signal" "syscall" "time"
"github.com/labstack/echo/v5")
func main() { // Setup e := echo.New() e.GET("/", func(c *echo.Context) error { time.Sleep(5 * time.Second) return c.JSON(http.StatusOK, "OK") })
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop()
sc := echo.StartConfig{ Address: ":1323", GracefulTimeout: 5 * time.Second, } if err := sc.Start(ctx, e); err != nil { e.Logger.Error("failed to start server", "error", err) }}使用自定义 HTTP 服务器
Section titled “使用自定义 HTTP 服务器”如果你自己管理 http.Server,请在 goroutine 中启动它,等待 signal context,
然后带超时调用 Shutdown:
func mainWithHTTPServer() { // Setup e := echo.New() e.GET("/", func(c *echo.Context) error { time.Sleep(5 * time.Second) return c.JSON(http.StatusOK, "OK") })
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop()
s := http.Server{Addr: ":1323", Handler: e} // Start server go func() { if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { e.Logger.Error("failed to start server", "error", err) } }()
// Wait for interrupt signal to gracefully shut down the server with a timeout of 10 seconds. <-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := s.Shutdown(ctx); err != nil { e.Logger.Error("failed to stop server", "error", err) }}