A worker pool collectively handling a set of connections.
This crate is written for the use-case where a server is listening for connections, and wants
to spread the load of handling accepted connections across multiple threads. Specifically, this
crate implements a worker pool that shares a single mio::Poll
instance, and collectively
accept new connections and handle events for existing ones.
Users will want to start with the PoolBuilder
struct, which allows creating a new pool from
anything that can act as a Listener
(basically, anything that can be polled and accept new
connections that can themselves be polled; e.g., mio::net::TcpListener
).
```rust use std::io::prelude::*;
let addr = "127.0.0.1:0".parse().unwrap();
let server = mio::net::TcpListener::bind(&addr).unwrap();
let addr = server.localaddr().unwrap();
let pool = PoolBuilder::from(server).unwrap();
let h = pool.andreturn(|v| v)
.run(1 /* # workers */, |c: &mut mio::net::TcpStream, s: &mut Vecc
!
let mut buf = [0u8; 1024];
// let's just echo back what we read
let n = c.read(&mut buf)?;
if n == 0 {
return Ok(true);
}
c.write_all(&buf[..n])?;
// keep some internal state
s.extend(&buf[..n]);
// assume there could be more data
Ok(false)
});
// new clients can now connect on addr
use std::net::TcpStream;
let mut c = TcpStream::connect(&addr).unwrap();
c.writeall(b"hello world").unwrap();
let mut buf = [0u8; 1024];
let n = c.read(&mut buf).unwrap();
asserteq!(&buf[..n], b"hello world");
// we can terminate the pool at any time
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.intoiter().next().unwrap();
asserteq!(&result.unwrap(), b"hello world");
```