1
0
mirror of https://github.com/docker/cli.git synced 2026-01-13 18:22:35 +03:00

Add flusher check to utils.WriteFlusher.

That way we can know when the stream has been flushed.

Signed-off-by: David Calavera <david.calavera@gmail.com>
Upstream-commit: 3b05005a1262e53d042512e88c52a6dae0f2e93d
Component: engine
This commit is contained in:
David Calavera
2015-04-20 14:18:14 -07:00
parent 16569396d9
commit 1edadd0f1c
2 changed files with 14 additions and 5 deletions

View File

@@ -1285,9 +1285,6 @@ func (s *Server) postBuild(eng *engine.Engine, version version.Version, w http.R
}
}
stdout := engine.NewOutput()
stdout.Set(utils.NewWriteFlusher(w))
if version.GreaterThanOrEqualTo("1.8") {
w.Header().Set("Content-Type", "application/json")
buildConfig.JSONFormat = true
@@ -1304,7 +1301,8 @@ func (s *Server) postBuild(eng *engine.Engine, version version.Version, w http.R
buildConfig.Pull = true
}
buildConfig.Stdout = stdout
output := utils.NewWriteFlusher(w)
buildConfig.Stdout = output
buildConfig.Context = r.Body
buildConfig.RemoteURL = r.FormValue("remote")
@@ -1336,7 +1334,9 @@ func (s *Server) postBuild(eng *engine.Engine, version version.Version, w http.R
}
if err := builder.Build(s.daemon, eng, buildConfig); err != nil {
if !stdout.Used() {
// Do not write the error in the http output if it's still empty.
// This prevents from writing a 200(OK) when there is an interal error.
if !output.Flushed() {
return err
}
sf := streamformatter.NewStreamFormatter(version.GreaterThanOrEqualTo("1.8"))

View File

@@ -128,12 +128,14 @@ type WriteFlusher struct {
sync.Mutex
w io.Writer
flusher http.Flusher
flushed bool
}
func (wf *WriteFlusher) Write(b []byte) (n int, err error) {
wf.Lock()
defer wf.Unlock()
n, err = wf.w.Write(b)
wf.flushed = true
wf.flusher.Flush()
return n, err
}
@@ -142,9 +144,16 @@ func (wf *WriteFlusher) Write(b []byte) (n int, err error) {
func (wf *WriteFlusher) Flush() {
wf.Lock()
defer wf.Unlock()
wf.flushed = true
wf.flusher.Flush()
}
func (wf *WriteFlusher) Flushed() bool {
wf.Lock()
defer wf.Unlock()
return wf.flushed
}
func NewWriteFlusher(w io.Writer) *WriteFlusher {
var flusher http.Flusher
if f, ok := w.(http.Flusher); ok {