January 2019
Intermediate to advanced
520 pages
14h 32m
English
Here, we'll reuse an existing microservice and remove the start_worker function, and the WorkerRequest and WorkerResult types. Keep the convert function and add a new dependency to Cargo.toml:
futures-cpupool = "0.1"
Import the CpuPool type from that crate:
use futures_cpupool::CpuPool;
The pool is now ready to use in the request handler. We can pass it as a parameter, like we did with the Sender of the worker thread in the previous example:
fn main() { let addr = ([127, 0, 0, 1], 8080).into(); let pool = CpuPool::new(4); let builder = Server::bind(&addr); let server = builder.serve(move || { let pool = pool.clone(); service_fn(move |req| microservice_handler(pool.clone(), req)) }); let server = server.map_err(drop); hyper::rt::run(server); ...Read now
Unlock full access