Accessing a shared state from a service function

To get access to a shared state, you need to provide a reference to the shared data. This is simple, because we've already wrapped our state with Arc, which provides us with a clone() function to duplicate the reference to the shared object.

Since our service function needs extra parameters, we have to rewrite the definition and call our microservice_handler function. Now it has an extra argument, which is the reference to the shared state:

fn microservice_handler(req: Request<Body>, user_db: &UserDb)
-> impl Future<Item=Response<Body>, Error=Error>

We also have to send this expected reference to the main function:

fn main() {
let addr = ([127, 0, 0, 1], 8080).into();
let builder = Server::bind(&addr);
let user_db = Arc::new(Mutex::new(Slab::new()));
let server = builder.serve(move || {
let user_db = user_db.clone();
service_fn(move |req| microservice_handler(req, &user_db))
});
let server = server.map_err(drop);
hyper::rt::run(server);
}

As you can see, we created a Slab and wrapped it with Mutex and Arc. After that, we  moved the object, called user_db, into the serve function call of the server builder that's using the move keyword. When the reference moves into the closure, we can send it to microservice_handler. This is a handler function called by a closure sent to the service_fn call. We have to clone the reference to move it to a nested closure, because that closure can be called multiple times. We shouldn't move the object completely, however, because a closure sent to the serve function can be called multiple times and so the runtime might need the object again later.

In other words, both closures can be called multiple times. The closure of service_fn will be called in the same thread as the runtime, and we can use a reference for the value inside it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.60.249