pub struct Request<T> { /* private fields */ }
Expand description
A gRPC request and metadata from an RPC call.
Implementations§
Source§impl<T> Request<T>
impl<T> Request<T>
Sourcepub fn new(message: T) -> Self
pub fn new(message: T) -> Self
Create a new gRPC request.
Request::new(HelloRequest {
name: "Bob".into(),
});
Sourcepub fn metadata(&self) -> &MetadataMap
pub fn metadata(&self) -> &MetadataMap
Get a reference to the custom request metadata.
Sourcepub fn metadata_mut(&mut self) -> &mut MetadataMap
pub fn metadata_mut(&mut self) -> &mut MetadataMap
Get a mutable reference to the request metadata.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes self
, returning the message
Sourcepub fn into_parts(self) -> (MetadataMap, Extensions, T)
pub fn into_parts(self) -> (MetadataMap, Extensions, T)
Consumes self
returning the parts of the request.
Sourcepub fn from_parts(
metadata: MetadataMap,
extensions: Extensions,
message: T,
) -> Self
pub fn from_parts( metadata: MetadataMap, extensions: Extensions, message: T, ) -> Self
Create a new gRPC request from metadata, extensions and message.
Sourcepub fn local_addr(&self) -> Option<SocketAddr>
pub fn local_addr(&self) -> Option<SocketAddr>
Get the local address of this connection.
This will return None
if the IO
type used
does not implement Connected
or when using a unix domain socket.
This currently only works on the server side.
Sourcepub fn remote_addr(&self) -> Option<SocketAddr>
pub fn remote_addr(&self) -> Option<SocketAddr>
Get the remote address of this connection.
This will return None
if the IO
type used
does not implement Connected
or when using a unix domain socket.
This currently only works on the server side.
Sourcepub fn set_timeout(&mut self, deadline: Duration)
pub fn set_timeout(&mut self, deadline: Duration)
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"
);
Sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Returns a reference to the associated extensions.
Sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Returns a mutable reference to the associated extensions.
§Example
Extensions can be set in interceptors:
use tonic::{Request, service::interceptor};
#[derive(Clone)] // Extensions must be Clone
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§
Source§impl<T> IntoRequest<T> for Request<T>
impl<T> IntoRequest<T> for Request<T>
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Auto Trait Implementations§
impl<T> Freeze for Request<T>where
T: Freeze,
impl<T> !RefUnwindSafe for Request<T>
impl<T> Send for Request<T>where
T: Send,
impl<T> Sync for Request<T>where
T: Sync,
impl<T> Unpin for Request<T>where
T: Unpin,
impl<T> !UnwindSafe for Request<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request