Skip to content

Commit 4b2df1d

Browse files
Add mqtt reconnect_interval config option. (#63)
1 parent c2a94e8 commit 4b2df1d

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/cmd/configfile.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ pub fn run(config: &Configuration) {
100100
# TLS key file (optional)
101101
tls_key="{{ mqtt.tls_key }}"
102102
103+
# Reconnect interval.
104+
#
105+
# This defines the reconnection interval to the MQTT broker in case of
106+
# network issues.
107+
reconnect_interval="{{ integration.mqtt.reconnect_interval }}"
108+
103109
104110
# Backend configuration.
105111
[backend]

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub struct Mqtt {
6666
pub ca_cert: String,
6767
pub tls_cert: String,
6868
pub tls_key: String,
69+
#[serde(with = "humantime_serde")]
70+
pub reconnect_interval: Duration,
6971
}
7072

7173
impl Default for Mqtt {
@@ -83,6 +85,7 @@ impl Default for Mqtt {
8385
ca_cert: "".into(),
8486
tls_cert: "".into(),
8587
tls_key: "".into(),
88+
reconnect_interval: Duration::from_secs(1),
8689
}
8790
}
8891
}

src/mqtt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::fs::File;
22
use std::io::{BufReader, Cursor};
33
use std::sync::Arc;
4-
use std::time::Duration;
54

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

214214
async move {
215215
info!("Starting MQTT event loop");
@@ -238,7 +238,7 @@ pub async fn setup(conf: &Configuration) -> Result<()> {
238238
}
239239
} else {
240240
error!("Connection error, code: {:?}", v.code);
241-
sleep(Duration::from_secs(1)).await
241+
sleep(reconnect_interval).await
242242
}
243243
}
244244
_ => {}
@@ -248,7 +248,7 @@ pub async fn setup(conf: &Configuration) -> Result<()> {
248248
commands::exec_callback(&on_mqtt_connection_error).await;
249249

250250
error!("MQTT error, error: {}", e);
251-
sleep(Duration::from_secs(1)).await
251+
sleep(reconnect_interval).await
252252
}
253253
}
254254
}

0 commit comments

Comments
 (0)