Open liubocflt opened 1 week ago
Maybe the following answer can help you.
use tonic::{Request, Response, Status};
use std::time::Instant;
async fn foo_interceptor(
mut req: Request,
next: tonic::service::Interceptor,
) -> Result<Response, Status> {
let start = Instant::now();
// Handle the request
let mut response = next.call(req).await?;
// Add custom headers to response
response.metadata_mut()
.insert("x-elapsed-time", elapsed.as_nanos().to_string().parse().unwrap());
Ok(response)
}
2. add the inteceptor to the server
```rust
Server::builder()
.add_service(
ServiceBuilder::new()
.intercept(foo_interceptor)
.service(your_service)
)
.serve(addr)
.await?;
@imotai Thanks for your suggestions, unfortunately, I was not able to make it work with next: tonic::service::Interceptor
.
I did it successfully with the example here, It's cumbersome to do it this way though. https://github.com/hyperium/tonic/blob/master/examples/src/tower/server.rs
Hi devs,
Thanks a lot for developing tonic for rust grpc.
I'm looking for a solution of
tonic
to set header in an interceptor when a request leaves the grpc server, is there a helper for this to do so?In golang, it can be written as