From eb6e4faeb59b73445de3676f75ca428a6c2abde6 Mon Sep 17 00:00:00 2001 From: pyihe <785131182@qq.com> Date: Wed, 10 Aug 2022 17:17:04 +0800 Subject: [PATCH] feature(https): add timeout --- https/http_api/http.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/https/http_api/http.go b/https/http_api/http.go index df6600d..3ea1261 100644 --- a/https/http_api/http.go +++ b/https/http_api/http.go @@ -3,6 +3,7 @@ package http_api import ( "context" "net/http" + "time" "github.com/gin-gonic/gin" "github.com/pyihe/go-pkg/errors" @@ -85,7 +86,10 @@ func (s *HttpServer) AddHandler(handler APIHandler) { } func (s *HttpServer) Stop() error { - if err := s.server.Shutdown(context.Background()); err != nil { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if err := s.server.Shutdown(ctx); err != nil { return err } s.wg.Wait()