Documentation
¶
Index ¶
- type RingBuffer
- func (r *RingBuffer[T]) Capacity() int
- func (r *RingBuffer[T]) ClearBuffer()
- func (r *RingBuffer[T]) Close() error
- func (r *RingBuffer[T]) CopyConfig(source *RingBuffer[T]) *RingBuffer[T]
- func (r *RingBuffer[T]) Flush()
- func (r *RingBuffer[T]) Free() int
- func (r *RingBuffer[T]) GetAllView() (part1, part2 []T, err error)
- func (r *RingBuffer[T]) GetBlockedReaders() int
- func (r *RingBuffer[T]) GetBlockedWriters() int
- func (r *RingBuffer[T]) GetN(n int) (items []T, err error)
- func (r *RingBuffer[T]) GetNView(n int) (part1, part2 []T, err error)
- func (r *RingBuffer[T]) GetOne() (item T, err error)
- func (r *RingBuffer[T]) IsEmpty() bool
- func (r *RingBuffer[T]) IsFull() bool
- func (r *RingBuffer[T]) Length(lock bool) int
- func (r *RingBuffer[T]) PeekN(n int) (items []T, err error)
- func (r *RingBuffer[T]) PeekNView(n int) (part1, part2 []T, err error)
- func (r *RingBuffer[T]) PeekOne() (item T, err error)
- func (r *RingBuffer[T]) Reset()
- func (r *RingBuffer[T]) WakeUpOneReader()
- func (r *RingBuffer[T]) WakeUpOneWriter()
- func (r *RingBuffer[T]) WithBlocking(block bool) *RingBuffer[T]
- func (r *RingBuffer[T]) WithPreReadBlockHook(hook func() (obj T, tryAgain bool, success bool)) *RingBuffer[T]
- func (r *RingBuffer[T]) WithPreWriteBlockHook(hook func() bool) *RingBuffer[T]
- func (r *RingBuffer[T]) WithReadTimeout(d time.Duration) *RingBuffer[T]
- func (r *RingBuffer[T]) WithTimeout(d time.Duration) *RingBuffer[T]
- func (r *RingBuffer[T]) WithWriteTimeout(d time.Duration) *RingBuffer[T]
- func (r *RingBuffer[T]) Write(item T) error
- func (r *RingBuffer[T]) WriteMany(items []T) (n int, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RingBuffer ¶
type RingBuffer[T any] struct { // contains filtered or unexported fields }
RingBuffer is a circular buffer that implements io.ReaderWriter interface. It operates like a buffered pipe, where data is written to a RingBuffer and can be read back from another goroutine. It is safe to concurrently read and write RingBuffer.
Key features: - Thread-safe concurrent read/write operations - Configurable blocking/non-blocking behavior - Timeout support for read/write operations - Pre-read hook for custom blocking behavior - Efficient circular buffer implementation
func New ¶
func New[T any](size int) *RingBuffer[T]
New returns a new RingBuffer whose buffer has the given size.
func NewWithConfig ¶
func NewWithConfig[T any](size int, cfg *config.RingBufferConfig[T]) (*RingBuffer[T], error)
NewWithConfig creates a new RingBuffer with the given size and configuration. It returns an error if the size is less than or equal to 0.
func (*RingBuffer[T]) Capacity ¶
func (r *RingBuffer[T]) Capacity() int
Capacity returns the size of the underlying buffer
func (*RingBuffer[T]) ClearBuffer ¶
func (r *RingBuffer[T]) ClearBuffer()
ClearBuffer clears all items in the buffer and resets read/write positions. Useful when shrinking the buffer or cleaning up resources.
func (*RingBuffer[T]) Close ¶
func (r *RingBuffer[T]) Close() error
Close closes the ring buffer and cleans up resources. Behavior: - Sets error to io.EOF - Clears all items in the buffer - Signals all waiting readers and writers - All subsequent operations will return io.EOF
func (*RingBuffer[T]) CopyConfig ¶
func (r *RingBuffer[T]) CopyConfig(source *RingBuffer[T]) *RingBuffer[T]
CopyConfig copies the configuration settings from the source buffer to the target buffer. This includes blocking mode, timeouts, and cancellation context.
func (*RingBuffer[T]) Flush ¶
func (r *RingBuffer[T]) Flush()
Flush clears all items from the buffer while maintaining its configuration. This includes: - Resetting read and write positions to 0 - Clearing the full flag - Clearing the buffer contents - Maintaining error state and configuration (blocking, timeouts, hooks)
func (*RingBuffer[T]) Free ¶
func (r *RingBuffer[T]) Free() int
Free returns the number of items that can be written without blocking. This is the available space in the buffer.
func (*RingBuffer[T]) GetAllView ¶
func (r *RingBuffer[T]) GetAllView() (part1, part2 []T, err error)
GetAllView returns a view of all items in the buffer. The view is not a copy, but a reference to the buffer. The view is valid until the buffer is modified. If the view is modified, the buffer will be modified. Make sure to get the items out of the slice before the buffer is modified. This is more efficient than GetAll, but less safe, depending on your use case. Returns ErrIsEmpty if the buffer is empty.
func (*RingBuffer[T]) GetBlockedReaders ¶ added in v0.2.0
func (r *RingBuffer[T]) GetBlockedReaders() int
GetBlockedReaders returns the number of blocked readers
func (*RingBuffer[T]) GetBlockedWriters ¶
func (r *RingBuffer[T]) GetBlockedWriters() int
GetBlockedWriters returns the number of blocked writers
func (*RingBuffer[T]) GetN ¶
func (r *RingBuffer[T]) GetN(n int) (items []T, err error)
GetMany returns n items from the buffer. Behavior: - Gets all n items or blocks until it can - Returns ErrIsEmpty if buffer is empty and not blocking - Returns context.DeadlineExceeded if timeout occurs - Handles wrapping around the buffer end
func (*RingBuffer[T]) GetNView ¶
func (r *RingBuffer[T]) GetNView(n int) (part1, part2 []T, err error)
GetManyView returns a view of exactly n items from the buffer. The view is not a copy, but a reference to the buffer. The view is valid until the buffer is modified. If the view is modified, the buffer will be modified. Make sure to get the items out of the slice before the buffer is modified. This is more efficient than GetMany, but less safe, depending on your use case. Returns: - ErrInvalidLength if n <= 0 or n > buffer size - ErrIsEmpty if buffer is empty and not blocking - context.DeadlineExceeded if timeout occurs
func (*RingBuffer[T]) GetOne ¶
func (r *RingBuffer[T]) GetOne() (item T, err error)
GetOne returns a single item from the buffer. Behavior: - Blocks if buffer is empty and in blocking mode - Returns ErrIsEmpty if buffer is empty and not blocking - Returns context.DeadlineExceeded if timeout occurs - Signals waiting writers when data is read
func (*RingBuffer[T]) IsEmpty ¶
func (r *RingBuffer[T]) IsEmpty() bool
IsEmpty returns true when the ringbuffer is empty.
func (*RingBuffer[T]) IsFull ¶
func (r *RingBuffer[T]) IsFull() bool
IsFull returns true when the ringbuffer is full.
func (*RingBuffer[T]) Length ¶
func (r *RingBuffer[T]) Length(lock bool) int
Length returns the number of items that can be read. This is the actual number of items in the buffer.
func (*RingBuffer[T]) PeekN ¶
func (r *RingBuffer[T]) PeekN(n int) (items []T, err error)
PeekMany returns exactly n items without removing them from the buffer. Returns ErrIsEmpty if there aren't enough items available.
func (*RingBuffer[T]) PeekNView ¶
func (r *RingBuffer[T]) PeekNView(n int) (part1, part2 []T, err error)
PeekManyView returns a view of exactly n items from the buffer without removing them. The view is not a copy, but a reference to the buffer. The view is valid until the buffer is modified. If the view is modified, the buffer will be modified. Make sure to get the items out of the slice before the buffer is modified. This is more efficient than PeekN, but less safe, depending on your use case. Returns ErrIsEmpty if there aren't exactly n items available.
func (*RingBuffer[T]) PeekOne ¶
func (r *RingBuffer[T]) PeekOne() (item T, err error)
PeekOne returns the next item without removing it from the buffer
func (*RingBuffer[T]) Reset ¶
func (r *RingBuffer[T]) Reset()
Reset resets the buffer to its initial state. This includes: - Resetting read and write positions to 0 - Clearing the full flag - Clearing any error state - Clearing the buffer contents
func (*RingBuffer[T]) WakeUpOneReader ¶ added in v0.3.0
func (r *RingBuffer[T]) WakeUpOneReader()
wake up one reader
func (*RingBuffer[T]) WakeUpOneWriter ¶ added in v0.3.0
func (r *RingBuffer[T]) WakeUpOneWriter()
wake up one writer
func (*RingBuffer[T]) WithBlocking ¶
func (r *RingBuffer[T]) WithBlocking(block bool) *RingBuffer[T]
WithBlocking sets the blocking mode of the ring buffer. When blocking is enabled: - Read operations will block when the buffer is empty - Write operations will block when the buffer is full - Condition variables are created for synchronization
func (*RingBuffer[T]) WithPreReadBlockHook ¶
func (r *RingBuffer[T]) WithPreReadBlockHook(hook func() (obj T, tryAgain bool, success bool)) *RingBuffer[T]
WithPreReadBlockHook sets a hook function that will be called before blocking on a read or hitting a deadline. This allows for custom handling of blocking situations, such as trying alternative sources for data.
func (*RingBuffer[T]) WithPreWriteBlockHook ¶
func (r *RingBuffer[T]) WithPreWriteBlockHook(hook func() bool) *RingBuffer[T]
WithPreWriteBlockHook sets a hook function that will be called before blocking on a write or hitting a deadline. This allows for custom handling of blocking situations, such as trying alternative destinations for data.
func (*RingBuffer[T]) WithReadTimeout ¶
func (r *RingBuffer[T]) WithReadTimeout(d time.Duration) *RingBuffer[T]
WithReadTimeout sets the timeout for read operations. Read operations wait for writes to complete, so this sets the write timeout. This method automatically enables blocking mode since timeouts require blocking behavior.
func (*RingBuffer[T]) WithTimeout ¶
func (r *RingBuffer[T]) WithTimeout(d time.Duration) *RingBuffer[T]
WithTimeout sets both read and write timeouts for the ring buffer. When a timeout occurs, the operation returns context.DeadlineExceeded. A timeout of 0 or less disables timeouts. This method automatically enables blocking mode since timeouts require blocking behavior.
func (*RingBuffer[T]) WithWriteTimeout ¶
func (r *RingBuffer[T]) WithWriteTimeout(d time.Duration) *RingBuffer[T]
WithWriteTimeout sets the timeout for write operations. Write operations wait for reads to complete, so this sets the read timeout. This method automatically enables blocking mode since timeouts require blocking behavior.
func (*RingBuffer[T]) Write ¶
func (r *RingBuffer[T]) Write(item T) error
Write writes a single item to the buffer. Behavior: - Blocks if buffer is full and in blocking mode - Returns ErrIsFull if buffer is full and not blocking - Returns context.DeadlineExceeded if timeout occurs - Signals waiting readers when data is written
func (*RingBuffer[T]) WriteMany ¶
func (r *RingBuffer[T]) WriteMany(items []T) (n int, err error)
WriteMany writes multiple items to the buffer. Behavior: - Writes all items or none - Returns ErrIsFull if buffer doesn't have enough space and not blocking - Blocks until all items can be written or timeout occurs - Returns number of items written and any error - Handles wrapping around the buffer end
