diff --git a/src/cmd/configfile.rs b/src/cmd/configfile.rs index d97211c..7695c98 100644 --- a/src/cmd/configfile.rs +++ b/src/cmd/configfile.rs @@ -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] diff --git a/src/config.rs b/src/config.rs index 0b6d2f9..28afc73 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 { @@ -83,6 +85,7 @@ impl Default for Mqtt { ca_cert: "".into(), tls_cert: "".into(), tls_key: "".into(), + reconnect_interval: Duration::from_secs(1), } } } diff --git a/src/mqtt.rs b/src/mqtt.rs index 2dbeb34..31f6dce 100644 --- a/src/mqtt.rs +++ b/src/mqtt.rs @@ -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; @@ -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"); @@ -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 } } _ => {} @@ -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 } } }