diff --git a/rust/src/mqtt/mqtt.rs b/rust/src/mqtt/mqtt.rs index 4e16250e8a..9517a67294 100644 --- a/rust/src/mqtt/mqtt.rs +++ b/rust/src/mqtt/mqtt.rs @@ -120,6 +120,7 @@ pub struct MQTTState { skip_request: usize, skip_response: usize, max_msg_len: usize, + tx_index_completed: usize, } impl MQTTState { @@ -132,6 +133,7 @@ impl MQTTState { skip_request: 0, skip_response: 0, max_msg_len: unsafe { MAX_MSG_LEN as usize }, + tx_index_completed: 0, } } @@ -148,6 +150,7 @@ impl MQTTState { } } if found { + self.tx_index_completed = 0; self.transactions.remove(index); } } @@ -184,13 +187,16 @@ impl MQTTState { tx.toserver = true; } if self.transactions.len() > unsafe { MQTT_MAX_TX } { - for tx_old in &mut self.transactions { + let mut index = self.tx_index_completed; + for tx_old in self.transactions.iter_mut().skip(self.tx_index_completed) { + index = index + 1; if !tx_old.complete { tx_old.complete = true; MQTTState::set_event(tx_old, MQTTEvent::TooManyTransactions); break; } } + self.tx_index_completed = index; } return tx; }