Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply delay to mqtt connect errors by default #2832

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions crates/common/mqtt_channel/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ use futures::StreamExt;
use log::error;
use log::info;
use rumqttc::AsyncClient;
use rumqttc::ConnectionError;
use rumqttc::Event;
use rumqttc::EventLoop;
use rumqttc::Incoming;
use rumqttc::Outgoing;
use rumqttc::Packet;
use rumqttc::StateError;
use std::time::Duration;
use tokio::time::sleep;

Expand Down Expand Up @@ -178,14 +176,11 @@ impl Connection {
host = config.broker.host,
port = config.broker.port
);
let should_delay = Connection::pause_on_error(&err);

// Errors on send are ignored: it just means the client has closed the receiving channel.
let _ = error_sender.send(err.into()).await;

if should_delay {
Connection::do_pause().await;
}
Connection::do_pause().await;
}
_ => (),
}
Expand Down Expand Up @@ -249,14 +244,11 @@ impl Connection {

Err(err) => {
error!("MQTT connection error: {err}");
let delay = Connection::pause_on_error(&err);

// Errors on send are ignored: it just means the client has closed the receiving channel.
let _ = error_sender.send(err.into()).await;

if delay {
Connection::do_pause().await;
}
Connection::do_pause().await;
}
_ => (),
}
Expand Down Expand Up @@ -305,15 +297,6 @@ impl Connection {
let _ = done.send(());
}

pub(crate) fn pause_on_error(err: &ConnectionError) -> bool {
matches!(
err,
rumqttc::ConnectionError::Io(_)
| rumqttc::ConnectionError::MqttState(StateError::Io(_))
| rumqttc::ConnectionError::MqttState(_)
)
}

pub(crate) async fn do_pause() {
sleep(Duration::from_secs(1)).await;
}
Expand Down