ringslice

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 4 Imported by: 1

README

ringslice

A slice you can write to, read from, and fall out of sync if not reading fast enough.

This is based on the original ringbuf, using Go's templates to support any type of content.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStaleReader = errors.New("ringbuffer reader is stale (didn't read fast enough - do you need a larger buffer?)")
)

Functions

This section is empty.

Types

type Reader

type Reader[T any] struct {
	// contains filtered or unexported fields
}

func (*Reader[T]) Close

func (r *Reader[T]) Close() error

Close signals this reader will not be used anymore and has finished processing, and should be called after a reader is not useful anymore.

If using Writer.Close then calling Close on readers is mandatory, failing to do so will cause a deadlock.

func (*Reader[T]) Read

func (r *Reader[T]) Read(p []T) (int, error)

Read will read data from the ringbuffer to the provided buffer. If no new data is available, Read() will either return io.EOF (a later call may return new data), or block until data becomes available (if set blocking).

func (*Reader[T]) ReadOne

func (r *Reader[T]) ReadOne() (T, error)

func (*Reader[T]) Reset

func (r *Reader[T]) Reset()

Reset sets the reader's position after the writer's latest write.

func (*Reader[T]) SetAutoSkip

func (r *Reader[T]) SetAutoSkip(enabled bool)

SetAutoSkip allows enabling auto skip, when this reader hasn't been reading fast enough and missed some data. This is generally unsafe, but in some cases may be useful to avoid having to handle stale readers.

type Writer

type Writer[T any] struct {
	// contains filtered or unexported fields
}

Writer is the main data container.

func New

func New[T any](size int64) (*Writer[T], error)

func (*Writer[T]) Append

func (w *Writer[T]) Append(values ...T) (int, error)

Append values to the slice

func (*Writer[T]) BlockingCurrentReader added in v0.1.1

func (w *Writer[T]) BlockingCurrentReader() *Reader[T]

BlockingCurrentReader returns a new reader positionned at the buffer's edge.

func (*Writer[T]) BlockingReader

func (w *Writer[T]) BlockingReader() *Reader[T]

BlockingReader returns a new reader positioned at the buffer's oldest available position which reads will block if no new data is available.

func (*Writer[T]) Close

func (w *Writer[T]) Close() error

Close will cause all readers to return EOF once they have read the whole buffer and will wait until all readers have called Close(). If you do not need EOF synchronization you can ignore the whole close system as it is not used to free any resources, but if you use ringbuf as an output buffer, for example, it will enable waiting for writes to have completed prior to ending the program.

Note that if any reader failed to call close prior to end and being freed this may cause a deadlock. Use CloseNow() to avoid this.

func (*Writer[T]) Reader

func (w *Writer[T]) Reader() *Reader[T]

Reader returns a new reader positioned at the buffer's oldest available position. The reader can be moved to the most recent position by calling its Reset() method.

If there isn't enough data to read, the reader's read method will return error io.EOF. If you need Read() to not return until new data is available, use BlockingReader()

func (*Writer[T]) Size

func (w *Writer[T]) Size() int64

func (*Writer[T]) TotalWritten

func (w *Writer[T]) TotalWritten() int64

func (*Writer[T]) Write

func (w *Writer[T]) Write(values []T) (int, error)

Jump to

Keyboard shortcuts

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