From 92ec5023eb50a3ae83b8b2046d52962806454f67 Mon Sep 17 00:00:00 2001 From: Orne Brocaar Date: Wed, 10 Jul 2024 15:44:01 +0100 Subject: [PATCH] Spawn message_callback to not block eventloop. In case the message_callback takes a long time to return, this would affect the MQTT loop (e.g. keep-alive messages are not sent / responded in time). Closes #57. --- src/mqtt.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/mqtt.rs b/src/mqtt.rs index 319e6b8..e12159d 100644 --- a/src/mqtt.rs +++ b/src/mqtt.rs @@ -218,9 +218,13 @@ pub async fn setup(conf: &Configuration) -> Result<()> { match v { Event::Incoming(Incoming::Publish(p)) => { - if let Err(e) = message_callback(p).await { - error!("Handling message error, error: {}", e); - } + tokio::spawn({ + async move { + if let Err(e) = message_callback(p).await { + error!("Handling message error, error: {}", e); + } + } + }); } Event::Incoming(Incoming::ConnAck(v)) => { if v.code == ConnectReturnCode::Success {