Documentation
¶
Overview ¶
Package disruptor provides an implementation of the LMAX Disruptor.
If for some reason you have Go code that needs to process messages at sub-microsecond latency, where shaving every nanosecond counts, then consider the disruptor pattern.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCapacity is the error corresponding to wrong capacity. ErrCapacity = fmt.Errorf("capacity must be a power of two") // ErrMissingReaderGroup is the error corresponding to missing // reader group(s). ErrMissingReaderGroup = fmt.Errorf("missing reader group(s)") // ErrEmptyReaderGroup is the error corresponding to an empty // reader group. ErrEmptyReaderGroup = fmt.Errorf("reader group is empty") )
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder[T any] struct { // contains filtered or unexported fields }
Builder builds a disruptor.
func NewBuilder ¶
NewBuilder returns a builder of a disruptor.
func (*Builder[T]) WithReaderGroup ¶
func (b *Builder[T]) WithReaderGroup(group ...ReaderFunc) *Builder[T]
WithReaderGroup represents a group of readers. If this is the first time WithReaderGroup is called, the reader group is the descendant of the Writer. Otherwise, the reader group is a descendant of the reader group of the previously passed in WithReaderGroup().
func (*Builder[T]) WithReaderYield ¶ added in v0.1.3
WithReaderYield overrides how ReadLoop yields when the buffer is empty.
func (*Builder[T]) WithWriterYield ¶ added in v0.1.3
WithWriterYield overrides how Write/WriteBatch yields when the buffer is full. yield receives the number of times yield has been called so far in a Write/WriteBatch call.
type Disruptor ¶
type Disruptor[T any] struct { // contains filtered or unexported fields }
Disruptor supports a single writer and multiple readers.
func (*Disruptor[T]) LoopRead ¶
func (d *Disruptor[T]) LoopRead()
LoopRead continuously reads messages and passes them to a provided reader(s). Blocks until the ring buffer is closed and empty.
func (*Disruptor[T]) Write ¶
func (d *Disruptor[T]) Write(f func(item *T))
Write adds an item to the disruptor. f writes in-place into the ring buffer.
func (*Disruptor[T]) WriteBatch ¶
WriteBatch adds n items to the disruptor. f is essentially a function that accepts a two sub-slices of the internal ring buffer:
1. First sub-slice is to the end of the ring buffer, 2. Secondsub-slice is from the beginning of the ring buffer.
It is possible the 2nd sub-slice is empty if n doesn't wrap around the ring buffer, i.e. length == 0.
Use WriteBatch over Write only if the complexity is needed and if the overhead of sub-slicing is much smaller than the time saved by batching, e.g. when working with SIMD code to write large numbers of items into the disruptor.
type ReaderFunc ¶
type ReaderFunc interface {
// contains filtered or unexported methods
}
ReaderFunc represents a reader function.
func BatchReaderFunc ¶
func BatchReaderFunc[T any](f func(ptrs [2]*T, lens [2]int)) ReaderFunc
BatchReaderFunc returns a ReaderFunc that reads in batches. f is essentially a function that accepts a sub-slice of the internal ring buffer and is called twice:
1. First sub-slice is to the end of the ring buffer, 2. Secondsub-slice is from the beginning of the ring buffer.
It is possible the 2nd sub-slice is empty if n doesn't wrap around the ring buffer, i.e. length == 0.
Use BatchReaderFunc over SingleReaderFunc only if the complexity is needed and if the overhead of sub-slicing is much smaller than the time saved by batching, e.g. when working with SIMD code to read large numbers of items from the disruptor.
func SingleReaderFunc ¶
func SingleReaderFunc[T any](f func(*T)) ReaderFunc
SingleReaderFunc returns a ReaderFunc that reads one at a time.