A lock-free SPSC (Single Producer Single Consumer) ring buffer implementation in Go.
- Lock-free implementation using atomic operations
- Supports single producer and single consumer (SPSC)
- Implements
io.Readerandio.Writerinterfaces - Zero-copy read operations available
- Buffer size rounded to next power of 2
go get github.com/drgolem/ringbufferpackage main
import (
"fmt"
"github.com/drgolem/ringbuffer"
)
func main() {
rb := ringbuffer.New(1024)
// Write data
data := []byte("Hello, World!")
n, err := rb.Write(data)
if err != nil {
panic(err)
}
fmt.Printf("Wrote %d bytes\n", n)
// Read data
buf := make([]byte, 100)
n, err = rb.Read(buf)
if err != nil {
panic(err)
}
fmt.Printf("Read: %s\n", buf[:n])
}New(size uint64) *RingBuffer- Creates a new ring buffer
Read(data []byte) (int, error)- Reads data from bufferReadSlices() ([]byte, []byte, uint64)- Returns direct buffer slices (zero-copy)PeekContiguous() []byte- Returns contiguous readable data (zero-copy)Consume(n uint64) error- Advances read position
Write(data []byte) (int, error)- Writes data to buffer
AvailableRead() uint64- Bytes available for readingAvailableWrite() uint64- Bytes available for writingSize() uint64- Total buffer capacity
Reset()- Clears the buffer (not thread-safe)
This implementation is only safe for single producer, single consumer usage. One goroutine should write, and one goroutine should read. Multiple producers or consumers will cause data races.
MIT