ringbuf

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ProcessFunc

type ProcessFunc[T any] func(T)

ProcessFunc defines the function signature for processing messages T can be any type: []byte, string, struct, etc.

type RingMPSC

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

RingMPSC is a high-performance ring buffer for multiple producers and a single consumer Uses a mutex to serialize concurrent writes from multiple producers

func NewRingMPSC

func NewRingMPSC[T any](cfg RingMPSCConfig[T]) *RingMPSC[T]

NewRingMPSC creates a new MPSC ring buffer with the given configuration

func (*RingMPSC[T]) Capacity

func (rb *RingMPSC[T]) Capacity() uint64

Capacity returns the capacity of the buffer

func (*RingMPSC[T]) Close

func (rb *RingMPSC[T]) Close()

Close closes the ring buffer and wakes up blocked consumers After close, producers cannot push new data, but consumers will drain remaining data Safe to call multiple times

func (*RingMPSC[T]) IsClosed

func (rb *RingMPSC[T]) IsClosed() bool

IsClosed returns whether the ring buffer is closed

func (*RingMPSC[T]) IsEmpty

func (rb *RingMPSC[T]) IsEmpty() bool

IsEmpty reports whether the ring buffer contains no items. It suffers from a race condition where the result may be stale immediately after return.

func (*RingMPSC[T]) IsFull

func (rb *RingMPSC[T]) IsFull() bool

IsFull reports whether the ring buffer is at capacity. It suffers from a race condition where the result may be stale immediately after return.

func (*RingMPSC[T]) Length

func (rb *RingMPSC[T]) Length() uint64

Length returns the approximate number of items currently in the buffer. The returned length is approximate and may be stale by the time it's used.

func (*RingMPSC[T]) Pop

func (rb *RingMPSC[T]) Pop() (T, bool)

Pop reads data by consumer with blocking Blocks and waits until data arrives if buffer is empty Returns the data and success flag (false indicates closed AND buffer is empty) When closed, will drain all remaining data before returning false

func (*RingMPSC[T]) Push

func (rb *RingMPSC[T]) Push(item T) bool

Push writes data by producer with blocking Blocks and waits until space is available if buffer is full Returns false if ring buffer is closed, true on success

func (*RingMPSC[T]) TryPop

func (rb *RingMPSC[T]) TryPop() (T, bool)

TryPop reads data by consumer (non-blocking) Returns the data and a flag indicating success

func (*RingMPSC[T]) TryPush

func (rb *RingMPSC[T]) TryPush(item T) bool

TryPush writes data by producer (non-blocking) Returns true on success, false if buffer is full or closed

type RingMPSCConfig

type RingMPSCConfig[T any] struct {
	// Capacity is the buffer size of the ring buffer
	// Default: 1024
	Capacity uint64

	// ProcessFunc is the function to process each message
	// Optional: if provided, a consumer goroutine will be automatically started
	// If nil, you need to manually call Pop() to consume messages
	ProcessFunc ProcessFunc[T]
}

RingMPSCConfig holds configuration for RingMPSC T is the type of messages being processed

type RingSPSC

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

RingSPSC is a lock-free ring buffer for single producer and single consumer Implements high-performance SPSC queue using atomic operations, ensuring strict FIFO ordering

func NewRingSPSC

func NewRingSPSC[T any](cfg RingSPSCConfig[T]) *RingSPSC[T]

NewRingSPSC creates a new SPSC ring buffer with the given configuration

func (*RingSPSC[T]) Capacity

func (rb *RingSPSC[T]) Capacity() uint64

Capacity returns the capacity of the buffer

func (*RingSPSC[T]) Close

func (rb *RingSPSC[T]) Close()

Close closes the ring buffer and wakes up blocked consumers After close, producers cannot push new data, but consumers will drain remaining data Safe to call multiple times

func (*RingSPSC[T]) IsClosed

func (rb *RingSPSC[T]) IsClosed() bool

IsClosed returns whether the ring buffer is closed

func (*RingSPSC[T]) IsEmpty

func (rb *RingSPSC[T]) IsEmpty() bool

IsEmpty reports whether the ring buffer contains no items. It suffers from a race condition where the result may be stale immediately after return.

func (*RingSPSC[T]) IsFull

func (rb *RingSPSC[T]) IsFull() bool

IsFull reports whether the ring buffer is at capacity. It suffers from a race condition where the result may be stale immediately after return.

func (*RingSPSC[T]) Length

func (rb *RingSPSC[T]) Length() uint64

Length returns the approximate number of items currently in the buffer. The returned length is approximate and may be stale by the time it's used.

func (*RingSPSC[T]) Pop

func (rb *RingSPSC[T]) Pop() (T, bool)

Pop reads data by consumer with blocking Blocks and waits until data arrives if buffer is empty Returns the data and success flag (false indicates closed AND buffer is empty) When closed, will drain all remaining data before returning false

func (*RingSPSC[T]) Push

func (rb *RingSPSC[T]) Push(item T) bool

Push writes data by producer with blocking Blocks and waits until space is available if buffer is full Returns false if ring buffer is closed, true on success This approach ensures data is not lost, forming a backpressure mechanism

func (*RingSPSC[T]) TryPop

func (rb *RingSPSC[T]) TryPop() (T, bool)

TryPop reads data by consumer (non-blocking) Returns the data and a flag indicating success

func (*RingSPSC[T]) TryPush

func (rb *RingSPSC[T]) TryPush(item T) bool

TryPush writes data by producer (non-blocking) Returns true on success, false if buffer is full or closed

type RingSPSCConfig

type RingSPSCConfig[T any] struct {
	// Capacity is the buffer size of the ring buffer
	// Default: 1024
	Capacity uint64

	// ProcessFunc is the function to process each message
	// Optional: if provided, a consumer goroutine will be automatically started
	// If nil, you need to manually call Pop() to consume messages
	ProcessFunc ProcessFunc[T]
}

RingSPSCConfig holds configuration for RingSPSC T is the type of messages being processed

Jump to

Keyboard shortcuts

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