Teach http client to abort channel streaming on context cancellation
When the response includes the X-Chunked-Output header, we treat that as channel output, and fire up a goroutine to decode the chunks. This routine need to look for context cancellation so that it can exit cleanly.
Tor Arne Vestbø committed
Apr 17, 2015 at 18:23 UTC
cc45e21e4c1040c7b1bc78f9ae84da3ce043ba8f
1 file changed
+10
commands/http/client.go
+10
@@ -181,6 +181,8 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
181
dec := json.NewDecoder(httpRes.Body)
182
outputType := reflect.TypeOf(req.Command().Type)
183
184
+ ctx := req.Context().Context
185
+
186
for {
187
var v interface{}
188
var err error
@@ -194,6 +196,14 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
196
fmt.Println(err.Error())
197
return
198
}
199
+
200
+ select {
201
+ case <-ctx.Done():
202
+ close(outChan)
203
+ return
204
+ default:
205
+ }
206
+
207
if err == io.EOF {
208
close(outChan)
209
return