pub struct WorkerPool<T: Send + 'static, R: Send + 'static> { /* private fields */ }Expand description
A dynamically-scaling pool of Tokio worker tasks.
Generic over:
T— the input type passed to each work item (must beSend + 'static)R— the result type returned by the handler (must beSend + 'static)
Implementations§
Source§impl<T: Send + 'static, R: Send + 'static> WorkerPool<T, R>
impl<T: Send + 'static, R: Send + 'static> WorkerPool<T, R>
Sourcepub fn new(
config: WorkerConfig,
handler: Arc<dyn Fn(T) -> BoxFuture<'static, R> + Send + Sync>,
) -> Self
pub fn new( config: WorkerConfig, handler: Arc<dyn Fn(T) -> BoxFuture<'static, R> + Send + Sync>, ) -> Self
Create a new pool with the given configuration and async handler function.
min_workers worker tasks are spawned immediately.
Sourcepub async fn submit(&self, item: T, priority: u8) -> Receiver<R>
pub async fn submit(&self, item: T, priority: u8) -> Receiver<R>
Submit a work item with the given priority (higher = more urgent).
Returns a [oneshot::Receiver<R>] that resolves when processing is done.
If the queue is full (bounded channel) this will apply back-pressure and
await until space is available.
Sourcepub fn scale_down(&self)
pub fn scale_down(&self)
Signal one idle worker above min_workers to exit on its next idle timeout.
This is a best-effort hint; the worker may be processing an item and will only exit after completing it and then timing out.
Sourcepub async fn drain_and_shutdown(&self)
pub async fn drain_and_shutdown(&self)
Wait for the queue to drain completely, then stop all workers.
After this method returns the pool must not be used again.
Auto Trait Implementations§
impl<T, R> !RefUnwindSafe for WorkerPool<T, R>
impl<T, R> !UnwindSafe for WorkerPool<T, R>
impl<T, R> Freeze for WorkerPool<T, R>
impl<T, R> Send for WorkerPool<T, R>
impl<T, R> Sync for WorkerPool<T, R>
impl<T, R> Unpin for WorkerPool<T, R>
impl<T, R> UnsafeUnpin for WorkerPool<T, R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request