stepancheg / grpc-rust

Rust implementation of gRPC
MIT License
1.37k stars 124 forks source link

grpc service needs to mutate self #145

Closed david415 closed 5 years ago

david415 commented 5 years ago

The generated code doesn't seem to work for my use case since the grpc methods are called with &self instead of &mut self. However, I suspect plenty of other services besides mine will want to mutate state. Am I missing something?

here's my project branch attempting to use this library: https://github.com/katzenpost/multispool/tree/service_multi_spool.0

stepancheg commented 5 years ago

The service implementation object can be invoked from multiple threads (not in the default setup, but it is possible).

If you want to mutate data, you can guard the data with mutex:

struct MyServiceData {
    ...
}

struct MyServiceImpl {
    data: Mutex<MyServiceData>,
}

impl MyService for MyServiceImpl {
    fn call(&self, req: Req) -> Future<Resp> {
        let data = self.data.lock().unwrap();
        data.mutate()
    }
}