Skip to content

Commit

Permalink
Add mqtt reconnect_interval config option. (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-juteau authored Jan 24, 2025
1 parent c2a94e8 commit 4b2df1d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/cmd/configfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ pub fn run(config: &Configuration) {
# TLS key file (optional)
tls_key="{{ mqtt.tls_key }}"
# Reconnect interval.
#
# This defines the reconnection interval to the MQTT broker in case of
# network issues.
reconnect_interval="{{ integration.mqtt.reconnect_interval }}"
# Backend configuration.
[backend]
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub struct Mqtt {
pub ca_cert: String,
pub tls_cert: String,
pub tls_key: String,
#[serde(with = "humantime_serde")]
pub reconnect_interval: Duration,
}

impl Default for Mqtt {
Expand All @@ -83,6 +85,7 @@ impl Default for Mqtt {
ca_cert: "".into(),
tls_cert: "".into(),
tls_key: "".into(),
reconnect_interval: Duration::from_secs(1),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/mqtt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fs::File;
use std::io::{BufReader, Cursor};
use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result};
use chirpstack_api::gw;
Expand Down Expand Up @@ -210,6 +209,7 @@ pub async fn setup(conf: &Configuration) -> Result<()> {
tokio::spawn({
let on_mqtt_connected = conf.callbacks.on_mqtt_connected.clone();
let on_mqtt_connection_error = conf.callbacks.on_mqtt_connection_error.clone();
let reconnect_interval = conf.mqtt.reconnect_interval.clone();

async move {
info!("Starting MQTT event loop");
Expand Down Expand Up @@ -238,7 +238,7 @@ pub async fn setup(conf: &Configuration) -> Result<()> {
}
} else {
error!("Connection error, code: {:?}", v.code);
sleep(Duration::from_secs(1)).await
sleep(reconnect_interval).await
}
}
_ => {}
Expand All @@ -248,7 +248,7 @@ pub async fn setup(conf: &Configuration) -> Result<()> {
commands::exec_callback(&on_mqtt_connection_error).await;

error!("MQTT error, error: {}", e);
sleep(Duration::from_secs(1)).await
sleep(reconnect_interval).await
}
}
}
Expand Down

0 comments on commit 4b2df1d

Please sign in to comment.