github.com/Cloud-Foundations/Dominator@v0.3.4/lib/bufwriter/api.go (about)

     1  // Package bufwriter implements a simplified buffered writer, similar to the
     2  // bufio package in the Go standard library, but adds automatic flushing.
     3  package bufwriter
     4  
     5  import (
     6  	"io"
     7  	"sync"
     8  	"time"
     9  )
    10  
    11  type FlushWriter interface {
    12  	Flush() error
    13  	io.Writer
    14  }
    15  
    16  type Writer struct {
    17  	flushDelay     time.Duration
    18  	flushingWriter FlushWriter
    19  	mutex          sync.Mutex // Protect everything below.
    20  	err            error
    21  	flushPending   bool
    22  }
    23  
    24  // NewAutoFlushWriter wraps a FlushWriter and returns an io.Writer. The returned
    25  // writer will automatically call the wrapped Flush method after each Write
    26  // call.
    27  func NewAutoFlushWriter(w FlushWriter) io.Writer {
    28  	return newAutoFlushWriter(w)
    29  }
    30  
    31  // NewWriter wraps a io.Writer and returns a *Writer. Written data are flushed
    32  // within the time specified by flushDelay. If writer does not implement the
    33  // FlushWriter interface then a bufio.Writer is created.
    34  // The Flush and Write methods of writer must not be called directly.
    35  func NewWriter(writer io.Writer, flushDelay time.Duration) *Writer {
    36  	return newWriter(writer, flushDelay)
    37  }
    38  
    39  func (b *Writer) Flush() error { return b.flush() }
    40  
    41  func (b *Writer) Write(p []byte) (int, error) { return b.write(p) }