From 9df483d6cdc7541032a075e3fed66f80256cac77 Mon Sep 17 00:00:00 2001 From: Santtu Lakkala Date: Wed, 9 Oct 2024 12:00:41 +0300 Subject: [PATCH] Miscellany cleanups Signed-off-by: Santtu Lakkala --- client/src/client.rs | 2 +- client/src/endpoint.rs | 29 +++++++++-------------------- src/admin/server.rs | 2 +- src/utils/naming.rs | 2 +- 4 files changed, 12 insertions(+), 23 deletions(-) diff --git a/client/src/client.rs b/client/src/client.rs index 83725ef..0f245cc 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -167,7 +167,7 @@ impl AdminClient { pub async fn watch(&self) -> anyhow::Result { use pb::admin::watch_item::Status; use pb::admin::WatchItem; - let (tx, rx) = async_channel::bounded::(10); + let (tx, rx) = async_channel::bounded(10); let (quittx, mut quitrx) = mpsc::channel(1); let mut watch = self diff --git a/client/src/endpoint.rs b/client/src/endpoint.rs index 02a5fd6..0bf3a40 100644 --- a/client/src/endpoint.rs +++ b/client/src/endpoint.rs @@ -1,4 +1,5 @@ use std::path::PathBuf; +use std::sync::Arc; use std::time::Duration; use anyhow::anyhow; @@ -40,11 +41,11 @@ impl TlsConfig { let client_identity = Identity::from_pem(client_cert, client_key); let tls_name = self .tls_name - .as_ref() + .as_deref() .ok_or_else(|| anyhow!("Missing TLS name"))?; Ok(ClientTlsConfig::new() .ca_certificate(ca) - .domain_name(tls_name.as_str()) + .domain_name(tls_name) .identity(client_identity)) } @@ -69,32 +70,20 @@ fn transport_config_to_url(ea: &EndpointAddress, with_tls: bool) -> String { } async fn connect_unix_socket(endpoint: Endpoint, path: &String) -> anyhow::Result { - let mut path = Some(path.to_owned()); + let path = Arc::new(path.to_owned()); let ch = endpoint .connect_with_connector(service_fn(move |_: Uri| { - let path = path.take(); - async move { - if let Some(path) = path { - // Connect to a Uds socket - Ok::<_, std::io::Error>(TokioIo::new(UnixStream::connect(path).await?)) - } else { - Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Path already taken", - )) - } - } + let path = path.clone(); + async move { UnixStream::connect(path.as_ref()).await.map(TokioIo::new) } })) .await?; Ok(ch) } -async fn connect_vsock_socket(endpoint: Endpoint, vs: &VsockAddr) -> anyhow::Result { - let vs = vs.to_owned(); +async fn connect_vsock_socket(endpoint: Endpoint, vs: VsockAddr) -> anyhow::Result { let ch = endpoint .connect_with_connector(service_fn(move |_: Uri| async move { - let stream = VsockStream::connect(vs).await?; - Ok::<_, std::io::Error>(TokioIo::new(stream)) + VsockStream::connect(vs).await.map(TokioIo::new) })) .await?; Ok(ch) @@ -114,7 +103,7 @@ impl EndpointConfig { EndpointAddress::Tcp { .. } => endpoint.connect().await?, EndpointAddress::Unix(unix) => connect_unix_socket(endpoint, unix).await?, EndpointAddress::Abstract(abs) => connect_unix_socket(endpoint, abs).await?, - EndpointAddress::Vsock(vs) => connect_vsock_socket(endpoint, vs).await?, + EndpointAddress::Vsock(vs) => connect_vsock_socket(endpoint, *vs).await?, }; Ok(channel) } diff --git a/src/admin/server.rs b/src/admin/server.rs index 5faae1b..5b23354 100644 --- a/src/admin/server.rs +++ b/src/admin/server.rs @@ -223,7 +223,7 @@ impl AdminServiceImpl { error!("could not get status of unit {}: {}", &entry.name, err); self.handle_error(entry) .await - .with_context(|| "during handle error")? + .context("during handle error")? } Ok(status) => { let inactive = status.active_state != "active"; diff --git a/src/utils/naming.rs b/src/utils/naming.rs index f884b6e..f50de9b 100644 --- a/src/utils/naming.rs +++ b/src/utils/naming.rs @@ -27,7 +27,7 @@ pub fn parse_application_name(name: &str) -> anyhow::Result<(&str, i32)> { if let Some(name_no_suffix) = name.strip_suffix(".service") { if let Some((left, right)) = name_no_suffix.rsplit_once('@') { let num = right - .parse::() + .parse() .with_context(|| format!("While parsing number part of {name}"))?; return Ok((left, num)); }