hyperium / tonic

A native gRPC client & server implementation with async/await support.
https://docs.rs/tonic
MIT License
9.35k stars 957 forks source link

Async Interceptor support #1700

Closed heartforit closed 1 month ago

heartforit commented 1 month ago

Hi,

I was wondering after hours of research how to implement a simple async interceptor method without tons of boilerplate code. I scanned the examples folder without success. In my opinion, it should be easy for the developer to write and handle async code with rust. My ugly solution for now is

fn my_interceptor_sync(request: Request<()>) -> Result<Request<()>, Status> {
    tokio::task::block_in_place(|| {
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(my_async_interceptor(request))
    })
}

But this code is pretty bad. How can I improve it? I could not find anything useful.

Best

heartforit commented 1 month ago

I tried: https://crates.io/crates/tonic-async-interceptor But it its apart from the current main implementation, but it seems to work.

heartforit commented 1 month ago

Works with this matching version combination (took me hours to figure that out)

[dependencies]
tonic = "0.11"
prost = "0.12"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
tower = "0.4.13"
futures-util = "0.3.30"
tonic-async-interceptor = "0.11.1"

[build-dependencies]
tonic-build = "0.11"