Documentation
¶
Overview ¶
Package ringbuffer provides a high-performance, lock-free MPMC (Multi-Producer Multi-Consumer) ring buffer implementation. It uses atomic operations and spin-waiting for efficient concurrent access, suitable for high-throughput scenarios. The buffer size must be a power of two for optimal performance.
Example usage:
rb, err := ringbuffer.New[int](1024)
if err != nil {
log.Fatal(err)
}
defer rb.Close()
go func() {
for i := 0; i < 100; i++ {
rb.Write(i)
}
rb.Close()
}()
for {
v, err := rb.Read()
if err != nil {
break
}
fmt.Println(v)
}
Index ¶
- Variables
- type RingBuffer
- func (rb *RingBuffer[T]) Cap() int
- func (rb *RingBuffer[T]) Close()
- func (rb *RingBuffer[T]) IsEmpty() bool
- func (rb *RingBuffer[T]) IsFull() bool
- func (rb *RingBuffer[T]) Len() int
- func (rb *RingBuffer[T]) Read() (T, error)
- func (rb *RingBuffer[T]) WaitForClose()
- func (rb *RingBuffer[T]) Write(value T) error
- type Slot
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type RingBuffer ¶
type RingBuffer[T any] struct { // contains filtered or unexported fields }
RingBuffer represents the MPMC ring buffer
func (*RingBuffer[T]) Cap ¶ added in v0.1.30
func (rb *RingBuffer[T]) Cap() int
Cap returns the capacity of the buffer
func (*RingBuffer[T]) IsEmpty ¶ added in v0.1.30
func (rb *RingBuffer[T]) IsEmpty() bool
IsEmpty returns true if the buffer is empty
func (*RingBuffer[T]) IsFull ¶ added in v0.1.30
func (rb *RingBuffer[T]) IsFull() bool
IsFull returns true if the buffer is full
func (*RingBuffer[T]) Len ¶ added in v0.1.30
func (rb *RingBuffer[T]) Len() int
Len returns the current number of elements in the buffer
func (*RingBuffer[T]) Read ¶
func (rb *RingBuffer[T]) Read() (T, error)
Read reads data from the ring buffer by a consumer
func (*RingBuffer[T]) WaitForClose ¶ added in v0.1.30
func (rb *RingBuffer[T]) WaitForClose()
WaitForClose blocks until all data has been consumed after closing
func (*RingBuffer[T]) Write ¶
func (rb *RingBuffer[T]) Write(value T) error
Write writes data into the ring buffer by a producer.
It explicitly boxes the value on the heap to ensure the pointer remains valid, avoiding reliance on compiler escape analysis for safety.