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 c5bb7c9
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 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 All @@ -90,15 +90,15 @@ impl Client {
/// Send a vector of DogStatsDActionOwned, this is the same as `send` except it uses the "owned"
/// version of DogStatsDAction. See the docs for DogStatsDActionOwned for details.
pub fn send_owned(&self, actions: Vec<DogStatsDActionOwned>) {
let client_guard = match self.get_or_init_client() {
let client_opt = match self.get_or_init_client() {
Ok(guard) => guard,
Err(e) => {
error!("Failed to get client: {}", e);
return;
}
};

if let Some(client) = &*client_guard {
if let Some(client) = &*client_opt {
for action in actions {
if let Err(err) = match action {
DogStatsDActionOwned::Count(metric, value, tags) => {
Expand Down Expand Up @@ -129,14 +129,14 @@ impl Client {
&self,
actions: Vec<DogStatsDAction<'a, T, V>>,
) {
let client_guard = match self.get_or_init_client() {
let client_opt = match self.get_or_init_client() {
Ok(guard) => guard,
Err(e) => {
error!("Failed to get client: {}", e);
return;
}
};
if let Some(client) = &*client_guard {
if let Some(client) = &*client_opt {
for action in actions {
if let Err(err) = match action {
DogStatsDAction::Count(metric, value, tags) => {
Expand All @@ -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 c5bb7c9

Please sign in to comment.