tonic/transport/mod.rs
1//! Batteries included server and client.
2//!
3//! This module provides a set of batteries included, fully featured and
4//! fast set of HTTP/2 server and client's. These components each provide a or
5//! `rustls` tls backend when the respective feature flag is enabled, and
6//! provides builders to configure transport behavior.
7//!
8//! # Features
9//!
10//! - TLS support via [rustls].
11//! - Load balancing
12//! - Timeouts
13//! - Concurrency Limits
14//! - Rate limiting
15//!
16//! # Examples
17//!
18//! ## Client
19//!
20//! ```no_run
21//! # use tonic::transport::{Channel, Certificate, ClientTlsConfig};
22//! # use std::time::Duration;
23//! # use tonic::body::BoxBody;
24//! # use tonic::client::GrpcService;;
25//! # use http::Request;
26//! # #[cfg(feature = "rustls")]
27//! # async fn do_thing() -> Result<(), Box<dyn std::error::Error>> {
28//! let cert = std::fs::read_to_string("ca.pem")?;
29//!
30//! let mut channel = Channel::from_static("https://example.com")
31//! .tls_config(ClientTlsConfig::new()
32//! .ca_certificate(Certificate::from_pem(&cert))
33//! .domain_name("example.com".to_string()))?
34//! .timeout(Duration::from_secs(5))
35//! .rate_limit(5, Duration::from_secs(1))
36//! .concurrency_limit(256)
37//! .connect()
38//! .await?;
39//!
40//! channel.call(Request::new(BoxBody::empty())).await?;
41//! # Ok(())
42//! # }
43//! ```
44//!
45//! ## Server
46//!
47//! ```no_run
48//! # use tonic::transport::{Server, Identity, ServerTlsConfig};
49//! # use tower::{Service, service_fn};
50//! # use futures_util::future::{err, ok};
51//! # #[cfg(feature = "rustls")]
52//! # async fn do_thing() -> Result<(), Box<dyn std::error::Error>> {
53//! # #[derive(Clone)]
54//! # pub struct Svc;
55//! # impl Service<hyper::Request<hyper::Body>> for Svc {
56//! # type Response = hyper::Response<tonic::body::BoxBody>;
57//! # type Error = tonic::Status;
58//! # type Future = futures_util::future::Ready<Result<Self::Response, Self::Error>>;
59//! # fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
60//! # Ok(()).into()
61//! # }
62//! # fn call(&mut self, _req: hyper::Request<hyper::Body>) -> Self::Future {
63//! # unimplemented!()
64//! # }
65//! # }
66//! # impl tonic::transport::NamedService for Svc {
67//! # const NAME: &'static str = "some_svc";
68//! # }
69//! # let my_svc = Svc;
70//! let cert = std::fs::read_to_string("server.pem")?;
71//! let key = std::fs::read_to_string("server.key")?;
72//!
73//! let addr = "[::1]:50051".parse()?;
74//!
75//! Server::builder()
76//! .tls_config(ServerTlsConfig::new()
77//! .identity(Identity::from_pem(&cert, &key)))?
78//! .concurrency_limit_per_connection(256)
79//! .add_service(my_svc)
80//! .serve(addr)
81//! .await?;
82//!
83//! # Ok(())
84//! # }
85//! ```
86//!
87//! [rustls]: https://docs.rs/rustls/0.16.0/rustls/
88
89pub mod channel;
90pub mod server;
91
92mod error;
93mod service;
94mod tls;
95
96#[doc(inline)]
97pub use self::channel::{Channel, Endpoint};
98pub use self::error::Error;
99#[doc(inline)]
100pub use self::server::{NamedService, Server};
101#[doc(inline)]
102pub use self::service::TimeoutExpired;
103pub use self::tls::Certificate;
104pub use hyper::{Body, Uri};
105
106#[cfg(feature = "tls")]
107#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
108pub use self::channel::ClientTlsConfig;
109#[cfg(feature = "tls")]
110#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
111pub use self::server::ServerTlsConfig;
112#[cfg(feature = "tls")]
113#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
114pub use self::tls::Identity;
115
116type BoxFuture<T, E> =
117 std::pin::Pin<Box<dyn std::future::Future<Output = Result<T, E>> + Send + 'static>>;