Request Queue (Rust)
Topics: backpressure, bounded queue, worker pool
Problem
Implement a bounded work queue drained by a fixed pool of worker threads:
pub enum QueueError { Full, Closed }
impl<T: Send + 'static> Queue<T> {
pub fn new<F: Fn(T) + Send + Sync + 'static>(capacity: usize, workers: usize, handler: F) -> Self;
pub fn submit(&self, item: T) -> Result<(), QueueError>;
pub fn close(&self);
}
new(capacity, workers, handler) starts workers threads, each running handler on items it
dequeues.
submit enqueues without blocking: Err(Full) when the buffer is full (load shedding),
Err(Closed) after close.
close stops accepting items, lets the workers drain everything already buffered, and joins
them before returning.
Hint: std::sync::mpsc::sync_channel(capacity) gives a bounded buffer whose try_send reports
Full vs Disconnected; share the Receiver behind an Arc<Mutex<…>> so workers pull from it.
Grading
Your solution.rs is compiled together with a trusted tests.rs (which include!s it) using
rustc --test. Only solution.rs is yours to edit.
Sign in to submit your solution.