Closed anton-dutov closed 6 years ago
The rule of thumb is that anything that is socket specific should not be at the Service
layer (there are, of course, exceptions). It can be done at the transport layer, which is per socket: https://tokio.rs/docs/going-deeper-tokio/transports/.
That said, it can also be possible to do at the Service
layer once NewService
returns a future:
impl NewService for MyService {
fn new_service(&self) -> Self::Future {
let token = self.token.clone();
self.proto.new_service()
.and_then(|s| s.call(Auth(token))
.and_then(|resp| {
validate_resp(resp)?
// return authenticated service
})
}
}
So, where are save state between requests?
If you want to save state between requests, you should use an Rc
or Arc
somewhere. The "somewhere" depends on the details of what you want the state to be shared across.
By example, some rpc service with persistent connections, typical work is
At step two we created auth token and bound to socket (all socket's have bounded HashMap, where state saves), how to impl that via Service?