Skip to content

Commit 651eabb

Browse files
authored
chore: refactor gateway http context (zeromicro#4636)
1 parent e6b4372 commit 651eabb

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Diff for: gateway/server.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ func (s *Server) buildHttpHandler(target *HttpClientConf) http.HandlerFunc {
185185
return
186186
}
187187

188+
// set the timeout if it's configured, take effect only if it's greater than 0
189+
// and less than the deadline of the original request
188190
if target.Timeout > 0 {
189191
timeout := time.Duration(target.Timeout) * time.Millisecond
190192
ctx, cancel := context.WithTimeout(r.Context(), timeout)
191193
defer cancel()
192194
req = req.WithContext(ctx)
193-
} else {
194-
req = req.WithContext(r.Context())
195195
}
196196

197197
resp, err := httpc.DoRequest(req)
@@ -278,7 +278,7 @@ func buildRequestWithNewTarget(r *http.Request, target *HttpClientConf) (*http.R
278278
}
279279
}
280280

281-
return &http.Request{
281+
newReq := &http.Request{
282282
Method: r.Method,
283283
URL: &u,
284284
Header: r.Header.Clone(),
@@ -287,7 +287,10 @@ func buildRequestWithNewTarget(r *http.Request, target *HttpClientConf) (*http.R
287287
ProtoMinor: r.ProtoMinor,
288288
ContentLength: r.ContentLength,
289289
Body: io.NopCloser(r.Body),
290-
}, nil
290+
}
291+
292+
// make sure the context is passed to the new request
293+
return newReq.WithContext(r.Context()), nil
291294
}
292295

293296
func createDescriptorSource(cli zrpc.Client, up Upstream) (grpcurl.DescriptorSource, error) {

0 commit comments

Comments
 (0)