broker_tokio::runtime

Struct Builder

Source
pub struct Builder { /* private fields */ }
Expand description

Builds Tokio Runtime with custom configuration values.

Methods can be chained in order to set the configuration values. The Runtime 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::runtime::Builder;

fn main() {
    // build runtime
    let runtime = Builder::new()
        .threaded_scheduler()
        .core_threads(4)
        .thread_name("my-custom-name")
        .thread_stack_size(3 * 1024 * 1024)
        .build()
        .unwrap();

    // use runtime ...
}

Implementations§

Source§

impl Builder

Source

pub fn new() -> Builder

Returns a new runtime builder initialized with default configuration values.

Configuration methods can be chained on the return value.

Source

pub fn enable_all(&mut self) -> &mut Self

Enable both I/O and time drivers.

Doing this is a shorthand for calling enable_io and enable_time individually. If additional components are added to Tokio in the future, enable_all will include these future components.

§Examples
use tokio::runtime;

let rt = runtime::Builder::new()
    .enable_all()
    .build()
    .unwrap();
Source

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

👎Deprecated: In future will be replaced by core_threads method

Set the maximum number of worker threads for the Runtime’s thread pool.

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.

Source

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

Set the core number of worker threads for the Runtime’s thread pool.

This should 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.

These threads will be always active and running.

§Examples
use tokio::runtime;

let rt = runtime::Builder::new()
    .core_threads(4)
    .build()
    .unwrap();
Source

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

Specifies limit for threads, spawned by the Runtime.

This is number of threads to be used by Runtime, including core_threads Having max_threads less than core_threads results in invalid configuration when building multi-threaded Runtime, which would cause a panic.

Similarly to the core_threads, this number should be between 1 and 32,768.

The default value is 512.

When multi-threaded runtime is not used, will act as limit on additional threads.

Otherwise as core_threads are always active, it limits additional threads (e.g. for blocking annotations) as max_threads - core_threads.

Source

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

Set name of threads spawned by the Runtime’s thread pool.

The default name is “tokio-runtime-worker”.

§Examples

let rt = runtime::Builder::new()
    .thread_name("my-pool")
    .build();
Source

pub fn thread_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

let rt = runtime::Builder::new()
    .thread_stack_size(32 * 1024)
    .build();
Source

pub fn on_thread_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

let runtime = runtime::Builder::new()
    .on_thread_start(|| {
        println!("thread started");
    })
    .build();
Source

pub fn on_thread_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

let runtime = runtime::Builder::new()
    .on_thread_stop(|| {
        println!("thread stopping");
    })
    .build();
Source

pub fn build(&mut self) -> Result<Runtime>

Create the configured Runtime.

The returned ThreadPool instance is ready to spawn tasks.

§Examples
use tokio::runtime::Builder;

let mut rt = Builder::new().build().unwrap();

rt.block_on(async {
    println!("Hello from the Tokio runtime");
});
Source§

impl Builder

Source

pub fn enable_io(&mut self) -> &mut Self

Available on crate feature io-driver only.

Enable the I/O driver.

Doing this enables using net, process, signal, and some I/O types on the runtime.

§Examples
use tokio::runtime;

let rt = runtime::Builder::new()
    .enable_io()
    .build()
    .unwrap();
Source§

impl Builder

Source

pub fn enable_time(&mut self) -> &mut Self

Available on crate feature time only.

Enable the time driver.

Doing this enables using tokio::time on the runtime.

§Examples
use tokio::runtime;

let rt = runtime::Builder::new()
    .enable_time()
    .build()
    .unwrap();
Source§

impl Builder

Source

pub fn basic_scheduler(&mut self) -> &mut Self

Use a simpler scheduler that runs all tasks on the current-thread.

The executor and all necessary drivers will all be run on the current thread during block_on calls.

Source§

impl Builder

Source

pub fn threaded_scheduler(&mut self) -> &mut Self

Available on crate feature rt-threaded only.

Use a multi-threaded scheduler for executing tasks.

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§

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

Source§

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

Source§

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.