COOKBOOK(手册) - 平滑关闭

优质
小牛编辑
125浏览
2023-12-01

平滑关闭

使用 grace

server.go

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/facebookgo/grace/gracehttp"
  5. "github.com/labstack/echo"
  6. )
  7. func main() {
  8. // Setup
  9. e := echo.New()
  10. e.GET("/", func(c echo.Context) error {
  11. return c.String(http.StatusOK, "Six sick bricks tick")
  12. })
  13. e.Server.Addr = ":1323"
  14. // Serve it like a boss
  15. e.Logger.Fatal(gracehttp.Serve(e.Server))
  16. }

使用 graceful

server.go

  1. package main
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/labstack/echo"
  6. "github.com/tylerb/graceful"
  7. )
  8. func main() {
  9. // Setup
  10. e := echo.New()
  11. e.GET("/", func(c echo.Context) error {
  12. return c.String(http.StatusOK, "Sue sews rose on slow joe crows nose")
  13. })
  14. e.Server.Addr = ":1323"
  15. // Serve it like a boss
  16. graceful.ListenAndServe(e.Server, 5*time.Second)
  17. }