eclipse / paho.mqtt.rust

paho.mqtt.rust
Other
511 stars 102 forks source link

long latency to set up connections when creating multiple connected clients #217

Open zhen9910 opened 7 months ago

zhen9910 commented 7 months ago

When creating multiple mqtt connections using paho-mqtt, version "0.12", some of connections takes more than 1s to be established.

This is a similar issue reported in https://github.com/eclipse/paho.mqtt.rust/issues/133 and https://github.com/eclipse/paho.mqtt.cpp/issues/365, but it looks appear again.

Could you take a look? It can be reproduced using the same test code in https://github.com/eclipse/paho.mqtt.rust/issues/133.

fn create_connected_client() -> paho_mqtt::AsyncClient {
    const MQTT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1);
    let start_of_create = std::time::Instant::now();
    let create_opts = paho_mqtt::CreateOptionsBuilder::new()
        .server_uri("tcp://localhost:1883")
        .finalize();
    let mqtt_client = paho_mqtt::AsyncClient::new(create_opts).unwrap();
    let conn_opts = paho_mqtt::ConnectOptionsBuilder::new().finalize();
    let _ = mqtt_client.connect(conn_opts).wait_for(MQTT_TIMEOUT);
    println!(
        "It took {:?} until client was connected",
        start_of_create.elapsed()
    );
    mqtt_client
}

fn main() {
    let cli_1 = create_connected_client();
    let cli_2 = create_connected_client();
    let cli_3 = create_connected_client();
    let cli_5 = create_connected_client();
    let cli_6 = create_connected_client();
}

It took 116.445807ms until client was connected It took 1.000388665s until client was connected It took 1.000957073s until client was connected It took 102.734703ms until client was connected It took 1.000762798s until client was connected