From 2c4c39a9be404b6185861f8dba105f772330ee66 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Wed, 17 Aug 2022 19:01:31 +0200 Subject: [PATCH 1/5] allow to set socket timeout fixes #112 --- adafruit_minimqtt/adafruit_minimqtt.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 7ec73be8..8d11f555 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -129,6 +129,7 @@ class MQTT: :param socket socket_pool: A pool of socket resources available for the given radio. :param ssl_context: SSL context for long-lived SSL connections. :param bool use_binary_mode: Messages are passed as bytearray instead of string to callbacks. + :param int socket_timeout: socket timeout, in seconds """ @@ -145,6 +146,7 @@ def __init__( socket_pool=None, ssl_context=None, use_binary_mode=False, + socket_timeout=1, ): self._socket_pool = socket_pool @@ -152,6 +154,7 @@ def __init__( self._sock = None self._backwards_compatible_sock = False self._use_binary_mode = use_binary_mode + self._socket_timeout = socket_timeout self.keep_alive = keep_alive self._user_data = None @@ -209,12 +212,12 @@ def __init__( self.on_unsubscribe = None # pylint: disable=too-many-branches - def _get_connect_socket(self, host, port, *, timeout=1): + def _get_connect_socket(self, host, port, *, timeout): """Obtains a new socket and connects to a broker. :param str host: Desired broker hostname :param int port: Desired broker port - :param int timeout: Desired socket timeout + :param int timeout: Desired socket timeout in seconds """ # For reconnections - check if we're using a socket already and close it if self._sock: @@ -444,7 +447,9 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None): self.logger.debug("Attempting to establish MQTT connection...") # Get a new socket - self._sock = self._get_connect_socket(self.broker, self.port) + self._sock = self._get_connect_socket( + self.broker, self.port, timeout=self._socket_timeout + ) # Fixed Header fixed_header = bytearray([0x10]) From 5f35f2b9c0104c0b033428333f958d8e37451a56 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Wed, 17 Aug 2022 20:04:14 +0200 Subject: [PATCH 2/5] suppress the too-many-lines pylint error the module length grew above 1k lines --- adafruit_minimqtt/adafruit_minimqtt.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 8d11f555..36672d71 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -6,6 +6,8 @@ # Modified Work Copyright (c) 2019 Bradley Beach, esp32spi_mqtt # Modified Work Copyright (c) 2012-2019 Roger Light and others, Paho MQTT Python +# pylint: disable=too-many-lines + """ `adafruit_minimqtt` ================================================================================ From aac193664bf085f478aefa66cc1c89d5277e2e89 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 18 Aug 2022 16:48:35 +0200 Subject: [PATCH 3/5] add the default value back --- adafruit_minimqtt/adafruit_minimqtt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 36672d71..4705fbc4 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -214,12 +214,12 @@ def __init__( self.on_unsubscribe = None # pylint: disable=too-many-branches - def _get_connect_socket(self, host, port, *, timeout): + def _get_connect_socket(self, host, port, *, timeout=1): """Obtains a new socket and connects to a broker. :param str host: Desired broker hostname :param int port: Desired broker port - :param int timeout: Desired socket timeout in seconds + :param int timeout: Desired socket timeout, in seconds """ # For reconnections - check if we're using a socket already and close it if self._sock: From 53708f6668ab664e0c49b53c0034b80222f5bff2 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 18 Aug 2022 16:59:28 +0200 Subject: [PATCH 4/5] try to explain what socket timeout means --- adafruit_minimqtt/adafruit_minimqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 4705fbc4..fa00f553 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -131,7 +131,7 @@ class MQTT: :param socket socket_pool: A pool of socket resources available for the given radio. :param ssl_context: SSL context for long-lived SSL connections. :param bool use_binary_mode: Messages are passed as bytearray instead of string to callbacks. - :param int socket_timeout: socket timeout, in seconds + :param int socket_timeout: How often to check socket state for read/write/connect operations, in seconds. """ From cb4b3d1b54f4f7f5d268d1e14d55775e0704d8dd Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 18 Aug 2022 17:06:45 +0200 Subject: [PATCH 5/5] wrap the line --- adafruit_minimqtt/adafruit_minimqtt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index fa00f553..56673aad 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -131,7 +131,8 @@ class MQTT: :param socket socket_pool: A pool of socket resources available for the given radio. :param ssl_context: SSL context for long-lived SSL connections. :param bool use_binary_mode: Messages are passed as bytearray instead of string to callbacks. - :param int socket_timeout: How often to check socket state for read/write/connect operations, in seconds. + :param int socket_timeout: How often to check socket state for read/write/connect operations, + in seconds. """