Struct tokio_executor::threadpool::Builder

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

Builds a thread pool with custom configuration values.

Methods can be chained in order to set the configuration values. The thread pool is constructed by calling build.

New instances of Builder are obtained via Builder::new.

See function level documentation for details on the various configuration settings.

§Examples

use tokio_executor::threadpool::Builder;

use std::time::Duration;

let thread_pool = Builder::new()
    .pool_size(4)
    .keep_alive(Some(Duration::from_secs(30)))
    .build();

thread_pool.spawn(async {
    println!("called from a worker thread");
});

// Gracefully shutdown the threadpool
thread_pool.shutdown().wait();

Implementations§

source§

impl Builder

source

pub fn new() -> Builder

Returns a new thread pool builder initialized with default configuration values.

Configuration methods can be chained on the return value.

§Examples
use tokio_executor::threadpool::Builder;
use std::time::Duration;

let thread_pool = Builder::new()
    .pool_size(4)
    .keep_alive(Some(Duration::from_secs(30)))
    .build();
source

pub fn pool_size(&mut self, val: usize) -> &mut Self

Set the maximum number of worker threads for the thread pool instance.

This must be a number between 1 and 32,768 though it is advised to keep this value on the smaller side.

The default value is the number of cores available to the system.

§Examples
use tokio_executor::threadpool::Builder;

let thread_pool = Builder::new()
    .pool_size(4)
    .build();
source

pub fn max_blocking(&mut self, val: usize) -> &mut Self

Set the maximum number of concurrent blocking sections.

When the maximum concurrent blocking calls is reached, any further calls to blocking will return NotReady and the task is notified once previously in-flight calls to blocking return.

This must be a number between 1 and 32,768 though it is advised to keep this value on the smaller side.

The default value is 100.

§Examples
use tokio_executor::threadpool::Builder;

let thread_pool = Builder::new()
    .max_blocking(200)
    .build();
source

pub fn keep_alive(&mut self, val: Option<Duration>) -> &mut Self

Set the thread keep alive duration

If set, a thread that has completed a blocking call will wait for up to the specified duration to become a worker thread again. Once the duration elapses, the thread will shutdown.

When the value is None, the thread will wait to become a worker thread forever.

The default value is None.

§Examples
use tokio_executor::threadpool::Builder;
use std::time::Duration;

let thread_pool = Builder::new()
    .keep_alive(Some(Duration::from_secs(30)))
    .build();
source

pub fn panic_handler<F>(&mut self, f: F) -> &mut Self
where F: Fn(Box<dyn Any + Send>) + Send + Sync + 'static,

Sets a callback to be triggered when a panic during a future bubbles up to Tokio. By default Tokio catches these panics, and they will be ignored. The parameter passed to this callback is the same error value returned from std::panic::catch_unwind(). To abort the process on panics, use std::panic::resume_unwind() in this callback as shown below.

§Examples
use tokio_executor::threadpool::Builder;

let thread_pool = Builder::new()
    .panic_handler(|err| std::panic::resume_unwind(err))
    .build();
source

pub fn name_prefix<S: Into<String>>(&mut self, val: S) -> &mut Self

Set name prefix of threads spawned by the scheduler

Thread name prefix is used for generating thread names. For example, if prefix is my-pool-, then threads in the pool will get names like my-pool-1 etc.

If this configuration is not set, then the thread will use the system default naming scheme.

§Examples
use tokio_executor::threadpool::Builder;

let thread_pool = Builder::new()
    .name_prefix("my-pool-")
    .build();
source

pub fn stack_size(&mut self, val: usize) -> &mut Self

Set the stack size (in bytes) for worker threads.

The actual stack size may be greater than this value if the platform specifies minimal stack size.

The default stack size for spawned threads is 2 MiB, though this particular stack size is subject to change in the future.

§Examples
use tokio_executor::threadpool::Builder;

let thread_pool = Builder::new()
    .stack_size(32 * 1024)
    .build();
source

pub fn around_worker<F>(&mut self, f: F) -> &mut Self
where F: Fn(&Worker) + Send + Sync + 'static,

Execute function f on each worker thread.

This function is provided a handle to the worker and is expected to call Worker::run, otherwise the worker thread will shutdown without doing any work.

§Examples
use tokio_executor::threadpool::Builder;

let thread_pool = Builder::new()
    .around_worker(|worker| {
        println!("worker is starting up");
        worker.run();
        println!("worker is shutting down");
    })
    .build();
source

pub fn after_start<F>(&mut self, f: F) -> &mut Self
where F: Fn() + Send + Sync + 'static,

Execute function f after each thread is started but before it starts doing work.

This is intended for bookkeeping and monitoring use cases.

§Examples
use tokio_executor::threadpool::Builder;

let thread_pool = Builder::new()
    .after_start(|| {
        println!("thread started");
    })
    .build();
source

pub fn before_stop<F>(&mut self, f: F) -> &mut Self
where F: Fn() + Send + Sync + 'static,

Execute function f before each thread stops.

This is intended for bookkeeping and monitoring use cases.

§Examples
use tokio_executor::threadpool::Builder;

let thread_pool = Builder::new()
    .before_stop(|| {
        println!("thread stopping");
    })
    .build();
source

pub fn custom_park<F, P>(&mut self, f: F) -> &mut Self
where F: Fn(&WorkerId) -> P + 'static, P: Park + Send + 'static, P::Error: Error,

Customize the park instance used by each worker thread.

The provided closure f is called once per worker and returns a Park instance that is used by the worker to put itself to sleep.

§Examples
use tokio_executor::threadpool::Builder;
use tokio_executor::threadpool::park::DefaultPark;

let thread_pool = Builder::new()
    .custom_park(|_| {
        // This is the default park type that the worker would use if we
        // did not customize it.
        let park = DefaultPark::new();

        // Decorate the `park` instance, allowing us to customize work
        // that happens when a worker thread goes to sleep.
        decorate(park)
    })
    .build();
source

pub fn build(&self) -> ThreadPool

Create the configured ThreadPool.

The returned ThreadPool instance is ready to spawn tasks.

§Examples
use tokio_executor::threadpool::Builder;

let thread_pool = Builder::new()
    .build();

Trait Implementations§

source§

impl Debug for Builder

source§

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

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

impl Default for Builder

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for Builder

§

impl !RefUnwindSafe for Builder

§

impl !Send for Builder

§

impl !Sync for Builder

§

impl Unpin for Builder

§

impl !UnwindSafe for Builder

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