@@ -71,7 +71,7 @@ pub enum DogStatsDAction<'a, T: AsRef<str>, V: IntoIterator<Item = &'a Tag>> {
71
71
/// A dogstatsd-client that flushes stats to a given endpoint.
72
72
#[ derive( Debug , Default ) ]
73
73
pub struct Client {
74
- client : Arc < Mutex < Option < StatsdClient > > > ,
74
+ client : Mutex < Arc < Option < StatsdClient > > > ,
75
75
endpoint : Option < Endpoint > ,
76
76
}
77
77
@@ -90,15 +90,15 @@ impl Client {
90
90
/// Send a vector of DogStatsDActionOwned, this is the same as `send` except it uses the "owned"
91
91
/// version of DogStatsDAction. See the docs for DogStatsDActionOwned for details.
92
92
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 ( ) {
94
94
Ok ( guard) => guard,
95
95
Err ( e) => {
96
96
error ! ( "Failed to get client: {}" , e) ;
97
97
return ;
98
98
}
99
99
} ;
100
100
101
- if let Some ( client) = & * client_guard {
101
+ if let Some ( client) = & * client_opt {
102
102
for action in actions {
103
103
if let Err ( err) = match action {
104
104
DogStatsDActionOwned :: Count ( metric, value, tags) => {
@@ -129,14 +129,14 @@ impl Client {
129
129
& self ,
130
130
actions : Vec < DogStatsDAction < ' a , T , V > > ,
131
131
) {
132
- let client_guard = match self . get_or_init_client ( ) {
132
+ let client_opt = match self . get_or_init_client ( ) {
133
133
Ok ( guard) => guard,
134
134
Err ( e) => {
135
135
error ! ( "Failed to get client: {}" , e) ;
136
136
return ;
137
137
}
138
138
} ;
139
- if let Some ( client) = & * client_guard {
139
+ if let Some ( client) = & * client_opt {
140
140
for action in actions {
141
141
if let Err ( err) = match action {
142
142
DogStatsDAction :: Count ( metric, value, tags) => {
@@ -162,19 +162,21 @@ impl Client {
162
162
}
163
163
}
164
164
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
+ } ;
175
177
}
176
178
177
- Ok ( client_guard )
179
+ Ok ( None . into ( ) )
178
180
}
179
181
}
180
182
0 commit comments