From 8342157271cbd3ebef5b122c6f575737e93a640a Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 8 Feb 2025 20:57:34 +0800 Subject: [PATCH] chore: refactor gateway http context --- gateway/server.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gateway/server.go b/gateway/server.go index e1af48cef253..77acd9a3eaa9 100644 --- a/gateway/server.go +++ b/gateway/server.go @@ -185,13 +185,15 @@ func (s *Server) buildHttpHandler(target *HttpClientConf) http.HandlerFunc { return } + // accept the context from the original request, including the deadline + req = req.WithContext(r.Context()) + // set the timeout if it's configured, take effect only if it's greater than 0 + // and less than the deadline of the original request if target.Timeout > 0 { timeout := time.Duration(target.Timeout) * time.Millisecond ctx, cancel := context.WithTimeout(r.Context(), timeout) defer cancel() req = req.WithContext(ctx) - } else { - req = req.WithContext(r.Context()) } resp, err := httpc.DoRequest(req)