Skip to content

Commit

Permalink
Fix MQTT reconnect timeouts (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
graebm authored Mar 30, 2020
1 parent e3cdbaf commit bf06cac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
9 changes: 7 additions & 2 deletions awscrt/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'")

Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]",
description="A common runtime for AWS Python projects",
Expand Down
11 changes: 10 additions & 1 deletion source/mqtt_client_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bf06cac

Please sign in to comment.