DisruptorBuilder

Struct DisruptorBuilder 

Source
pub struct DisruptorBuilder {}
Expand description

§Disruptor Builder Pattern Guide

The builder follows a type-state pattern to ensure compile-time correctness. Each step in the builder chain enforces required configuration in a specific order:

  1. Start with data provider (ring buffer)
  2. Configure waiting strategy
  3. Set up sequencer
  4. Add event handlers
  5. Build the final disruptor

§Example Usage

use disruptor_rs::{
    DisruptorBuilder, EventHandler, EventProcessorExecutor, EventProducer,
    sequence::Sequence,
};

#[derive(Default)]
struct MyEvent;

#[derive(Default)]
struct MyEventHandler;

impl EventHandler<MyEvent> for MyEventHandler {
    fn on_event(&self, _event: &MyEvent, _sequence: Sequence, _end_of_batch: bool) {}
    fn on_start(&self) {}
    fn on_shutdown(&self) {}
}

let (executor, producer) = DisruptorBuilder::with_ring_buffer::<MyEvent>(1024)
    .with_busy_spin_waiting_strategy()
    .with_single_producer_sequencer()
    .with_barrier(|scope| {
        scope.handle_events(MyEventHandler::default());
    })
    .build();

§Builder States

  • WithDataProvider: Initial state, holds the ring buffer
  • WithWaitingStrategy: Configures how consumers wait for new events
  • WithSequencer: Manages the sequencing of events
  • WithEventHandlers: Configures event processing chain

§Barrier Scopes

The with_barrier method creates scopes for configuring event handlers in a dependency chain. Handlers within the same barrier scope can run in parallel, while different barrier scopes run sequentially.

use disruptor_rs::{
    DisruptorBuilder, EventHandler, EventProcessorExecutor, EventProducer,
    sequence::Sequence,
};

#[derive(Default)]
struct MyEvent;

#[derive(Default)]
struct MyEventHandler;

impl EventHandler<MyEvent> for MyEventHandler {
    fn on_event(&self, _event: &MyEvent, _sequence: Sequence, _end_of_batch: bool) {}
    fn on_start(&self) {}
    fn on_shutdown(&self) {}
}

let (executor, producer) = DisruptorBuilder::with_ring_buffer::<MyEvent>(1024)
    .with_busy_spin_waiting_strategy()
    .with_single_producer_sequencer()
    .with_barrier(|scope| {
        // These handlers run in parallel
        scope.handle_events(MyEventHandler::default());
        scope.handle_events(MyEventHandler::default());
    })
    .with_barrier(|scope| {
        // This handler waits for both previous handlers
        scope.handle_events(MyEventHandler::default());
    })
    .build();

Implementations§

Source§

impl DisruptorBuilder

Source

pub fn new<D: DataProvider<T>, T>( data_provider: Arc<D>, ) -> WithDataProvider<D, T>
where T: Send + Sync,

Source

pub fn with_ring_buffer<T>( capacity: usize, ) -> WithDataProvider<RingBuffer<T>, T>
where T: Default + Send + Sync,

Trait Implementations§

Source§

impl Debug for DisruptorBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.