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