mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
chore: refactor gateway http context (#4636)
This commit is contained in:
@@ -185,13 +185,13 @@ func (s *Server) buildHttpHandler(target *HttpClientConf) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
if target.Timeout > 0 {
|
||||||
timeout := time.Duration(target.Timeout) * time.Millisecond
|
timeout := time.Duration(target.Timeout) * time.Millisecond
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), timeout)
|
ctx, cancel := context.WithTimeout(r.Context(), timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
} else {
|
|
||||||
req = req.WithContext(r.Context())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := httpc.DoRequest(req)
|
resp, err := httpc.DoRequest(req)
|
||||||
@@ -278,7 +278,7 @@ func buildRequestWithNewTarget(r *http.Request, target *HttpClientConf) (*http.R
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &http.Request{
|
newReq := &http.Request{
|
||||||
Method: r.Method,
|
Method: r.Method,
|
||||||
URL: &u,
|
URL: &u,
|
||||||
Header: r.Header.Clone(),
|
Header: r.Header.Clone(),
|
||||||
@@ -287,7 +287,10 @@ func buildRequestWithNewTarget(r *http.Request, target *HttpClientConf) (*http.R
|
|||||||
ProtoMinor: r.ProtoMinor,
|
ProtoMinor: r.ProtoMinor,
|
||||||
ContentLength: r.ContentLength,
|
ContentLength: r.ContentLength,
|
||||||
Body: io.NopCloser(r.Body),
|
Body: io.NopCloser(r.Body),
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
// make sure the context is passed to the new request
|
||||||
|
return newReq.WithContext(r.Context()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createDescriptorSource(cli zrpc.Client, up Upstream) (grpcurl.DescriptorSource, error) {
|
func createDescriptorSource(cli zrpc.Client, up Upstream) (grpcurl.DescriptorSource, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user