From abecc6cedc2605092d3bf75702d6d40daa00d160 Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Sat, 14 Jun 2025 23:57:51 -0700 Subject: [PATCH] wrap field instead of implementing clone for whole data struct --- src/config.rs | 25 +++++++++---------------- src/fetch/mod.rs | 2 +- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/config.rs b/src/config.rs index 693167d..038dc4f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -356,9 +356,10 @@ clone_trait_object!(UrlVerifier); /// prevent denial of service attacks, where an attacker triggers fetching of recursive objects. /// /// +#[derive(Clone)] pub struct Data { pub(crate) config: FederationConfig, - pub(crate) request_counter: AtomicU32, + pub(crate) request_counter: RequestCounter, } impl Data { @@ -381,7 +382,7 @@ impl Data { } /// Total number of outgoing HTTP requests made with this data. pub fn request_count(&self) -> u32 { - self.request_counter.load(Ordering::Relaxed) + self.request_counter.0.load(Ordering::Relaxed) } /// Add HTTP signature to arbitrary request @@ -412,21 +413,13 @@ impl Deref for Data { } } -impl Clone for Data { - fn clone(&self) -> Self { - Data { - config: self.config.clone(), - request_counter: self.request_counter.load(Ordering::Relaxed).into(), - } - } +/// Wrapper to implement `Clone` +#[derive(Default)] +pub(crate) struct RequestCounter(pub(crate) AtomicU32); - fn clone_from(&mut self, source: &Self) { - let Data { - config, - request_counter, - } = self; - config.clone_from(&source.config); - *request_counter.get_mut() = source.request_counter.load(Ordering::Relaxed); +impl Clone for RequestCounter { + fn clone(&self) -> Self { + RequestCounter(self.0.load(Ordering::Relaxed).into()) } } diff --git a/src/fetch/mod.rs b/src/fetch/mod.rs index 424a115..289c55d 100644 --- a/src/fetch/mod.rs +++ b/src/fetch/mod.rs @@ -106,7 +106,7 @@ async fn fetch_object_http_with_accept( config.verify_url_valid(url).await?; info!("Fetching remote object {}", url.to_string()); - let mut counter = data.request_counter.fetch_add(1, Ordering::SeqCst); + let mut counter = data.request_counter.0.fetch_add(1, Ordering::SeqCst); // fetch_add returns old value so we need to increment manually here counter += 1; if counter > config.http_fetch_limit {