2015-05-08 18:33:33 +00:00
|
|
|
package ioutils
|
|
|
|
|
|
|
|
import (
|
2015-11-03 00:11:28 +00:00
|
|
|
"errors"
|
2015-05-08 18:33:33 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2015-11-03 00:11:28 +00:00
|
|
|
// WriteFlusher wraps the Write and Flush operation ensuring that every write
|
|
|
|
// is a flush. In addition, the Close method can be called to intercept
|
|
|
|
// Read/Write calls if the targets lifecycle has already ended.
|
2015-05-08 18:33:33 +00:00
|
|
|
type WriteFlusher struct {
|
2015-11-03 00:11:28 +00:00
|
|
|
mu sync.Mutex
|
2015-05-08 18:33:33 +00:00
|
|
|
w io.Writer
|
|
|
|
flusher http.Flusher
|
|
|
|
flushed bool
|
2015-11-03 00:11:28 +00:00
|
|
|
closed error
|
|
|
|
|
|
|
|
// TODO(stevvooe): Use channel for closed instead, remove mutex. Using a
|
|
|
|
// channel will allow one to properly order the operations.
|
2015-05-08 18:33:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 00:11:28 +00:00
|
|
|
var errWriteFlusherClosed = errors.New("writeflusher: closed")
|
|
|
|
|
2015-05-08 18:33:33 +00:00
|
|
|
func (wf *WriteFlusher) Write(b []byte) (n int, err error) {
|
2015-11-03 00:11:28 +00:00
|
|
|
wf.mu.Lock()
|
|
|
|
defer wf.mu.Unlock()
|
|
|
|
if wf.closed != nil {
|
|
|
|
return 0, wf.closed
|
|
|
|
}
|
|
|
|
|
2015-05-08 18:33:33 +00:00
|
|
|
n, err = wf.w.Write(b)
|
2015-11-03 00:11:28 +00:00
|
|
|
wf.flush() // every write is a flush.
|
2015-05-08 18:33:33 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the stream immediately.
|
|
|
|
func (wf *WriteFlusher) Flush() {
|
2015-11-03 00:11:28 +00:00
|
|
|
wf.mu.Lock()
|
|
|
|
defer wf.mu.Unlock()
|
|
|
|
|
|
|
|
wf.flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush the stream immediately without taking a lock. Used internally.
|
|
|
|
func (wf *WriteFlusher) flush() {
|
|
|
|
if wf.closed != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-08 18:33:33 +00:00
|
|
|
wf.flushed = true
|
|
|
|
wf.flusher.Flush()
|
|
|
|
}
|
|
|
|
|
2015-08-03 01:45:05 +00:00
|
|
|
// Flushed returns the state of flushed.
|
|
|
|
// If it's flushed, return true, or else it return false.
|
2015-05-08 18:33:33 +00:00
|
|
|
func (wf *WriteFlusher) Flushed() bool {
|
2015-11-03 00:11:28 +00:00
|
|
|
// BUG(stevvooe): Remove this method. Its use is inherently racy. Seems to
|
|
|
|
// be used to detect whether or a response code has been issued or not.
|
|
|
|
// Another hook should be used instead.
|
|
|
|
wf.mu.Lock()
|
|
|
|
defer wf.mu.Unlock()
|
|
|
|
|
2015-05-08 18:33:33 +00:00
|
|
|
return wf.flushed
|
|
|
|
}
|
|
|
|
|
2015-11-03 00:11:28 +00:00
|
|
|
// Close closes the write flusher, disallowing any further writes to the
|
|
|
|
// target. After the flusher is closed, all calls to write or flush will
|
|
|
|
// result in an error.
|
|
|
|
func (wf *WriteFlusher) Close() error {
|
|
|
|
wf.mu.Lock()
|
|
|
|
defer wf.mu.Unlock()
|
|
|
|
|
|
|
|
if wf.closed != nil {
|
|
|
|
return wf.closed
|
|
|
|
}
|
|
|
|
|
|
|
|
wf.closed = errWriteFlusherClosed
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-03 01:45:05 +00:00
|
|
|
// NewWriteFlusher returns a new WriteFlusher.
|
2015-05-08 18:33:33 +00:00
|
|
|
func NewWriteFlusher(w io.Writer) *WriteFlusher {
|
|
|
|
var flusher http.Flusher
|
|
|
|
if f, ok := w.(http.Flusher); ok {
|
|
|
|
flusher = f
|
|
|
|
} else {
|
|
|
|
flusher = &NopFlusher{}
|
|
|
|
}
|
|
|
|
return &WriteFlusher{w: w, flusher: flusher}
|
|
|
|
}
|