From bf06cac9d4dbea73f237f56750cff4526e5acdf2 Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Mon, 30 Mar 2020 12:16:51 -0700 Subject: [PATCH] Fix MQTT reconnect timeouts (#140) --- awscrt/mqtt.py | 9 +++++++-- setup.py | 2 +- source/mqtt_client_connection.c | 11 ++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/awscrt/mqtt.py b/awscrt/mqtt.py index afb9971a9..c3b76345f 100644 --- a/awscrt/mqtt.py +++ b/awscrt/mqtt.py @@ -173,6 +173,9 @@ def __init__(self, assert isinstance(websocket_proxy_options, HttpProxyOptions) or websocket_proxy_options is None assert callable(websocket_handshake_transform) or websocket_handshake_transform is None + if reconnect_min_timeout_secs > reconnect_max_timeout_secs: + raise ValueError("'reconnect_min_timeout_secs' cannot exceed 'reconnect_max_timeout_secs'") + if keep_alive_secs * 1000 <= ping_timeout_ms: raise ValueError("'keep_alive_secs' duration must be longer than 'ping_timeout_ms'") @@ -190,6 +193,8 @@ def __init__(self, self.host_name = host_name self.port = port self.clean_session = clean_session + self.reconnect_min_timeout_secs = reconnect_min_timeout_secs + self.reconnect_max_timeout_secs = reconnect_max_timeout_secs self.keep_alive_secs = keep_alive_secs self.ping_timeout_ms = ping_timeout_ms self.will = will @@ -198,8 +203,6 @@ def __init__(self, self.socket_options = socket_options if socket_options else SocketOptions() self.websocket_proxy_options = websocket_proxy_options - # TODO: reconnect_min_timeout_secs & reconnect_max_timeout_secs currently unused - self._binding = _awscrt.mqtt_client_connection_new( self, client, @@ -256,6 +259,8 @@ def on_connect(error_code, return_code, session_present): self.port, self.socket_options, self.client.tls_ctx, + self.reconnect_min_timeout_secs, + self.reconnect_max_timeout_secs, self.keep_alive_secs, self.ping_timeout_ms, self.will, diff --git a/setup.py b/setup.py index 43e92de83..48d97aaea 100644 --- a/setup.py +++ b/setup.py @@ -244,7 +244,7 @@ def awscrt_ext(): setuptools.setup( name="awscrt", - version="0.5.12", + version="0.5.13", author="Amazon Web Services, Inc", author_email="aws-sdk-common-runtime@amazon.com", description="A common runtime for AWS Python projects", diff --git a/source/mqtt_client_connection.c b/source/mqtt_client_connection.c index 7663665c9..e25aebb05 100644 --- a/source/mqtt_client_connection.c +++ b/source/mqtt_client_connection.c @@ -501,6 +501,8 @@ PyObject *aws_py_mqtt_client_connection_connect(PyObject *self, PyObject *args) uint16_t port_number; PyObject *socket_options_py; PyObject *tls_ctx_py; + uint64_t reconnect_min_timeout_secs; + uint64_t reconnect_max_timeout_secs; uint16_t keep_alive_time; uint32_t ping_timeout; PyObject *will; @@ -513,7 +515,7 @@ PyObject *aws_py_mqtt_client_connection_connect(PyObject *self, PyObject *args) PyObject *ws_proxy_options_py; if (!PyArg_ParseTuple( args, - "Os#s#HOOHIOz#z#OOO", + "Os#s#HOOKKHIOz#z#OOO", &impl_capsule, &client_id, &client_id_len, @@ -522,6 +524,8 @@ PyObject *aws_py_mqtt_client_connection_connect(PyObject *self, PyObject *args) &port_number, &socket_options_py, &tls_ctx_py, + &reconnect_min_timeout_secs, + &reconnect_max_timeout_secs, &keep_alive_time, &ping_timeout, &will, @@ -553,6 +557,11 @@ PyObject *aws_py_mqtt_client_connection_connect(PyObject *self, PyObject *args) struct aws_byte_cursor server_name_cur = aws_byte_cursor_from_array(server_name, server_name_len); + if (aws_mqtt_client_connection_set_reconnect_timeout( + py_connection->native, reconnect_min_timeout_secs, reconnect_max_timeout_secs)) { + return PyErr_AwsLastError(); + } + if (will != Py_None) { if (!s_set_will(py_connection->native, will)) { return NULL;