Struct mio_pool::PoolBuilder
[−]
[src]
pub struct PoolBuilder<L> where
L: Listener, { /* fields omitted */ }
Used to configure a mio pool before launching it.
Users will want to call PoolBuilder::from
to start a new pool from a Listener
, and then
PoolBuilder::run
to begin accepting and handling connections.
Examples
let addr = "127.0.0.1:0".parse().unwrap(); let server = mio::net::TcpListener::bind(&addr).unwrap(); let pool = PoolBuilder::from(server).unwrap(); let h = pool.run(1 /* # workers */, |c: &mut mio::net::TcpStream, s: &mut ()| { use std::io::prelude::*; let mut buf = [0u8; 1024]; let n = c.read(&mut buf)?; if n == 0 { return Ok(true); } c.write_all(&buf[..n])?; Ok(false) }); // ... // during this period, new clients can connect // ... let results = h.terminate(); // results here contains the final state of each worker in the pool. // that is, the final value in each `s` passed to the closure in `run`. let result = results.into_iter().next().unwrap(); assert_eq!(result.unwrap(), ());
Methods
impl<L> PoolBuilder<L> where
L: 'static + Listener,
[src]
L: 'static + Listener,
fn from(listener: L) -> Result<Self>
[src]
Prepare a new pool from the given listener.
The pool will monitor the listener for new connections, and distribute the task of accepting them, and handling requests to accepted connections, among a pool of threads.
fn run_with_adapter<A, C, F, R>(
self,
workers: usize,
adapter: A,
on_ready: F
) -> PoolHandle<R> where
A: Fn(L::Connection) -> C + 'static + Send + Sync,
C: Evented + Send + 'static,
F: Fn(&mut C, &mut R) -> Result<bool> + 'static + Send + Sync,
R: 'static + Default + Send,
[src]
self,
workers: usize,
adapter: A,
on_ready: F
) -> PoolHandle<R> where
A: Fn(L::Connection) -> C + 'static + Send + Sync,
C: Evented + Send + 'static,
F: Fn(&mut C, &mut R) -> Result<bool> + 'static + Send + Sync,
R: 'static + Default + Send,
Run the pool with a custom adapter for every newly accepted connection.
This method behaves the same as PoolBuilder::run
, except that a function for adapting
accepted connections before using them can also be specified. This allows users to wrap
something akin to an TcpStream
into a more sophisticated connection type (e.g., by adding
buffering).
fn run<F, R>(self, workers: usize, on_ready: F) -> PoolHandle<R> where
F: Fn(&mut L::Connection, &mut R) -> Result<bool> + 'static + Send + Sync,
R: 'static + Default + Send,
[src]
F: Fn(&mut L::Connection, &mut R) -> Result<bool> + 'static + Send + Sync,
R: 'static + Default + Send,
Start accepting and handling connections using this pool.
The pool will consist of workers
worker threads that each accept new connections and
handle requests arriving at existing ones. Every time a connection has available data,
on_ready
will be called by one of the workers. A connection will stay in the pool until
on_ready
returns an error, or Ok(true)
indicating EOF.
Each worker also has local state of type R
. This state can be mutated by on_ready
, and
is returned when the pool exits.