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 ¶
const DefaultMemoryBudgetBytes uint64 = 64 << 20
DefaultMemoryBudgetBytes is the default per-Ring memory budget (64 MiB).
Variables ¶
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.
type Options ¶ added in v0.37.7
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 ¶
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
NewChecked allocates a Ring using DefaultMemoryBudgetBytes and reports invalid or over-budget capacities before allocating.
func NewWithOptions ¶ added in v0.37.7
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 ¶
BatchDrain pops up to len(dst) slots. Returns the number drained.
func (*Ring) Len ¶
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.
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.