github.com/torfuzx/docker@v1.8.1/pkg/ioutils/writeflusher.go (about) 1 package ioutils 2 3 import ( 4 "io" 5 "net/http" 6 "sync" 7 ) 8 9 type WriteFlusher struct { 10 sync.Mutex 11 w io.Writer 12 flusher http.Flusher 13 flushed bool 14 } 15 16 func (wf *WriteFlusher) Write(b []byte) (n int, err error) { 17 wf.Lock() 18 defer wf.Unlock() 19 n, err = wf.w.Write(b) 20 wf.flushed = true 21 wf.flusher.Flush() 22 return n, err 23 } 24 25 // Flush the stream immediately. 26 func (wf *WriteFlusher) Flush() { 27 wf.Lock() 28 defer wf.Unlock() 29 wf.flushed = true 30 wf.flusher.Flush() 31 } 32 33 func (wf *WriteFlusher) Flushed() bool { 34 wf.Lock() 35 defer wf.Unlock() 36 return wf.flushed 37 } 38 39 func NewWriteFlusher(w io.Writer) *WriteFlusher { 40 var flusher http.Flusher 41 if f, ok := w.(http.Flusher); ok { 42 flusher = f 43 } else { 44 flusher = &NopFlusher{} 45 } 46 return &WriteFlusher{w: w, flusher: flusher} 47 }