Skip to content

Commit ec8b60b

Browse files
authored
Merge pull request adafruit#116 from vladak/socket_timeout_tunable
allow to set socket timeout
2 parents a698c04 + 83c4a32 commit ec8b60b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# Modified Work Copyright (c) 2019 Bradley Beach, esp32spi_mqtt
77
# Modified Work Copyright (c) 2012-2019 Roger Light and others, Paho MQTT Python
88

9+
# pylint: disable=too-many-lines
10+
911
"""
1012
`adafruit_minimqtt`
1113
================================================================================
@@ -129,6 +131,8 @@ class MQTT:
129131
:param socket socket_pool: A pool of socket resources available for the given radio.
130132
:param ssl_context: SSL context for long-lived SSL connections.
131133
:param bool use_binary_mode: Messages are passed as bytearray instead of string to callbacks.
134+
:param int socket_timeout: How often to check socket state for read/write/connect operations,
135+
in seconds.
132136
133137
"""
134138

@@ -145,13 +149,15 @@ def __init__(
145149
socket_pool=None,
146150
ssl_context=None,
147151
use_binary_mode=False,
152+
socket_timeout=1,
148153
):
149154

150155
self._socket_pool = socket_pool
151156
self._ssl_context = ssl_context
152157
self._sock = None
153158
self._backwards_compatible_sock = False
154159
self._use_binary_mode = use_binary_mode
160+
self._socket_timeout = socket_timeout
155161

156162
self.keep_alive = keep_alive
157163
self._user_data = None
@@ -214,7 +220,7 @@ def _get_connect_socket(self, host, port, *, timeout=1):
214220
215221
:param str host: Desired broker hostname
216222
:param int port: Desired broker port
217-
:param int timeout: Desired socket timeout
223+
:param int timeout: Desired socket timeout, in seconds
218224
"""
219225
# For reconnections - check if we're using a socket already and close it
220226
if self._sock:
@@ -444,7 +450,9 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
444450
self.logger.debug("Attempting to establish MQTT connection...")
445451

446452
# Get a new socket
447-
self._sock = self._get_connect_socket(self.broker, self.port)
453+
self._sock = self._get_connect_socket(
454+
self.broker, self.port, timeout=self._socket_timeout
455+
)
448456

449457
# Fixed Header
450458
fixed_header = bytearray([0x10])

0 commit comments

Comments
 (0)