Skip to content

Commit 4470826

Browse files
committedOct 21, 2015
fix streaming output to flush per write
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 795e242 commit 4470826

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed
 

‎commands/http/handler.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,45 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
245245
}
246246

247247
w.WriteHeader(status)
248-
_, err = io.Copy(w, out)
248+
err = flushCopy(w, out)
249249
if err != nil {
250250
log.Error("err: ", err)
251251
w.Header().Set(StreamErrHeader, sanitizedErrStr(err))
252252
}
253253
}
254254

255+
func flushCopy(w io.Writer, r io.Reader) error {
256+
buf := make([]byte, 4096)
257+
f, ok := w.(http.Flusher)
258+
if !ok {
259+
_, err := io.Copy(w, r)
260+
return err
261+
}
262+
for {
263+
n, err := r.Read(buf)
264+
switch err {
265+
case io.EOF:
266+
return nil
267+
case nil:
268+
// continue
269+
default:
270+
return err
271+
}
272+
273+
nw, err := w.Write(buf[:n])
274+
if err != nil {
275+
return err
276+
}
277+
278+
if nw != n {
279+
return fmt.Errorf("http write failed to write full amount: %d != %d", nw, n)
280+
}
281+
282+
f.Flush()
283+
}
284+
return nil
285+
}
286+
255287
func sanitizedErrStr(err error) string {
256288
s := err.Error()
257289
s = strings.Split(s, "\n")[0]

0 commit comments

Comments
 (0)
Please sign in to comment.