10 // WriteFlusher wraps the Write and Flush operation ensuring that every write
11 // is a flush. In addition, the Close method can be called to intercept
12 // Read/Write calls if the targets lifecycle has already ended.
13 type WriteFlusher struct {
20 // TODO(stevvooe): Use channel for closed instead, remove mutex. Using a
21 // channel will allow one to properly order the operations.
24 var errWriteFlusherClosed = errors.New("writeflusher: closed")
26 func (wf *WriteFlusher) Write(b []byte) (n int, err error) {
33 n, err = wf.w.Write(b)
34 wf.flush() // every write is a flush.
38 // Flush the stream immediately.
39 func (wf *WriteFlusher) Flush() {
46 // flush the stream immediately without taking a lock. Used internally.
47 func (wf *WriteFlusher) flush() {
56 // Flushed returns the state of flushed.
57 // If it's flushed, return true, or else it return false.
58 func (wf *WriteFlusher) Flushed() bool {
59 // BUG(stevvooe): Remove this method. Its use is inherently racy. Seems to
60 // be used to detect whether or a response code has been issued or not.
61 // Another hook should be used instead.
68 // Close closes the write flusher, disallowing any further writes to the
69 // target. After the flusher is closed, all calls to write or flush will
70 // result in an error.
71 func (wf *WriteFlusher) Close() error {
79 wf.closed = errWriteFlusherClosed
83 // NewWriteFlusher returns a new WriteFlusher.
84 func NewWriteFlusher(w io.Writer) *WriteFlusher {
85 var flusher http.Flusher
86 if f, ok := w.(http.Flusher); ok {
89 flusher = &NopFlusher{}
91 return &WriteFlusher{w: w, flusher: flusher}