Commit 4470826f authored by Jeromy's avatar Jeromy

fix streaming output to flush per write

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent 795e2422
......@@ -245,13 +245,45 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
}
w.WriteHeader(status)
_, err = io.Copy(w, out)
err = flushCopy(w, out)
if err != nil {
log.Error("err: ", err)
w.Header().Set(StreamErrHeader, sanitizedErrStr(err))
}
}
func flushCopy(w io.Writer, r io.Reader) error {
buf := make([]byte, 4096)
f, ok := w.(http.Flusher)
if !ok {
_, err := io.Copy(w, r)
return err
}
for {
n, err := r.Read(buf)
switch err {
case io.EOF:
return nil
case nil:
// continue
default:
return err
}
nw, err := w.Write(buf[:n])
if err != nil {
return err
}
if nw != n {
return fmt.Errorf("http write failed to write full amount: %d != %d", nw, n)
}
f.Flush()
}
return nil
}
func sanitizedErrStr(err error) string {
s := err.Error()
s = strings.Split(s, "\n")[0]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment