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,
impl<E, B, N> EventPoller<E, B, N>where
B: Barrier,
N: WakeupNotifier,
Sourcepub fn poll(&mut self) -> Result<EventGuard<'_, E, B, N>, Polling>
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:
- No events are available:
Polling::NoEvents - 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) => { /* ... */ },
};Sourcepub fn poll_take(
&mut self,
limit: u64,
) -> Result<EventGuard<'_, E, B, N>, Polling>
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) => { /* ... */ },
};Sourcepub fn poll_wait(
&mut self,
waiter: &mut impl Waiter,
) -> Result<EventGuard<'_, E, B, N>, Polling>
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.