Struct tonic::Request [−][src]
pub struct Request<T> { /* fields omitted */ }
Expand description
A gRPC request and metadata from an RPC call.
Implementations
Create a new gRPC request.
Request::new(HelloRequest {
name: "Bob".into(),
});
Get a reference to the custom request metadata.
Get a mutable reference to the request metadata.
Consumes self
, returning the message
Get the remote address of this connection.
This will return None
if the IO
type used
does not implement Connected
. This currently,
only works on the server side.
Get the peer certificates of the connected client.
This is used to fetch the certificates from the TLS session
and is mostly used for mTLS. This currently only returns
Some
on the server side of the transport
server with
TLS enabled connections.
Set the max duration the request is allowed to take.
Requires the server to support the grpc-timeout
metadata, which Tonic does.
The duration will be formatted according to the spec and use the most precise unit possible.
Example:
use std::time::Duration;
use tonic::Request;
let mut request = Request::new(());
request.set_timeout(Duration::from_secs(30));
let value = request.metadata().get("grpc-timeout").unwrap();
assert_eq!(
value,
// equivalent to 30 seconds
"30000000u"
);
Returns a reference to the associated extensions.
Returns a mutable reference to the associated extensions.
Example
Extensions can be set in interceptors:
use tonic::{Request, service::interceptor};
struct MyExtension {
some_piece_of_data: String,
}
interceptor(|mut request: Request<()>| {
request.extensions_mut().insert(MyExtension {
some_piece_of_data: "foo".to_string(),
});
Ok(request)
});
And picked up by RPCs:
use tonic::{async_trait, Status, Request, Response};
#[async_trait]
impl TestService for MyService {
async fn handler(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
let value: &MyExtension = req.extensions().get::<MyExtension>().unwrap();
Ok(Response::new(Output {}))
}
}
Trait Implementations
Wrap the input message T
in a tonic::Request
Auto Trait Implementations
impl<T> !RefUnwindSafe for Request<T>
impl<T> !UnwindSafe for Request<T>
Blanket Implementations
Mutably borrows from an owned value. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more