8 // WriteFlusher wraps the Write and Flush operation ensuring that every write
9 // is a flush. In addition, the Close method can be called to intercept
10 // Read/Write calls if the targets lifecycle has already ended.
11 type WriteFlusher struct {
20 type flusher interface {
24 var errWriteFlusherClosed = io.EOF
26 func (wf *WriteFlusher) Write(b []byte) (n int, err error) {
29 return 0, errWriteFlusherClosed
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 wf.flushedOnce.Do(func() {
52 // Flushed returns the state of flushed.
53 // If it's flushed, return true, or else it return false.
54 func (wf *WriteFlusher) Flushed() bool {
55 // BUG(stevvooe): Remove this method. Its use is inherently racy. Seems to
56 // be used to detect whether or a response code has been issued or not.
57 // Another hook should be used instead.
67 // Close closes the write flusher, disallowing any further writes to the
68 // target. After the flusher is closed, all calls to write or flush will
69 // result in an error.
70 func (wf *WriteFlusher) Close() error {
72 defer wf.closeLock.Unlock()
76 return errWriteFlusherClosed
83 // NewWriteFlusher returns a new WriteFlusher.
84 func NewWriteFlusher(w io.Writer) *WriteFlusher {
86 if f, ok := w.(flusher); ok {
91 return &WriteFlusher{w: w, flusher: fl, closed: make(chan struct{}), flushed: make(chan struct{})}