circular

package
v1.2.0-beta.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2022 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package circular provides a buffer with circular semantics.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("reader is closed")

ErrClosed is raised on read from closed Reader.

View Source
var ErrOutOfSync = errors.New("buffer overrun, read position overwritten")

ErrOutOfSync is raised when reader got too much out of sync with the writer.

View Source
var ErrSeekBeforeStart = errors.New("seek before start")

ErrSeekBeforeStart is raised when seek goes beyond start of the file.

Functions

This section is empty.

Types

type Buffer

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

Buffer implements circular buffer with a thread-safe writer, that supports multiple readers each with its own offset.

func NewBuffer

func NewBuffer(opts ...OptionFunc) (*Buffer, error)

NewBuffer creates new Buffer with specified options.

func (*Buffer) Capacity

func (buf *Buffer) Capacity() int

Capacity returns number of bytes allocated for the buffer.

func (*Buffer) GetReader

func (buf *Buffer) GetReader() *Reader

GetReader returns Reader object which implements io.ReadCloser, io.Seeker.

Reader starts at the most distant position in the past available and goes to the current write position.

func (*Buffer) GetStreamingReader

func (buf *Buffer) GetStreamingReader() *StreamingReader

GetStreamingReader returns StreamingReader object which implements io.ReadCloser, io.Seeker.

StreamingReader starts at the most distant position in the past available.

func (*Buffer) Offset

func (buf *Buffer) Offset() int64

Offset returns current write offset (number of bytes written).

func (*Buffer) Write

func (buf *Buffer) Write(p []byte) (int, error)

Write implements io.Writer interface.

type OptionFunc

type OptionFunc func(*Options) error

OptionFunc allows setting Buffer options.

func WithInitialCapacity

func WithInitialCapacity(capacity int) OptionFunc

WithInitialCapacity sets initial buffer capacity.

func WithMaxCapacity

func WithMaxCapacity(capacity int) OptionFunc

WithMaxCapacity sets maximum buffer capacity.

func WithSafetyGap

func WithSafetyGap(gap int) OptionFunc

WithSafetyGap sets safety gap between readers and writers to avoid buffer overrun for the reader.

Reader initial position is set to be as far as possible in the buffer history, but next concurrent write might overwrite read position, and safety gap helps to prevent it. With safety gap, maximum available bytes to read are: MaxCapacity-SafetyGap.

type Options

type Options struct {
	InitialCapacity int
	MaxCapacity     int
	SafetyGap       int
}

Options defines settings for Buffer.

type Reader

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

Reader implements seekable reader with local position in the Buffer which reads from the fixed part of the buffer.

Reader is not safe to be used with concurrent Read/Seek operations.

func (*Reader) Close

func (r *Reader) Close() error

Close implements io.Closer.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read implements io.Reader.

func (*Reader) Seek

func (r *Reader) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker.

type StreamingReader

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

StreamingReader implements seekable reader with local position in the Buffer.

StreamingReader is not safe to be used with concurrent Read/Seek operations.

StreamingReader blocks for new data once it exhausts contents of the buffer.

func (*StreamingReader) Close

func (r *StreamingReader) Close() error

Close implements io.Closer.

func (*StreamingReader) Read

func (r *StreamingReader) Read(p []byte) (n int, err error)

Read implements io.Reader.

func (*StreamingReader) Seek

func (r *StreamingReader) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker.

Jump to

Keyboard shortcuts

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