1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05: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>
This commit is contained in:
David Calavera 2015-04-20 14:18:14 -07:00
parent ae4063585e
commit 3b05005a12
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") { if version.GreaterThanOrEqualTo("1.8") {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
buildConfig.JSONFormat = true buildConfig.JSONFormat = true
@ -1304,7 +1301,8 @@ func (s *Server) postBuild(eng *engine.Engine, version version.Version, w http.R
buildConfig.Pull = true buildConfig.Pull = true
} }
buildConfig.Stdout = stdout output := utils.NewWriteFlusher(w)
buildConfig.Stdout = output
buildConfig.Context = r.Body buildConfig.Context = r.Body
buildConfig.RemoteURL = r.FormValue("remote") 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 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 return err
} }
sf := streamformatter.NewStreamFormatter(version.GreaterThanOrEqualTo("1.8")) sf := streamformatter.NewStreamFormatter(version.GreaterThanOrEqualTo("1.8"))

View file

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