ringbuf

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

package ringbuf Cache-line-aligned, lock-free MPMC ring buffer. Every slot occupies exactly one 64-byte cache line so producers and consumers never false-share. Power-of-two capacity; Vyukov MPMC queue discipline adapted for fixed-size slots.

Zero allocation on Push/Pop/BatchDrain in steady state. No locks. New allocates only after checking the Ring header and backing slices against the configured memory budget.

Slot fields are deliberately generic: Seq and Timestamp are owned by the ring, everything else is caller-defined. If a caller needs more than 8 bytes of payload, store a handle (index, hash, offset) in Payload and keep the bulk data in a side buffer the caller owns. A fixed-size slot is the entire point; variable-size payloads defeat it.

Index

Constants

View Source
const DefaultMemoryBudgetBytes uint64 = 64 << 20

DefaultMemoryBudgetBytes is the default per-Ring memory budget (64 MiB).

Variables

View Source
var (
	// ErrInvalidCapacity reports a capacity beyond the supported maximum.
	ErrInvalidCapacity = errors.New("ringbuf: invalid capacity")
	// ErrMemoryBudgetExceeded reports a ring allocation larger than its budget.
	ErrMemoryBudgetExceeded = errors.New("ringbuf: memory budget exceeded")
)

Functions

This section is empty.

Types

type Buffer added in v0.9.2

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

Buffer is a simple mutex-guarded ring buffer for T.

func NewBuffer added in v0.9.2

func NewBuffer[T any](capacity int) *Buffer[T]

NewBuffer allocates a Buffer using the default 64 MiB per-buffer budget. It returns nil when the allocation would exceed that budget; use NewBufferChecked to inspect the error or NewBufferWithOptions for an explicit per-buffer override. Capacities below 1 are normalized to 1.

func NewBufferChecked added in v0.37.8

func NewBufferChecked[T any](capacity int) (*Buffer[T], error)

NewBufferChecked allocates a Buffer using DefaultMemoryBudgetBytes and returns an error before allocating when the estimated size exceeds the budget or the backing array size is not representable.

func NewBufferWithOptions added in v0.37.8

func NewBufferWithOptions[T any](options BufferOptions) (*Buffer[T], error)

NewBufferWithOptions allocates a Buffer after checking the estimated size against the explicitly selected per-buffer budget. A zero budget rejects every allocation. The estimate excludes allocator metadata and objects reachable through pointers stored in T. For zero-size T, no per-element bytes are counted, so the byte budget does not cap logical capacity.

func (*Buffer[T]) Push added in v0.9.2

func (b *Buffer[T]) Push(item T)

func (*Buffer[T]) Snapshot added in v0.9.2

func (b *Buffer[T]) Snapshot() []T

type BufferOptions added in v0.37.8

type BufferOptions struct {
	Capacity          int
	MemoryBudgetBytes uint64
}

BufferOptions configure one generic Buffer allocation. Capacity below 1 retains NewBuffer's historical normalization to 1. MemoryBudgetBytes covers the Buffer header and backing array, excluding allocator metadata and memory reachable through pointers stored in T.

type Options added in v0.37.7

type Options struct {
	Capacity          int
	MemoryBudgetBytes uint64
}

Options configure one Ring allocation. Capacity below 2 retains New's historical default-capacity behavior. MemoryBudgetBytes is required and includes the Ring header plus its backing arrays, excluding allocator metadata and size-class rounding.

type Ring

type Ring struct {
	// contains filtered or unexported fields
}

Ring is a fixed-capacity MPMC ring buffer. Use New or NewWithOptions.

head and tail are padded to cache lines so concurrent producers and consumers do not false-share the two counters.

func New

func New(requestedCapacity int) *Ring

New allocates a Ring using the default 64 MiB budget. A capacity below 2 is replaced with defaultCapacity (1024); otherwise capacity is rounded up to the next power of two. It returns nil when validation fails; use NewChecked to inspect the error. Requests are never clamped to a smaller capacity.

func NewChecked added in v0.37.7

func NewChecked(requestedCapacity int) (*Ring, error)

NewChecked allocates a Ring using DefaultMemoryBudgetBytes and reports invalid or over-budget capacities before allocating.

func NewWithOptions added in v0.37.7

func NewWithOptions(options Options) (*Ring, error)

NewWithOptions allocates a Ring after validating its rounded capacity and estimated allocation size. MemoryBudgetBytes must be nonzero; the caller may explicitly choose a budget different from DefaultMemoryBudgetBytes.

func (*Ring) BatchDrain

func (r *Ring) BatchDrain(dst []Slot) int

BatchDrain pops up to len(dst) slots. Returns the number drained.

func (*Ring) Cap

func (r *Ring) Cap() int

Cap returns the ring's fixed capacity (a power of two, >= 2).

func (*Ring) Len

func (r *Ring) Len() int

Len returns a best-effort snapshot of queued slots; under concurrent Push/Pop it may be stale by the time it returns. If no stable snapshot is found within eight attempts, it returns 0. Do not use it for correctness-critical decisions.

func (*Ring) Pop

func (r *Ring) Pop(dst *Slot) bool

Pop dequeues one slot into dst. Returns false when the buffer is empty.

func (*Ring) Push

func (r *Ring) Push(s *Slot) bool

Push enqueues one slot. Returns false when the buffer is full. The caller's Slot is copied by value; the caller may reuse it immediately.

type Slot

type Slot struct {
	Seq       uint64   // offset  0 — written by the ring
	Timestamp int64    // offset  8 — caller-defined
	A         int64    // offset 16 — caller-defined
	B         int64    // offset 24 — caller-defined
	ID        [16]byte // offset 32 — caller-defined binary identifier
	Status    uint16   // offset 48 — caller-defined
	Kind      uint8    // offset 50 — caller-defined

	Payload [8]byte // offset 56 — caller-defined short payload
	// contains filtered or unexported fields
}

Slot is exactly one cache line (64 bytes on all supported architectures). Size is enforced by a compile-time assertion below.

Jump to

Keyboard shortcuts

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