Struct iroh_blobs::util::local_pool::LocalPool

source ·
pub struct LocalPool { /* private fields */ }
Expand description

A local task pool with proper shutdown

Unlike LocalPoolHandle, this pool will join all its threads when dropped, ensuring that all Drop implementations are run to completion.

On drop, this pool will immediately cancel all tasks that are currently being executed, and will wait for all threads to finish executing their loops before returning. This means that all drop implementations will be able to run to completion before drop exits.

On LocalPool::finish, this pool will notify all threads to shut down, and then wait for all threads to finish executing their loops before returning. This means that all currently executing tasks will be allowed to run to completion.

The pool will install the [tracing::Subscriber] which was set on the current thread of where it was created as the default subscriber in all spawned threads.

Implementations§

source§

impl LocalPool

source

pub fn single() -> Self

Create a new local pool with a single std thread.

source

pub fn new(config: Config) -> Self

Create a new local pool with the given config.

This will use the current tokio runtime handle, so it must be called from within a tokio runtime.

source

pub fn handle(&self) -> &LocalPoolHandle

Get a cheaply cloneable handle to the pool

This is not strictly necessary since we implement deref for LocalPoolHandle, but makes getting a handle more explicit.

source

pub async fn cancelled(&self)

A future that resolves when the pool is cancelled

source

pub async fn shutdown(self)

Immediately stop polling all tasks and wait for all threads to finish.

This is like drop, but waits for thread completion asynchronously.

If there was a panic on any of the threads, it will be re-thrown here.

source

pub async fn finish(self)

Gently shut down the pool

Notifies all the pool threads to shut down and waits for them to finish.

If you just want to drop the pool without giving the threads a chance to process their remaining tasks, just use Self::shutdown.

If you want to wait for only a limited time for the tasks to finish, you can race this function with a timeout.

Methods from Deref<Target = LocalPoolHandle>§

source

pub fn waiting_tasks(&self) -> usize

Get the number of tasks in the queue

This is not the number of tasks being executed, but the number of tasks waiting to be scheduled for execution. If this number is high, it indicates that the pool is very busy.

You might want to use this to throttle or reject requests.

source

pub fn try_spawn<T, F, Fut>(&self, gen: F) -> Result<Run<T>, SpawnError>
where F: FnOnce() -> Fut + Send + 'static, Fut: Future<Output = T> + 'static, T: Send + 'static,

Spawn a task in the pool and return a future that resolves when the task is done.

If you don’t care about the result, prefer LocalPoolHandle::spawn_detached since it is more efficient.

source

pub fn try_spawn_detached<F, Fut>(&self, gen: F) -> Result<(), SpawnError>
where F: FnOnce() -> Fut + Send + 'static, Fut: Future<Output = ()> + 'static,

Spawn a task in the pool.

The task will run to completion unless the pool is shut down or the task panics. In case of panic, the pool will either log the panic and continue or immediately shut down, depending on the PanicMode.

source

pub fn spawn<T, F, Fut>(&self, gen: F) -> Run<T>
where F: FnOnce() -> Fut + Send + 'static, Fut: Future<Output = T> + 'static, T: Send + 'static,

Spawn a task in the pool and await the result.

Like LocalPoolHandle::try_spawn, but panics if the pool is shut down.

source

pub fn spawn_detached<F, Fut>(&self, gen: F)
where F: FnOnce() -> Fut + Send + 'static, Fut: Future<Output = ()> + 'static,

Spawn a task in the pool.

Like LocalPoolHandle::try_spawn_detached, but panics if the pool is shut down.

source

pub fn try_spawn_detached_boxed( &self, gen: Box<dyn FnOnce() -> Pin<Box<dyn Future<Output = ()>>> + Send + 'static> ) -> Result<(), SpawnError>

Spawn a task in the pool.

This is like LocalPoolHandle::try_spawn_detached, but assuming that the generator function is already boxed. This is the lowest overhead way to spawn a task in the pool.

Trait Implementations§

source§

impl Debug for LocalPool

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for LocalPool

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Deref for LocalPool

§

type Target = LocalPoolHandle

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl Drop for LocalPool

source§

fn drop(&mut self)

Executes the destructor for this type. 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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T