Skip to content

Commit c5bb7c9

Browse files
committed
address comments
1 parent ec6f6cc commit c5bb7c9

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

dogstatsd-client/src/lib.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub enum DogStatsDAction<'a, T: AsRef<str>, V: IntoIterator<Item = &'a Tag>> {
7171
/// A dogstatsd-client that flushes stats to a given endpoint.
7272
#[derive(Debug, Default)]
7373
pub struct Client {
74-
client: Arc<Mutex<Option<StatsdClient>>>,
74+
client: Mutex<Arc<Option<StatsdClient>>>,
7575
endpoint: Option<Endpoint>,
7676
}
7777

@@ -90,15 +90,15 @@ impl Client {
9090
/// Send a vector of DogStatsDActionOwned, this is the same as `send` except it uses the "owned"
9191
/// version of DogStatsDAction. See the docs for DogStatsDActionOwned for details.
9292
pub fn send_owned(&self, actions: Vec<DogStatsDActionOwned>) {
93-
let client_guard = match self.get_or_init_client() {
93+
let client_opt = match self.get_or_init_client() {
9494
Ok(guard) => guard,
9595
Err(e) => {
9696
error!("Failed to get client: {}", e);
9797
return;
9898
}
9999
};
100100

101-
if let Some(client) = &*client_guard {
101+
if let Some(client) = &*client_opt {
102102
for action in actions {
103103
if let Err(err) = match action {
104104
DogStatsDActionOwned::Count(metric, value, tags) => {
@@ -129,14 +129,14 @@ impl Client {
129129
&self,
130130
actions: Vec<DogStatsDAction<'a, T, V>>,
131131
) {
132-
let client_guard = match self.get_or_init_client() {
132+
let client_opt = match self.get_or_init_client() {
133133
Ok(guard) => guard,
134134
Err(e) => {
135135
error!("Failed to get client: {}", e);
136136
return;
137137
}
138138
};
139-
if let Some(client) = &*client_guard {
139+
if let Some(client) = &*client_opt {
140140
for action in actions {
141141
if let Err(err) = match action {
142142
DogStatsDAction::Count(metric, value, tags) => {
@@ -162,19 +162,21 @@ impl Client {
162162
}
163163
}
164164

165-
fn get_or_init_client(&self) -> anyhow::Result<std::sync::MutexGuard<Option<StatsdClient>>> {
166-
let mut client_guard = self
167-
.client
168-
.lock()
169-
.map_err(|e| anyhow!("Failed to acquire dogstatsd client lock: {}", e))?;
170-
171-
if client_guard.is_none() {
172-
if let Some(endpoint) = &self.endpoint {
173-
*client_guard = Some(create_client(endpoint)?);
174-
}
165+
fn get_or_init_client(&self) -> anyhow::Result<Arc<Option<StatsdClient>>> {
166+
if let Some(endpoint) = &self.endpoint {
167+
let mut client_guard = self.client.lock().map_err(|e| {
168+
anyhow!("Failed to acquire dogstatsd client lock: {}", e.to_string())
169+
})?;
170+
return if client_guard.is_some() {
171+
Ok(client_guard.clone())
172+
} else {
173+
let client = Arc::new(Some(create_client(endpoint)?));
174+
*client_guard = client.clone();
175+
Ok(client)
176+
};
175177
}
176178

177-
Ok(client_guard)
179+
Ok(None.into())
178180
}
179181
}
180182

0 commit comments

Comments
 (0)