ring

package
v0.8.8 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSlowReader   = errors.New("slow reader detected")
	ErrBufferClosed = errors.New("buffer was closed")
)

ErrSlowReader is returned when a reader has been lapped by the writer.

Functions

This section is empty.

Types

type CircularBuffer

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

CircularBuffer is a generic, lock-based, fixed-size circular buffer. It supports one writer and multiple readers.

func NewCircularBuffer

func NewCircularBuffer[T any](size int) *CircularBuffer[T]

NewCircularBuffer creates a new generic circular buffer.

func (*CircularBuffer[T]) Close

func (b *CircularBuffer[T]) Close()

Close marks the buffer as closed, so readers will no longer block on reading. Readers can still consume remaining items in the buffer, after which they'll receive an ErrBufferClosed error.

func (*CircularBuffer[T]) NewReader

func (b *CircularBuffer[T]) NewReader() *Reader[T]

NewReader creates a new reader associated with the buffer.

func (*CircularBuffer[T]) Write

func (b *CircularBuffer[T]) Write(v T) error

Write adds a new value to the buffer.

type Reader

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

Reader represents a single consumer of the CircularBuffer.

func (*Reader[T]) Read

func (r *Reader[T]) Read(ctx context.Context) (T, error)

Read blocks until a new value is available and returns it. It returns an error if the context is cancelled or if the reader is lapped.

func (*Reader[T]) Rewind

func (r *Reader[T]) Rewind(predicate func(v T) bool) bool

Rewind moves the reader's cursor backward in the buffer to the first element that satisfies the predicate. Returns true if a matching element was found.

Jump to

Keyboard shortcuts

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