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

Reduce the delay of ASYNC connection #1458

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions src/MQTTAsyncUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,10 @@ static int MQTTAsync_processCommand(void)
make sure we check for writeability as well as readability, otherwise we wait around longer than we need to
in Socket_getReadySocket() */
if (rc == EINPROGRESS)
{
Socket_addPendingWrite(command->client->c->net.socket);
rc = MQTTAsync_connecting(command->client);
}
}
}
else if (command->command.type == SUBSCRIBE)
Expand Down Expand Up @@ -2031,7 +2034,7 @@ thread_return_type WINAPI MQTTAsync_receiveThread(void* n)

if (sock == 0)
continue;
timeout = 1000L;
timeout = 10L;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this modification is correct.
The first time the function is entered, the call to MQTTAsync_cycle(&sock, timeout, &rc); uses timeout = 10.
If this loop has to be executed the second time around, then timeout is increased to 1000.
The question really is why was 1000 picked?

Let's consider the following:

  1. What is really this timeout value?
    Feel free to look at static MQTTPacket* MQTTAsync_cycle(SOCKET* sock, unsigned long timeout, int* rc).
    It's used in the Socket_getReadySocket(0, (int)timeout, socket_mutex, &rc1); call.
  2. In the body of SOCKET Socket_getReadySocket(int more_work, int timeout, mutex_type mutex, int* rc), timeout is used to set a local variable timeout_ms, which as a good programming practice is initialized at the top of the function (happens to be 1000).
  3. Now, there are two versions of the SOCKET Socket_getReadySocket(int more_work, int timeout, mutex_type mutex, int* rc) function. Although they are different, they seem to imply that a slowdown in operation might be desired if getting a socket did not succeed the first time around.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this change is not the best practice. This can be considered to be a temporary fix. The root cause of connection delay is that, not all socket operations are done in Socket_getReadySocket(). TCP connection is established in send thread. Even when it is completed, receive thread will continue wait until timeout. The best solution to avoid unexpected timeout for TCP connection is to move that logic (TCP connection) to receive Thread.


/* find client corresponding to socket */
if (ListFindItem(MQTTAsync_handles, &sock, clientSockCompare) == NULL)
Expand Down Expand Up @@ -3017,8 +3020,6 @@ static MQTTPacket* MQTTAsync_cycle(SOCKET* sock, unsigned long timeout, int* rc)
MQTTAsync_lock_mutex(mqttasync_mutex);
should_stop = MQTTAsync_tostop;
MQTTAsync_unlock_mutex(mqttasync_mutex);
if (!should_stop && *sock == 0 && (timeout > 0L))
MQTTAsync_sleep(100L);
#if defined(OPENSSL)
}
#endif
Expand All @@ -3032,7 +3033,7 @@ static MQTTPacket* MQTTAsync_cycle(SOCKET* sock, unsigned long timeout, int* rc)
{
Log(TRACE_MINIMUM, -1, "m->c->connect_state = %d", m->c->connect_state);
if (m->c->connect_state == TCP_IN_PROGRESS || m->c->connect_state == SSL_IN_PROGRESS || m->c->connect_state == WEBSOCKET_IN_PROGRESS)
*rc = MQTTAsync_connecting(m);
;
else
pack = MQTTPacket_Factory(m->c->MQTTVersion, &m->c->net, rc);
if (m->c->connect_state == WAIT_FOR_CONNACK && *rc == SOCKET_ERROR)
Expand Down