stepancheg / grpc-rust

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

Pass context to rpc handlers #70

Closed rschulman closed 7 years ago

rschulman commented 7 years ago

It would be nice to be able to pass some arbitrary type to the rpc handlers from the server, in order to gain access to some global state across the entire application. I've been looking through the code, but I'm not seeing any way to do this yet.

If it isn't a feature yet, it seems like it would require a modification to the MethodHandlers in server.rs?

stepancheg commented 7 years ago

I'm not sure you need to pass context to each method.

When you implement grpc service, you implement generated service trait, e. g.

// generated trait by protoc-gen-rust-grpc
trait HelloService { ... }

struct MyHelloServiceImpl {
    // HERE you can store global state
}

impl HelloService for MyHelloServiceImpl { ... }
rschulman commented 7 years ago

Ok, that's interesting. Do you then create an instance of MyHelloServiceImpl and pass that to HelloServiceAsyncServer::new()?

stepancheg commented 7 years ago

Yes, exactly.