From 505015a4bd9362ddca1270ed664822e4874ffbd3 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Wed, 21 Jun 2023 14:02:42 +0200 Subject: [PATCH] Removed usage of sync::Arc where possible --- TODO | 1 - src/domain/acme/manager.rs | 9 +++++---- src/domain/acme/store.rs | 6 +++--- src/domain/config.rs | 6 +++--- src/domain/manager.rs | 8 ++++---- src/main.rs | 32 ++++++++++++-------------------- 6 files changed, 27 insertions(+), 35 deletions(-) diff --git a/TODO b/TODO index 64ea93b..e69de29 100644 --- a/TODO +++ b/TODO @@ -1 +0,0 @@ -- Try to switch from Arc to Box where possible diff --git a/src/domain/acme/manager.rs b/src/domain/acme/manager.rs index 2eb4b2c..3d57797 100644 --- a/src/domain/acme/manager.rs +++ b/src/domain/acme/manager.rs @@ -25,14 +25,14 @@ pub trait Manager: Sync + Send { } struct ManagerImpl { - store: sync::Arc, + store: Box, account: sync::Arc, } pub async fn new( - store: sync::Arc, + store: Box, contact_email: &str, -) -> Result, unexpected::Error> { +) -> Result, unexpected::Error> { let dir = acme2::DirectoryBuilder::new(LETS_ENCRYPT_URL.to_string()) .build() .await @@ -61,6 +61,7 @@ pub async fn new( .build() .await .or_unexpected_while("building account")?; + let account_key: acme::PrivateKey = account .private_key() .as_ref() @@ -71,7 +72,7 @@ pub async fn new( .set_account_key(&account_key) .or_unexpected_while("storing account key")?; - Ok(sync::Arc::new(ManagerImpl { store, account })) + Ok(Box::new(ManagerImpl { store, account })) } impl Manager for ManagerImpl { diff --git a/src/domain/acme/store.rs b/src/domain/acme/store.rs index 075327e..2e41217 100644 --- a/src/domain/acme/store.rs +++ b/src/domain/acme/store.rs @@ -1,6 +1,6 @@ use std::io::{Read, Write}; use std::str::FromStr; -use std::{fs, path, sync}; +use std::{fs, path}; use crate::domain::acme::{Certificate, PrivateKey}; use crate::error::unexpected::{self, Mappable}; @@ -70,7 +70,7 @@ struct FSStore { dir_path: path::PathBuf, } -pub fn new(dir_path: &path::Path) -> Result, unexpected::Error> { +pub fn new(dir_path: &path::Path) -> Result, unexpected::Error> { vec![ dir_path, dir_path.join("http01_challenge_keys").as_ref(), @@ -82,7 +82,7 @@ pub fn new(dir_path: &path::Path) -> Result, unexpected::Er }) .try_collect()?; - Ok(sync::Arc::new(FSStore { + Ok(Box::new(FSStore { dir_path: dir_path.into(), })) } diff --git a/src/domain/config.rs b/src/domain/config.rs index a24efdf..4315a7d 100644 --- a/src/domain/config.rs +++ b/src/domain/config.rs @@ -1,6 +1,6 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; -use std::{fs, io, sync}; +use std::{fs, io}; use crate::error::unexpected::{self, Intoable, Mappable}; use crate::{domain, origin}; @@ -49,9 +49,9 @@ struct FSStore { dir_path: PathBuf, } -pub fn new(dir_path: &Path) -> io::Result> { +pub fn new(dir_path: &Path) -> io::Result> { fs::create_dir_all(dir_path)?; - Ok(sync::Arc::new(FSStore { + Ok(Box::new(FSStore { dir_path: dir_path.into(), })) } diff --git a/src/domain/manager.rs b/src/domain/manager.rs index 44cc9c8..f18066a 100644 --- a/src/domain/manager.rs +++ b/src/domain/manager.rs @@ -147,9 +147,9 @@ pub trait Manager: Sync + Send + rustls::server::ResolvesServerCert { struct ManagerImpl { origin_store: Box, - domain_config_store: sync::Arc, + domain_config_store: Box, domain_checker: checker::DNSChecker, - acme_manager: Option>, + acme_manager: Option>, } async fn sync_origins(origin_store: &dyn origin::store::Store, canceller: CancellationToken) { @@ -180,9 +180,9 @@ async fn sync_origins(origin_store: &dyn origin::store::Store, canceller: Cancel pub fn new( task_stack: &mut util::TaskStack, origin_store: Box, - domain_config_store: sync::Arc, + domain_config_store: Box, domain_checker: checker::DNSChecker, - acme_manager: Option>, + acme_manager: Option>, ) -> sync::Arc { let manager = sync::Arc::new(ManagerImpl { origin_store, diff --git a/src/main.rs b/src/main.rs index 658d5f5..24a7c05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,8 @@ use futures::stream::StreamExt; use signal_hook_tokio::Signals; use std::net::SocketAddr; +use std::path; use std::str::FromStr; -use std::{path, sync}; #[derive(Parser, Debug)] #[command(version)] @@ -60,12 +60,6 @@ struct Cli { domain_acme_contact_email: Option, } -#[derive(Clone)] -struct HTTPSParams { - https_listen_addr: SocketAddr, - domain_acme_manager: sync::Arc, -} - #[tokio::main] async fn main() { let config = Cli::parse(); @@ -92,7 +86,7 @@ async fn main() { let domain_config_store = domiply::domain::config::new(&config.domain_config_store_dir_path) .expect("domain config store initialization failed"); - let https_params = if let Some(https_listen_addr) = config.https_listen_addr { + let domain_acme_manager = if config.https_listen_addr.is_some() { let domain_acme_store_dir_path = config.domain_acme_store_dir_path.unwrap(); let domain_acme_store = domiply::domain::acme::store::new(&domain_acme_store_dir_path) @@ -102,15 +96,11 @@ async fn main() { // settings. let domain_acme_contact_email = config.domain_acme_contact_email.unwrap(); - let domain_acme_manager = + Some( domiply::domain::acme::manager::new(domain_acme_store, &domain_acme_contact_email) .await - .expect("domain acme manager initialization failed"); - - Some(HTTPSParams { - https_listen_addr, - domain_acme_manager, - }) + .expect("domain acme manager initialization failed"), + ) } else { None }; @@ -122,7 +112,7 @@ async fn main() { origin_store, domain_config_store, domain_checker, - https_params.as_ref().map(|p| p.domain_acme_manager.clone()), + domain_acme_manager, ); let _ = domiply::service::http::new( @@ -132,10 +122,12 @@ async fn main() { config.passphrase, config.http_listen_addr.clone(), config.http_domain.clone(), - https_params.map(|p| domiply::service::http::HTTPSParams { - listen_addr: p.https_listen_addr, - cert_resolver: domain_manager.clone(), - }), + config + .https_listen_addr + .map(|listen_addr| domiply::service::http::HTTPSParams { + listen_addr, + cert_resolver: domain_manager.clone(), + }), ); let mut signals =