eclipse / paho.mqtt.java

Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
2.12k stars 883 forks source link

Connection takes too long because of Thread.sleep(100) in some threads of client #945

Open Lighthousexx opened 2 years ago

Lighthousexx commented 2 years ago

We found a MqttAsyncClient needs almost 350ms to complete its connection, intuitively, a connection itself should not take so much time...

Then we noticed this may be caused by the following codes in CommsSender, CommsReceiver, CommsCallback

eg. CommsSender

public void start(String threadName, ExecutorService executorService) {
    this.threadName = threadName;
    synchronized (lifecycle) {
        if (current_state == State.STOPPED && target_state == State.STOPPED) {
            target_state = State.RUNNING;
            if (executorService == null) {
                new Thread(this).start();
            } else {
                senderFuture = executorService.submit(this);
            }
        }
    }
    while (!isRunning()) {
        try { Thread.sleep(100); } catch (Exception e) { }
    }
}

So do they really need to sleep 100ms before running? isRunning() would return true and end the while loop in 5ms around in our tests. Thread.sleep(100) seems to slow down the connection and affect the performance of paho client.

Anyway, this procedure only needs 50ms actually but now it takes 350ms... What do the developers think about this problem? Are there any further considerations about this?