circ

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultBufferSize is the default size of the buffer in bytes.
	DefaultBufferSize int = 1024 * 256

	// DefaultBlockSize is the default size per R/W block in bytes.
	DefaultBlockSize int = 1024 * 8

	// ErrOutOfRange indicates that the index was out of range.
	ErrOutOfRange = errors.New("Indexes out of range")

	// ErrInsufficientBytes indicates that there were not enough bytes to return.
	ErrInsufficientBytes = errors.New("Insufficient bytes to return")
)

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	Mu sync.RWMutex // the buffer needs its own mutex to work properly.
	ID string       // the identifier of the buffer. This is used in debug output.

	State uint32 // indicates whether the buffer is reading from (1) or writing to (2).
	// contains filtered or unexported fields
}

Buffer is a circular buffer for reading and writing messages.

func NewBuffer

func NewBuffer(size, block int) *Buffer

NewBuffer returns a new instance of buffer. You should call NewReader or NewWriter instead of this function.

func NewBufferFromSlice

func NewBufferFromSlice(block int, buf []byte) *Buffer

NewBufferFromSlice returns a new instance of buffer using a pre-existing byte slice.

func (*Buffer) CapDelta

func (b *Buffer) CapDelta() int

CapDelta returns the difference between the head and tail.

func (*Buffer) CommitTail

func (b *Buffer) CommitTail(n int)

CommitTail moves the tail position of the buffer n bytes.

func (*Buffer) Get

func (b *Buffer) Get() []byte

Get returns the internal buffer.

func (*Buffer) GetPos

func (b *Buffer) GetPos() (int64, int64)

GetPos will return the tail and head positions of the buffer. This method is for use with testing.

func (*Buffer) Index

func (b *Buffer) Index(i int64) int

Index returns the buffer-relative index of an integer.

func (*Buffer) Read

func (b *Buffer) Read(n int) (p []byte, err error)

Read reads n bytes from the buffer, and will block until at n bytes exist in the buffer to read.

func (*Buffer) Set

func (b *Buffer) Set(p []byte, start, end int) error

Set writes bytes to a range of indexes in the byte buffer.

func (*Buffer) SetPos

func (b *Buffer) SetPos(tail, head int64)

SetPos sets the head and tail of the buffer.

func (*Buffer) Stop

func (b *Buffer) Stop()

Stop signals the buffer to stop processing.

type BytesPool

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

BytesPool is a pool of []byte.

func NewBytesPool

func NewBytesPool(n int) *BytesPool

NewBytesPool returns a sync.pool of []byte.

func (*BytesPool) Get

func (b *BytesPool) Get() []byte

Get returns a pooled bytes.Buffer.

func (*BytesPool) InUse

func (b *BytesPool) InUse() int64

InUse returns the number of pool blocks in use.

func (*BytesPool) Put

func (b *BytesPool) Put(x []byte)

Put puts the byte slice back into the pool.

type Reader

type Reader struct {
	*Buffer
}

Reader is a circular buffer for reading data from an io.Reader.

func NewReader

func NewReader(size, block int) *Reader

NewReader returns a new Circular Reader.

func NewReaderFromSlice

func NewReaderFromSlice(block int, p []byte) *Reader

NewReaderFromSlice returns a new Circular Reader using a pre-exising byte slice.

func (*Reader) ReadFrom

func (b *Reader) ReadFrom(r io.Reader) (total int64, err error)

ReadFrom reads bytes from an io.Reader and commits them to the buffer when there is sufficient capacity to do so.

type Writer

type Writer struct {
	*Buffer
}

Writer is a circular buffer for writing data to an io.Writer.

func NewWriter

func NewWriter(size, block int) *Writer

NewWriter returns a pointer to a new Circular Writer.

func NewWriterFromSlice

func NewWriterFromSlice(block int, p []byte) *Writer

NewWriterFromSlice returns a new Circular Writer using a pre-existing byte slice.

func (*Writer) Write

func (b *Writer) Write(p []byte) (total int, err error)

Write writes the buffer to the buffer p, returning the number of bytes written. The bytes written to the buffer are picked up by WriteTo.

func (*Writer) WriteTo

func (b *Writer) WriteTo(w io.Writer) (total int64, err error)

WriteTo writes the contents of the buffer to an io.Writer.

Jump to

Keyboard shortcuts

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