Skip to main content

EventPoller

Struct EventPoller 

Source
pub struct EventPoller<E, B, N: WakeupNotifier = NoopWakeupNotifier> { /* private fields */ }
Expand description

Represents an EventPoller that can be used to poll events from the ring buffer. Use the EventPoller when you want to control your own thread (instead of letting the Disruptor manage it). The EventPoller supports batch reading and can be used to process events in a loop.

When the EventGuard is dropped, it advances the consumer cursor and wakes any downstream consumers that may be blocked (when using blocking wait strategies).

loop {
    // 1. Either poll all available events:
    match event_poller.poll() {
        Ok(mut events) => {
            // Batch process events if efficient in your use case.
            let batch_size = (&mut events).len();
            // The guard named `events` is an `Iterator` so you can read events by iterating.
            for (sequence, event) in &mut events {
                println!("Processing event: {:?}", event);
                let _ = sequence;
            }
        },// When the guard (here named `events`) goes out of scope,
          // it signals to the Disruptor that reading is done.
        Err(Polling::NoEvents) => { /* Do other work or try again. */ },
        Err(Polling::Shutdown) => { break; }, // Exit the loop if the Disruptor is shut down.
    }
    // 2. Or limit the number of events per poll:
    match event_poller.poll_take(64) {
        Ok(mut events) => {
            // Process events same as above but yielding at most 64 events.
            for (sequence, event) in &mut events {
                println!("Processing event: {:?}", event);
                let _ = sequence;
            }
        },
        Err(Polling::NoEvents) => {},
        Err(Polling::Shutdown) => { break; },
    }
}

Implementations§

Source§

impl<E, B, N> EventPoller<E, B, N>
where B: Barrier, N: WakeupNotifier,

Source

pub fn poll(&mut self) -> Result<EventGuard<'_, E, B, N>, Polling>

Polls the ring buffer and returns an EventGuard if any events are available. The guard can be used like an iterator and yields all available events at the time of polling. Dropping the guard will signal to the Disruptor that the events have been processed.

This method does not block; it will return immediately.

This method is equivalent to calling EventPoller::poll_take with u64::MAX as the limit.

§Errors

This method returns an error if:

  1. No events are available: Polling::NoEvents
  2. The Disruptor is shut down: Polling::Shutdown
§Examples
match event_poller.poll() {
    Ok(mut events) => {
        for (sequence, event) in &mut events {
            let _ = sequence;
            let _ = event;
        }
    },
    Err(Polling::NoEvents) => { /* ... */ },
    Err(Polling::Shutdown) => { /* ... */ },
};
Source

pub fn poll_take( &mut self, limit: u64, ) -> Result<EventGuard<'_, E, B, N>, Polling>

Polls for available events, yielding at most limit events.

This method behaves like EventPoller::poll, but caps the number of events yielded by the returned EventGuard. Fewer events may be yielded if less are available at the time of polling.

Note: A limit of zero returns an EventGuard that yields no events (and not a Polling::NoEvents error).

§Examples
match event_poller.poll_take(64) {
    Ok(mut events) => {
        // Process events same as above but yielding at most 64 events.
        for (sequence, event) in &mut events {
            let _ = sequence;
            let _ = event;
        }
    },
    Err(Polling::NoEvents) => { /* ... */ },
    Err(Polling::Shutdown) => { /* ... */ },
};
Source

pub fn poll_wait( &mut self, waiter: &mut impl Waiter, ) -> Result<EventGuard<'_, E, B, N>, Polling>

Wait for events using the provided Waiter and then return an EventGuard.

This is useful when you want to drive an EventPoller from your own thread while still reusing the library’s wait strategies (e.g. BusySpin, YieldingWaitStrategy).

If the waiter returns WaitOutcome::Timeout, this returns Polling::NoEvents so the caller can do other work.

Auto Trait Implementations§

§

impl<E, B, N = NoopWakeupNotifier> !RefUnwindSafe for EventPoller<E, B, N>

§

impl<E, B, N = NoopWakeupNotifier> !UnwindSafe for EventPoller<E, B, N>

§

impl<E, B, N> Freeze for EventPoller<E, B, N>
where Arc<RingBuffer<E>>: Freeze, Arc<B>: Freeze, N: Freeze,

§

impl<E, B, N> Send for EventPoller<E, B, N>
where Arc<RingBuffer<E>>: Send, Arc<B>: Send,

§

impl<E, B, N> Sync for EventPoller<E, B, N>
where Arc<RingBuffer<E>>: Sync, Arc<B>: Sync,

§

impl<E, B, N> Unpin for EventPoller<E, B, N>
where Arc<RingBuffer<E>>: Unpin, Arc<B>: Unpin, N: Unpin,

§

impl<E, B, N> UnsafeUnpin for EventPoller<E, B, N>
where Arc<RingBuffer<E>>: UnsafeUnpin, Arc<B>: UnsafeUnpin, N: UnsafeUnpin,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.