Struct tonic::Request

source ·
pub struct Request<T> { /* private fields */ }
Expand description

A gRPC request and metadata from an RPC call.

Implementations§

source§

impl<T> Request<T>

source

pub fn new(message: T) -> Self

Create a new gRPC request.

Request::new(HelloRequest {
   name: "Bob".into(),
});
source

pub fn get_ref(&self) -> &T

Get a reference to the message

source

pub fn get_mut(&mut self) -> &mut T

Get a mutable reference to the message

source

pub fn metadata(&self) -> &MetadataMap

Get a reference to the custom request metadata.

source

pub fn metadata_mut(&mut self) -> &mut MetadataMap

Get a mutable reference to the request metadata.

source

pub fn into_inner(self) -> T

Consumes self, returning the message

source

pub fn into_parts(self) -> (MetadataMap, Extensions, T)

Consumes self returning the parts of the request.

source

pub fn from_parts( metadata: MetadataMap, extensions: Extensions, message: T, ) -> Self

Create a new gRPC request from metadata, extensions and message.

source

pub fn from_http(http: Request<T>) -> Self

Convert an HTTP request to a gRPC request

source

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.

source

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.

source

pub fn peer_certs(&self) -> Option<Arc<Vec<CertificateDer<'static>>>>

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.

source

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"
);
source

pub fn extensions(&self) -> &Extensions

Returns a reference to the associated extensions.

source

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: Debug> Debug for Request<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> IntoRequest<T> for Request<T>

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T> IntoStreamingRequest for Request<T>
where T: Stream + Send + 'static,

§

type Stream = T

The RPC request stream type
§

type Message = <T as Stream>::Item

The RPC request type
source§

fn into_streaming_request(self) -> Self

Wrap the stream of messages 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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more