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. The only allocation is the backing slices in New.
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 ¶
This section is empty.
Variables ¶
This section is empty.
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 Ring ¶
type Ring struct {
// contains filtered or unexported fields
}
Ring is a fixed-capacity MPMC ring buffer. Use New.
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. A capacity below 2 is replaced with defaultCapacity (1024); otherwise capacity is rounded up to the next power of two and clamped to maxCapacity. The smallest ring obtainable is New(2).
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. 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.