Skip to content

Commit

Permalink
Throttle error log messages from MQTT client
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed Dec 16, 2023
1 parent 0f616f6 commit 962e0ff
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions common/src/mqtt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::ThrottledErrorLogger;
use rumqttc::{AsyncClient, Event, EventLoop, Incoming, MqttOptions, Outgoing, Publish, QoS};
use serde::Deserialize;
use std::time::Duration;
Expand All @@ -19,6 +20,7 @@ pub struct MqttConfig {
pub struct MqttClient {
client: AsyncClient,
event_loop: EventLoop,
poll_error_logger: ThrottledErrorLogger<String>,

topic: String,
}
Expand All @@ -34,6 +36,7 @@ impl From<MqttConfig> for MqttClient {
Self {
client,
event_loop,
poll_error_logger: ThrottledErrorLogger::new(Duration::from_secs(5)),
topic: config.topic,
}
}
Expand Down Expand Up @@ -64,7 +67,9 @@ impl MqttClient {
}
Ok(_) => None,
Err(e) => {
warn!("rumqttc error: {:?}", e);
if let Some(e) = self.poll_error_logger.log(format!("{:?}", e)) {
warn!("rumqttc error: {}", e);
}
None
}
}
Expand All @@ -81,7 +86,9 @@ impl MqttClient {
}
Ok(_) => {}
Err(e) => {
warn!("rumqttc error: {:?}", e);
if let Some(e) = self.poll_error_logger.log(format!("{:?}", e)) {
warn!("rumqttc error: {}", e);
}
}
}
}
Expand Down Expand Up @@ -114,7 +121,9 @@ impl MqttClient {
}
Ok(_) => {}
Err(e) => {
warn!("rumqttc error: {:?}", e);
if let Some(e) = self.poll_error_logger.log(format!("{:?}", e)) {
warn!("rumqttc error: {}", e);
}
}
}
}
Expand Down

0 comments on commit 962e0ff

Please sign in to comment.