@@ -195,18 +195,22 @@ type clientStream struct {
195
195
}
196
196
197
197
// awaitRequestCancel runs in its own goroutine and waits for the user
198
- // to either cancel a RoundTrip request (using the provided
199
- // Request.Cancel channel), or for the request to be done (any way it
200
- // might be removed from the cc.streams map: peer reset, successful
201
- // completion, TCP connection breakage, etc)
202
- func (cs * clientStream ) awaitRequestCancel (cancel <- chan struct {}) {
203
- if cancel == nil {
198
+ // to cancel a RoundTrip request, its context to expire, or for the
199
+ // request to be done (any way it might be removed from the cc.streams
200
+ // map: peer reset, successful completion, TCP connection breakage,
201
+ // etc)
202
+ func (cs * clientStream ) awaitRequestCancel (req * http.Request ) {
203
+ ctx := reqContext (req )
204
+ if req .Cancel == nil && ctx .Done () == nil {
204
205
return
205
206
}
206
207
select {
207
- case <- cancel :
208
+ case <- req . Cancel :
208
209
cs .bufPipe .CloseWithError (errRequestCanceled )
209
210
cs .cc .writeStreamReset (cs .ID , ErrCodeCancel , nil )
211
+ case <- ctx .Done ():
212
+ cs .bufPipe .CloseWithError (ctx .Err ())
213
+ cs .cc .writeStreamReset (cs .ID , ErrCodeCancel , nil )
210
214
case <- cs .done :
211
215
}
212
216
}
@@ -684,6 +688,7 @@ func (cc *ClientConn) RoundTrip(req *http.Request) (*http.Response, error) {
684
688
685
689
readLoopResCh := cs .resc
686
690
bodyWritten := false
691
+ ctx := reqContext (req )
687
692
688
693
for {
689
694
select {
@@ -716,6 +721,14 @@ func (cc *ClientConn) RoundTrip(req *http.Request) (*http.Response, error) {
716
721
cs .abortRequestBodyWrite (errStopReqBodyWriteAndCancel )
717
722
}
718
723
return nil , errTimeout
724
+ case <- ctx .Done ():
725
+ cc .forgetStreamID (cs .ID )
726
+ if ! hasBody || bodyWritten {
727
+ cc .writeStreamReset (cs .ID , ErrCodeCancel , nil )
728
+ } else {
729
+ cs .abortRequestBodyWrite (errStopReqBodyWriteAndCancel )
730
+ }
731
+ return nil , ctx .Err ()
719
732
case <- req .Cancel :
720
733
cc .forgetStreamID (cs .ID )
721
734
if ! hasBody || bodyWritten {
@@ -1284,13 +1297,14 @@ func (rl *clientConnReadLoop) handleResponse(cs *clientStream, f *MetaHeadersFra
1284
1297
cs .bufPipe = pipe {b : buf }
1285
1298
cs .bytesRemain = res .ContentLength
1286
1299
res .Body = transportResponseBody {cs }
1287
- go cs .awaitRequestCancel (cs .req . Cancel )
1300
+ go cs .awaitRequestCancel (cs .req )
1288
1301
1289
1302
if cs .requestedGzip && res .Header .Get ("Content-Encoding" ) == "gzip" {
1290
1303
res .Header .Del ("Content-Encoding" )
1291
1304
res .Header .Del ("Content-Length" )
1292
1305
res .ContentLength = - 1
1293
1306
res .Body = & gzipReader {body : res .Body }
1307
+ setResponseUncompressed (res )
1294
1308
}
1295
1309
return res , nil
1296
1310
}
0 commit comments