Documentation ¶
Overview ¶
Package bufferutil provides helpers for dealing with buffers.
The main exported types are `Pool` or a re-usable pool of memory chunks, and BufferHandlers which are used to syndicate binary writes to multiple listeners.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct { sync.RWMutex // Size is the size of all bytes written to the buffer. Size int64 // Lines are the string lines broken up by newlines with associated timestamps Chunks []BufferChunk `json:"chunks"` // Handler is an optional listener for new line events. Handler BufferChunkHandler `json:"-"` }
Buffer is a writer that accepts binary but splits out onto new lines.
func (*Buffer) Write ¶
Write writes the contents to the output buffer. An important gotcha here is the `contents` parameter is by reference, as a result you can get into some bad loop capture states where this buffer will be assigned to multiple chunks but be effectively the same value. As a result, when you write to the output buffer, we fully copy this contents parameter for storage in the buffer.
type BufferChunk ¶
BufferChunk is a single write to a buffer with a timestamp.
func (BufferChunk) Copy ¶
func (bc BufferChunk) Copy() BufferChunk
Copy returns a copy of the chunk.
func (BufferChunk) MarshalJSON ¶
func (bc BufferChunk) MarshalJSON() ([]byte, error)
MarshalJSON implemnts json.Marshaler.
func (*BufferChunk) UnmarshalJSON ¶
func (bc *BufferChunk) UnmarshalJSON(contents []byte) error
UnmarshalJSON implements json.Unmarshaler.
type BufferChunkHandler ¶
type BufferChunkHandler func(BufferChunk)
BufferChunkHandler is a handler for output chunks.
type BufferHandlers ¶
BufferHandlers is a synchronized map of listeners for new lines to a line buffer.
func (*BufferHandlers) Add ¶
func (bl *BufferHandlers) Add(uid string, handler BufferChunkHandler)
Add adds a handler.
func (*BufferHandlers) Handle ¶
func (bl *BufferHandlers) Handle(chunk BufferChunk)
Handle calls the handlers.
func (*BufferHandlers) Remove ¶
func (bl *BufferHandlers) Remove(uid string)
Remove removes a listener.
type Pool ¶
Pool is a sync.Pool of bytes.Buffer.
func NewPool ¶
NewPool returns a new Pool, which returns bytes buffers pre-sized to a given minimum size.
The purpose of a buffer pool is to reduce the number of gc collections incurred when using bytes buffers repeatedly; instead of marking the buffer as to be collected, it is returned to the pool to be re-used.
Example:
pool := bufferutil.NewPool(1024) // pre-allocate 1024 bytes per buffer. func() { buf := pool.Get() defer pool.Put(buf) // do things with the buffer ... }()
type PutOnCloser ¶
PutOnCloser is a helper wrapper that will return a buffer to a given pool.
func PutOnClose ¶
func PutOnClose(buffer *bytes.Buffer, pool *Pool) *PutOnCloser
PutOnClose wraps a buffer with a close function that will return the buffer to the pool.
func (PutOnCloser) Close ¶
func (poc PutOnCloser) Close() error
Close returns the buffer to the pool.