Don't include Send/Sync in trait requirements

This commit is contained in:
Brian Picciano 2023-07-03 13:39:44 +02:00
parent fa85fe7fd8
commit 7a35befffe
6 changed files with 58 additions and 55 deletions

View File

@ -10,7 +10,7 @@ pub type GetHttp01ChallengeKeyError = acme::store::GetHttp01ChallengeKeyError;
pub type GetCertificateError = acme::store::GetCertificateError; pub type GetCertificateError = acme::store::GetCertificateError;
#[mockall::automock] #[mockall::automock]
pub trait Manager: Sync + Send { pub trait Manager {
fn sync_domain<'mgr>( fn sync_domain<'mgr>(
&'mgr self, &'mgr self,
domain: domain::Name, domain: domain::Name,
@ -25,12 +25,12 @@ pub trait Manager: Sync + Send {
} }
pub struct ManagerImpl { pub struct ManagerImpl {
store: Box<dyn acme::store::Store>, store: Box<dyn acme::store::Store + Send + Sync>,
account: sync::Arc<acme2::Account>, account: sync::Arc<acme2::Account>,
} }
impl ManagerImpl { impl ManagerImpl {
pub async fn new<Store: acme::store::Store + 'static>( pub async fn new<Store: acme::store::Store + Send + Sync + 'static>(
store: Store, store: Store,
contact_email: &str, contact_email: &str,
) -> Result<Self, unexpected::Error> { ) -> Result<Self, unexpected::Error> {

View File

@ -38,7 +38,7 @@ pub enum GetCertificateError {
} }
#[mockall::automock] #[mockall::automock]
pub trait Store: Sync + Send { pub trait Store {
fn set_account_key(&self, k: &PrivateKey) -> Result<(), unexpected::Error>; fn set_account_key(&self, k: &PrivateKey) -> Result<(), unexpected::Error>;
fn get_account_key(&self) -> Result<PrivateKey, GetAccountKeyError>; fn get_account_key(&self) -> Result<PrivateKey, GetAccountKeyError>;

View File

@ -39,7 +39,7 @@ pub enum SetError {
} }
#[mockall::automock] #[mockall::automock]
pub trait Store: Sync + Send { pub trait Store {
fn get(&self, domain: &domain::Name) -> Result<Config, GetError>; fn get(&self, domain: &domain::Name) -> Result<Config, GetError>;
fn set(&self, domain: &domain::Name, config: &Config) -> Result<(), SetError>; fn set(&self, domain: &domain::Name, config: &Config) -> Result<(), SetError>;
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>; fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>;

View File

@ -145,19 +145,47 @@ pub trait Manager: Sync + Send + rustls::server::ResolvesServerCert {
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>; fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>;
} }
struct ManagerImpl { pub struct ManagerImpl {
origin_store: Box<dyn origin::store::Store>, origin_store: Box<dyn origin::store::Store + Send + Sync>,
domain_config_store: Box<dyn config::Store>, domain_config_store: Box<dyn config::Store + Send + Sync>,
domain_checker: checker::DNSChecker, domain_checker: checker::DNSChecker,
acme_manager: Option<Box<dyn acme::manager::Manager>>, acme_manager: Option<Box<dyn acme::manager::Manager + Send + Sync>>,
} }
async fn sync_origins(origin_store: &dyn origin::store::Store, canceller: CancellationToken) { impl ManagerImpl {
pub fn new<
OriginStore: origin::store::Store + Send + Sync + 'static,
DomainConfigStore: config::Store + Send + Sync + 'static,
AcmeManager: acme::manager::Manager + Send + Sync + 'static,
>(
task_stack: &mut util::TaskStack<unexpected::Error>,
origin_store: OriginStore,
domain_config_store: DomainConfigStore,
domain_checker: checker::DNSChecker,
acme_manager: Option<AcmeManager>,
) -> sync::Arc<dyn Manager> {
let manager = sync::Arc::new(ManagerImpl {
origin_store: Box::from(origin_store),
domain_config_store: Box::from(domain_config_store),
domain_checker: domain_checker,
acme_manager: acme_manager
.map(|m| Box::new(m) as Box<dyn acme::manager::Manager + Send + Sync>),
});
task_stack.push_spawn(|canceller| {
let manager = manager.clone();
async move { Ok(manager.sync_origins(canceller).await) }
});
manager
}
async fn sync_origins(&self, canceller: CancellationToken) {
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(20 * 60)); let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(20 * 60));
loop { loop {
tokio::select! { tokio::select! {
_ = interval.tick() => { _ = interval.tick() => {
match origin_store.all_descrs() { match self.origin_store.all_descrs() {
Ok(iter) => iter.into_iter(), Ok(iter) => iter.into_iter(),
Err(err) => { Err(err) => {
log::error!("Error fetching origin descriptors: {err}"); log::error!("Error fetching origin descriptors: {err}");
@ -165,7 +193,7 @@ async fn sync_origins(origin_store: &dyn origin::store::Store, canceller: Cancel
} }
} }
.for_each(|descr| { .for_each(|descr| {
if let Err(err) = origin_store.sync(descr.clone(), origin::store::Limits {}) { if let Err(err) = self.origin_store.sync(descr.clone(), origin::store::Limits {}) {
log::error!("Failed to sync store for {:?}: {err}", descr); log::error!("Failed to sync store for {:?}: {err}", descr);
return; return;
} }
@ -174,32 +202,7 @@ async fn sync_origins(origin_store: &dyn origin::store::Store, canceller: Cancel
_ = canceller.cancelled() => return, _ = canceller.cancelled() => return,
} }
} }
} }
pub fn new<
OriginStore: origin::store::Store + 'static,
DomainConfigStore: config::Store + 'static,
AcmeManager: acme::manager::Manager + 'static,
>(
task_stack: &mut util::TaskStack<unexpected::Error>,
origin_store: OriginStore,
domain_config_store: DomainConfigStore,
domain_checker: checker::DNSChecker,
acme_manager: Option<AcmeManager>,
) -> sync::Arc<dyn Manager> {
let manager = sync::Arc::new(ManagerImpl {
origin_store: Box::from(origin_store),
domain_config_store: Box::from(domain_config_store),
domain_checker: domain_checker,
acme_manager: acme_manager.map(|m| Box::new(m) as Box<dyn acme::manager::Manager>),
});
task_stack.push_spawn(|canceller| {
let manager = manager.clone();
async move { Ok(sync_origins(manager.origin_store.as_ref(), canceller).await) }
});
manager
} }
impl Manager for ManagerImpl { impl Manager for ManagerImpl {

View File

@ -112,7 +112,7 @@ async fn main() {
let mut task_stack = domani::util::TaskStack::new(); let mut task_stack = domani::util::TaskStack::new();
let domain_manager = domani::domain::manager::new( let domain_manager = domani::domain::manager::ManagerImpl::new(
&mut task_stack, &mut task_stack,
origin_store, origin_store,
domain_config_store, domain_config_store,

View File

@ -42,7 +42,7 @@ pub enum AllDescrsError {
#[mockall::automock] #[mockall::automock]
/// Describes a storage mechanism for Origins. Each Origin is uniquely identified by its Descr. /// Describes a storage mechanism for Origins. Each Origin is uniquely identified by its Descr.
pub trait Store: Sync + Send { pub trait Store {
/// If the origin is of a kind which can be updated, sync will pull down the latest version of /// If the origin is of a kind which can be updated, sync will pull down the latest version of
/// the origin into the storage. /// the origin into the storage.
fn sync(&self, descr: origin::Descr, limits: Limits) -> Result<(), SyncError>; fn sync(&self, descr: origin::Descr, limits: Limits) -> Result<(), SyncError>;