We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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?
tonic
In golang, it can be written as
func fooInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { startTime := time.Now() err := handler(ctx, req) // elapsedTime := time.Since(startTime).Nanoseconds() // set header return err }
The text was updated successfully, but these errors were encountered:
Maybe the following answer can help you.
use tonic::{Request, Response, Status}; use std::time::Instant; async fn foo_interceptor<S>( mut req: Request<S>, next: tonic::service::Interceptor, ) -> Result<Response<S>, 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) }
Server::builder() .add_service( ServiceBuilder::new() .intercept(foo_interceptor) .service(your_service) ) .serve(addr) .await?;
Sorry, something went wrong.
@imotai Thanks for your suggestions, unfortunately, I was not able to make it work with next: tonic::service::Interceptor.
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
No branches or pull requests
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
The text was updated successfully, but these errors were encountered: