Struct ThreadPool

Source
pub struct ThreadPool {
    pub workers: Vec<Worker>,
    pub sender: Sender<Box<dyn FnOnce() + Send + 'static>>,
}
Expand description

The ThreadPool data structure holds the underlying mechanism for processing concurrent requests. It has a sender which propagates requests through the channel and a vector of workers.

Fields§

§workers: Vec<Worker>

This is an array of workers which constantly listen for requests to the server. The number of workers defaults to 10 but can be provided with the threads option.

§sender: Sender<Box<dyn FnOnce() + Send + 'static>>

This is a sender type used to propagate requests through a channel that every worker listens on.

Implementations§

Source§

impl ThreadPool

Source

pub fn new(pool_size: usize) -> ThreadPool

Returns a struct with the type ThreaPool. This method is used to initiate a new threadpool when the server is started. The workers/threads share ownership of a reciever which is used to accept requests.

Args

  • pool_size - This represents the number of workers/threads the server is started with.
§Examples
use server::thread::ThreadPool;
 
let pool: ThreadPool = ThreadPool::new(5);
assert_eq!(pool.workers.len(), 5);
§Panics

The associated function panics when the pool size provided is less that 1 or greater than 100.

Source

pub fn execute<F>(&self, f: F)
where F: FnOnce() + Send + 'static,

This function executes a closure f which essentially sends a stream of request down the channel. A worker in the pool recieves the request and executes it.

Args

  • f - A closure handle_connection.

The closure is then sent down the channel as a Job.

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.