@@ -185,13 +185,13 @@ func (s *Server) buildHttpHandler(target *HttpClientConf) http.HandlerFunc {
185
185
return
186
186
}
187
187
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
188
190
if target .Timeout > 0 {
189
191
timeout := time .Duration (target .Timeout ) * time .Millisecond
190
192
ctx , cancel := context .WithTimeout (r .Context (), timeout )
191
193
defer cancel ()
192
194
req = req .WithContext (ctx )
193
- } else {
194
- req = req .WithContext (r .Context ())
195
195
}
196
196
197
197
resp , err := httpc .DoRequest (req )
@@ -278,7 +278,7 @@ func buildRequestWithNewTarget(r *http.Request, target *HttpClientConf) (*http.R
278
278
}
279
279
}
280
280
281
- return & http.Request {
281
+ newReq := & http.Request {
282
282
Method : r .Method ,
283
283
URL : & u ,
284
284
Header : r .Header .Clone (),
@@ -287,7 +287,10 @@ func buildRequestWithNewTarget(r *http.Request, target *HttpClientConf) (*http.R
287
287
ProtoMinor : r .ProtoMinor ,
288
288
ContentLength : r .ContentLength ,
289
289
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
291
294
}
292
295
293
296
func createDescriptorSource (cli zrpc.Client , up Upstream ) (grpcurl.DescriptorSource , error ) {
0 commit comments