bufwr

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2019 License: MIT Imports: 4 Imported by: 0

README

bufwr

bufwr is a buffered writer that accumulates input data in an internal buffer until the buffer is filled up or some defined amount of time is pass.

It especially useful when you need both performance of buffered writing and predictable delay in case of temporally lag of input data.

bufwr is attentive to data bounds and does not split apart an input data chunk on flushing. So you can be sure that the underlying writer will always get a set of the whole data chunks.

Example

Good example of usage might be when you need to write huge amount of JSON/CSV/... messages to Kafka.

  • You want to pack some number of messages together in one block on writing for performance.
  • And a message should not be split apart and be written in two different blocks.
  • Also you don't want that messages get stuck in a buffer for а long time when the incoming message stream is poor/stopped.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer added in v0.2.0

type Writer struct {
	// contains filtered or unexported fields
}

func New

func New(size int, delayFlush time.Duration, w io.Writer) *Writer

New creates a new buffered writer that implements io.Write. The writer accumulates input data in a buffer with size of size, until it is filled up or delayFlush time is pass, and then writes to underlying writer w.

func (*Writer) OnError added in v0.2.0

func (w *Writer) OnError(handler func(error))

OnError sets the handler function that will be called if a error occurs while write to the underlying writer.

func (*Writer) Write added in v0.2.0

func (w *Writer) Write(data []byte) (int, error)

Write writes data to the internal buffer. It actually does not return any error because writing to the underlying writer most of the time happens asynchronously; see OnError method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL