From 2a71ee68cad0fef53fde70a8ba98d47de4c98a89 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 29 Oct 2024 23:16:54 -0400 Subject: [PATCH] Fix max udp size bug The buffer length counting was incorrect, and for large pipelines the 65k the entire packet will be dropped Fixes #30 --- src/client.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index f4a8633..a658a2c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -42,7 +42,7 @@ impl error::Error for StatsdError {} /// /// # Example /// -/// Creating a client and sending metrics is easy. +/// Creating a client and sending metrics can be done with: /// /// ```ignore /// use statsd::client::Client; @@ -415,7 +415,7 @@ impl Pipeline { _data += client.prepare(&data).as_ref(); while !self.stats.is_empty() { let stat = client.prepare(self.stats.pop_front().unwrap()); - if data.len() + stat.len() + 1 > self.max_udp_size { + if _data.len() + stat.len() + 1 > self.max_udp_size { client.send(_data.clone()); _data.clear(); _data += &stat; @@ -679,6 +679,20 @@ mod test { assert_eq!(vec!["myapp.metric:9.1|g", "myapp.metric:12.2|c"], response); } + #[test] + fn test_pipeline_set_max_udp_size_chunk() { + let server = Server::new(); + let client = Client::new(server.addr(), "myapp").unwrap(); + let response = server.run_while_receiving_all(|| { + let mut pipeline = client.pipeline(); + pipeline.set_max_udp_size(5); + pipeline.gauge("metric_gauge", 9.1); + pipeline.count("metric_letters", 12.2); + pipeline.send(&client); + }); + assert_eq!(vec!["myapp.metric_gauge:9.1|g", "myapp.metric_letters:12.2|c"], response); + } + #[test] fn test_pipeline_send_metric_after_pipeline() { let server = Server::new();