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

[Backport v2.4] Improved rumqttc event loop check. #2413

Merged
merged 3 commits into from
Apr 2, 2024
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
27 changes: 17 additions & 10 deletions crates/outbound-mqtt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod host_component;
use std::time::Duration;

use anyhow::Result;
use rumqttc::AsyncClient;
use rumqttc::{AsyncClient, Event, Incoming, Outgoing, QoS};
use spin_core::{async_trait, wasmtime::component::Resource};
use spin_world::v2::mqtt::{self as v2, Connection as MqttConnection, Error, Qos};

Expand Down Expand Up @@ -97,15 +97,22 @@ impl v2::HostConnection for OutboundMqtt {
.await
.map_err(other_error)?;

// Poll EventLoop once to send the message to MQTT broker or capture/throw error
// We may revisit this later to manage long running connections and their issues in the connection pool.
eventloop
.poll()
.await
.map_err(|err: rumqttc::ConnectionError| {
v2::Error::ConnectionFailed(err.to_string())
})?;

// Poll event loop until outgoing publish event is iterated over to send the message to MQTT broker or capture/throw error.
// We may revisit this later to manage long running connections, high throughput use cases and their issues in the connection pool.
loop {
let event = eventloop
.poll()
.await
.map_err(|err| v2::Error::ConnectionFailed(err.to_string()))?;

match (qos, event) {
(QoS::AtMostOnce, Event::Outgoing(Outgoing::Publish(_)))
| (QoS::AtLeastOnce, Event::Incoming(Incoming::PubAck(_)))
| (QoS::ExactlyOnce, Event::Outgoing(Outgoing::PubComp(_))) => break,

(_, _) => continue,
}
}
Ok(())
}
.await)
Expand Down
Loading