From 57b506af81672e76d64365e4c6e29a6a000f08db Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 20 Jun 2024 21:31:25 +0900 Subject: [PATCH 1/6] chore: Refactor redundant pattern match --- tonic/src/codec/decode.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tonic/src/codec/decode.rs b/tonic/src/codec/decode.rs index 95bf90d05..742660e31 100644 --- a/tonic/src/codec/decode.rs +++ b/tonic/src/codec/decode.rs @@ -291,13 +291,11 @@ impl StreamingInner { fn response(&mut self) -> Result<(), Status> { if let Direction::Response(status) = self.direction { - if let Err(e) = crate::status::infer_grpc_status(self.trailers.as_ref(), status) { - if let Some(e) = e { - // If the trailers contain a grpc-status, then we should return that as the error - // and otherwise stop the stream (by taking the error state) - self.trailers.take(); - return Err(e); - } + if let Err(Some(e)) = crate::status::infer_grpc_status(self.trailers.as_ref(), status) { + // If the trailers contain a grpc-status, then we should return that as the error + // and otherwise stop the stream (by taking the error state) + self.trailers.take(); + return Err(e); } } Ok(()) From c73f3319c1cf670ffe5b06c637eb7beedaba82af Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 20 Jun 2024 21:34:22 +0900 Subject: [PATCH 2/6] chore: Refactor redundant closure style --- tonic/src/service/router.rs | 7 ++----- tonic/src/transport/channel/service/connection.rs | 6 +----- tonic/src/transport/channel/service/connector.rs | 2 +- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/tonic/src/service/router.rs b/tonic/src/service/router.rs index 690d28fc2..e7f864f8d 100644 --- a/tonic/src/service/router.rs +++ b/tonic/src/service/router.rs @@ -168,10 +168,7 @@ where } fn call(&mut self, req: Request) -> Self::Future { - let fut = self.service.call(req.map(|body| boxed(body))); - Box::pin(async move { - fut.await - .map(|res| res.map(|body| axum::body::Body::new(body))) - }) + let fut = self.service.call(req.map(boxed)); + Box::pin(async move { fut.await.map(|res| res.map(axum::body::Body::new)) }) } } diff --git a/tonic/src/transport/channel/service/connection.rs b/tonic/src/transport/channel/service/connection.rs index 3d8ce0d1e..e2a40fbea 100644 --- a/tonic/src/transport/channel/service/connection.rs +++ b/tonic/src/transport/channel/service/connection.rs @@ -147,11 +147,7 @@ impl tower::Service> for SendRequest { fn call(&mut self, req: Request) -> Self::Future { let fut = self.inner.send_request(req); - Box::pin(async move { - fut.await - .map_err(Into::into) - .map(|res| res.map(|body| boxed(body))) - }) + Box::pin(async move { fut.await.map_err(Into::into).map(|res| res.map(boxed)) }) } } diff --git a/tonic/src/transport/channel/service/connector.rs b/tonic/src/transport/channel/service/connector.rs index 47628daf6..2e8291c04 100644 --- a/tonic/src/transport/channel/service/connector.rs +++ b/tonic/src/transport/channel/service/connector.rs @@ -118,7 +118,7 @@ where Ok::<_, crate::Error>(BoxedIo::new(io)) } .await - .map_err(|err| ConnectError(From::from(err))) + .map_err(ConnectError) }) } } From 76bcb49015291d92a60e6266bdf0044a0fba61f5 Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 20 Jun 2024 21:58:07 +0900 Subject: [PATCH 3/6] chore(examples): Remove redundant type converting --- examples/src/tls_rustls/server.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/src/tls_rustls/server.rs b/examples/src/tls_rustls/server.rs index 0fb31f8fa..202a333a1 100644 --- a/examples/src/tls_rustls/server.rs +++ b/examples/src/tls_rustls/server.rs @@ -26,7 +26,6 @@ async fn main() -> Result<(), Box> { let fd = std::fs::File::open(data_dir.join("tls/server.pem"))?; let mut buf = std::io::BufReader::new(&fd); rustls_pemfile::certs(&mut buf) - .into_iter() .map(|res| res.map(|cert| cert.to_owned())) .collect::, _>>()? }; @@ -34,7 +33,6 @@ async fn main() -> Result<(), Box> { let fd = std::fs::File::open(data_dir.join("tls/server.key"))?; let mut buf = std::io::BufReader::new(&fd); let key = rustls_pemfile::pkcs8_private_keys(&mut buf) - .into_iter() .next() .unwrap()? .clone_key(); From b2f8094f12e391575e24f8295a4740dba7fb0799 Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 20 Jun 2024 22:06:20 +0900 Subject: [PATCH 4/6] chore(examples): Use rustls_pemfile::private_key to parse private key --- examples/src/tls_rustls/server.rs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/examples/src/tls_rustls/server.rs b/examples/src/tls_rustls/server.rs index 202a333a1..65674a788 100644 --- a/examples/src/tls_rustls/server.rs +++ b/examples/src/tls_rustls/server.rs @@ -9,10 +9,7 @@ use pb::{EchoRequest, EchoResponse}; use std::sync::Arc; use tokio::net::TcpListener; use tokio_rustls::{ - rustls::{ - pki_types::{CertificateDer, PrivatePkcs8KeyDer}, - ServerConfig, - }, + rustls::{pki_types::CertificateDer, ServerConfig}, TlsAcceptor, }; use tonic::{body::BoxBody, transport::Server, Request, Response, Status}; @@ -29,23 +26,15 @@ async fn main() -> Result<(), Box> { .map(|res| res.map(|cert| cert.to_owned())) .collect::, _>>()? }; - let key: PrivatePkcs8KeyDer<'static> = { + let key = { let fd = std::fs::File::open(data_dir.join("tls/server.key"))?; let mut buf = std::io::BufReader::new(&fd); - let key = rustls_pemfile::pkcs8_private_keys(&mut buf) - .next() - .unwrap()? - .clone_key(); - - key - - // let key = std::fs::read(data_dir.join("tls/server.key"))?; - // PrivateKey(key) + rustls_pemfile::private_key(&mut buf)?.unwrap() }; let mut tls = ServerConfig::builder() .with_no_client_auth() - .with_single_cert(certs, key.into())?; + .with_single_cert(certs, key)?; tls.alpn_protocols = vec![b"h2".to_vec()]; let server = EchoServer::default(); From 2d7ad07dd8fac3ba01d468410e97c58749753754 Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 20 Jun 2024 22:19:40 +0900 Subject: [PATCH 5/6] chore(examples): Remove redundant CertificateDer::to_owned call --- examples/src/tls_rustls/server.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/src/tls_rustls/server.rs b/examples/src/tls_rustls/server.rs index 65674a788..2a9885d18 100644 --- a/examples/src/tls_rustls/server.rs +++ b/examples/src/tls_rustls/server.rs @@ -19,12 +19,10 @@ use tower_http::ServiceBuilderExt; #[tokio::main] async fn main() -> Result<(), Box> { let data_dir = std::path::PathBuf::from_iter([std::env!("CARGO_MANIFEST_DIR"), "data"]); - let certs: Vec> = { + let certs = { let fd = std::fs::File::open(data_dir.join("tls/server.pem"))?; let mut buf = std::io::BufReader::new(&fd); - rustls_pemfile::certs(&mut buf) - .map(|res| res.map(|cert| cert.to_owned())) - .collect::, _>>()? + rustls_pemfile::certs(&mut buf).collect::, _>>()? }; let key = { let fd = std::fs::File::open(data_dir.join("tls/server.key"))?; From 4bad7d62eb0f15ee8c0274a4187158b5721ed569 Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 20 Jun 2024 22:33:21 +0900 Subject: [PATCH 6/6] chore(examples): Create Routes directly --- examples/src/tls_rustls/server.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/src/tls_rustls/server.rs b/examples/src/tls_rustls/server.rs index 2a9885d18..23f894966 100644 --- a/examples/src/tls_rustls/server.rs +++ b/examples/src/tls_rustls/server.rs @@ -12,7 +12,7 @@ use tokio_rustls::{ rustls::{pki_types::CertificateDer, ServerConfig}, TlsAcceptor, }; -use tonic::{body::BoxBody, transport::Server, Request, Response, Status}; +use tonic::{body::BoxBody, service::Routes, Request, Response, Status}; use tower::{BoxError, ServiceExt}; use tower_http::ServiceBuilderExt; @@ -37,9 +37,7 @@ async fn main() -> Result<(), Box> { let server = EchoServer::default(); - let svc = Server::builder() - .add_service(pb::echo_server::EchoServer::new(server)) - .into_service(); + let svc = Routes::new(pb::echo_server::EchoServer::new(server)); let http = Builder::new(TokioExecutor::new());