Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ekump committed Feb 20, 2025
1 parent ec6f6cc commit 4933211
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions dogstatsd-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub enum DogStatsDAction<'a, T: AsRef<str>, V: IntoIterator<Item = &'a Tag>> {
/// A dogstatsd-client that flushes stats to a given endpoint.
#[derive(Debug, Default)]
pub struct Client {
client: Arc<Mutex<Option<StatsdClient>>>,
client: Mutex<Arc<Option<StatsdClient>>>,
endpoint: Option<Endpoint>,
}

Expand Down Expand Up @@ -162,19 +162,21 @@ impl Client {
}
}

fn get_or_init_client(&self) -> anyhow::Result<std::sync::MutexGuard<Option<StatsdClient>>> {
let mut client_guard = self
.client
.lock()
.map_err(|e| anyhow!("Failed to acquire dogstatsd client lock: {}", e))?;

if client_guard.is_none() {
if let Some(endpoint) = &self.endpoint {
*client_guard = Some(create_client(endpoint)?);
}
fn get_or_init_client(&self) -> anyhow::Result<Arc<Option<StatsdClient>>> {
if let Some(endpoint) = &self.endpoint {
let mut client_guard = self.client.lock().map_err(|e| {
anyhow!("Failed to acquire dogstatsd client lock: {}", e.to_string())
})?;
return if client_guard.is_some() {
Ok(client_guard.clone())
} else {
let client = Arc::new(Some(create_client(endpoint)?));
*client_guard = client.clone();
Ok(client)
};
}

Ok(client_guard)
Ok(None.into())
}
}

Expand Down

0 comments on commit 4933211

Please sign in to comment.