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
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//! # #[cfg(feature = "rustls")]
22//! # use tonic::transport::{Channel, Certificate, ClientTlsConfig};
23//! # use std::time::Duration;
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(tonic::body::empty_body())).await?;
41//! # Ok(())
42//! # }
43//! ```
44//!
45//! ## Server
46//!
47//! ```no_run
48//! # use std::convert::Infallible;
49//! # #[cfg(feature = "rustls")]
50//! # use tonic::transport::{Server, Identity, ServerTlsConfig};
51//! # use tonic::body::Body;
52//! # use tower::Service;
53//! # #[cfg(feature = "rustls")]
54//! # async fn do_thing() -> Result<(), Box<dyn std::error::Error>> {
55//! # #[derive(Clone)]
56//! # pub struct Svc;
57//! # impl Service<hyper::Request<Body>> for Svc {
58//! # type Response = hyper::Response<Body>;
59//! # type Error = Infallible;
60//! # type Future = std::future::Ready<Result<Self::Response, Self::Error>>;
61//! # fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
62//! # Ok(()).into()
63//! # }
64//! # fn call(&mut self, _req: hyper::Request<Body>) -> Self::Future {
65//! # unimplemented!()
66//! # }
67//! # }
68//! # impl tonic::server::NamedService for Svc {
69//! # const NAME: &'static str = "some_svc";
70//! # }
71//! # let my_svc = Svc;
72//! let cert = std::fs::read_to_string("server.pem")?;
73//! let key = std::fs::read_to_string("server.key")?;
74//!
75//! let addr = "[::1]:50051".parse()?;
76//!
77//! Server::builder()
78//! .tls_config(ServerTlsConfig::new()
79//! .identity(Identity::from_pem(&cert, &key)))?
80//! .concurrency_limit_per_connection(256)
81//! .add_service(my_svc)
82//! .serve(addr)
83//! .await?;
84//!
85//! # Ok(())
86//! # }
87//! ```
88//!
89//! [rustls]: https://docs.rs/rustls/0.16.0/rustls/
90
91#[cfg(feature = "channel")]
92pub mod channel;
93#[cfg(feature = "server")]
94pub mod server;
95
96mod error;
97mod service;
98#[cfg(feature = "_tls-any")]
99mod tls;
100
101#[doc(inline)]
102#[cfg(feature = "channel")]
103pub use self::channel::{Channel, Endpoint};
104pub use self::error::Error;
105#[doc(inline)]
106#[cfg(feature = "server")]
107pub use self::server::Server;
108
109#[cfg(feature = "_tls-any")]
110pub use self::tls::Certificate;
111pub use hyper::{body::Body, Uri};
112#[cfg(feature = "_tls-any")]
113pub use tokio_rustls::rustls::pki_types::CertificateDer;
114
115#[cfg(all(feature = "channel", feature = "_tls-any"))]
116pub use self::channel::ClientTlsConfig;
117#[cfg(all(feature = "server", feature = "_tls-any"))]
118pub use self::server::ServerTlsConfig;
119#[cfg(feature = "_tls-any")]
120pub use self::tls::Identity;