Skip to content

Commit

Permalink
Reduce reconnection message spam
Browse files Browse the repository at this point in the history
When reading a DLT from a device that is offline for a while
(for example during flashing), DLT would spam 3 messages
every 5 seconds.

Reduce this to 1 message per minute and add time information
into the messages to increase their usefulness
  • Loading branch information
Aigars Mahinovs authored and Aigars Mahinovs committed Sep 25, 2023
1 parent 3c3a8da commit e481d16
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
16 changes: 10 additions & 6 deletions dlt/dlt.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,21 +915,23 @@ def __init__(self, **kwords):
# it ourselves elsewhere
self.port = kwords.get("port", DLT_DAEMON_TCP_PORT)

def connect(self, timeout=None):
def connect(self, timeout=None, silent=False):
"""Connect to the server
If timeout is provided, block on connect until timeout occurs. If
timeout is not provided or is None, try to connect and return
immediately
:param int|None timeout: Seconds to wait for connection
:param bool|False silent: Do not log debug info
:returns: True if connected successfully, False otherwise
:rtype: bool
"""
connected = None
error_count = 0
if not self.is_udp_multicast:
logger.info("Connecting DLTClient using TCP Connection")
if not silent:
logger.info("Connecting DLTClient using TCP Connection")
if timeout:
end_time = time.time() + timeout
while time.time() < end_time:
Expand All @@ -939,7 +941,7 @@ def connect(self, timeout=None):
(ctypes.string_at(self.servIP), self.port), timeout=timeout_remaining
)
except IOError as exc:
if error_count < MAX_LOG_IN_ROW:
if error_count < MAX_LOG_IN_ROW and not silent:
logger.debug(
"DLT client connect failed to connect to %s:%s : %s", self.servIP, self.port, exc
)
Expand All @@ -966,14 +968,16 @@ def connect(self, timeout=None):
# connection loss in the main_loop below as described at
# http://stefan.buettcher.org/cs/conn_closed.html
self._connected_socket = socket.fromfd(self.sock, socket.AF_INET6, socket.SOCK_STREAM)
if error_count > MAX_LOG_IN_ROW:
if error_count > MAX_LOG_IN_ROW and not silent:
logger.debug("Surpressed %d messages for failed connection attempts", error_count - MAX_LOG_IN_ROW)

else:
logger.info("Connecting DLTClient using UDP Connection")
if not silent:
logger.info("Connecting DLTClient using UDP Connection")
connected = dltlib.dlt_client_connect(ctypes.byref(self), self.verbose)

logger.info("DLT Connection return: %s", connected)
if not silent:
logger.info("DLT Connection return: %s", connected)
return connected == DLT_RETURN_OK

def disconnect(self):
Expand Down
30 changes: 22 additions & 8 deletions dlt/dlt_broker_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ def __init__(
self.timeout = client_cfg.get("timeout", DLT_CLIENT_TIMEOUT)
self._client = None
self.tracefile = None
self.last_connected = time.time()
self.last_message = time.time() - 120.0

def is_valid_message(self, message):
return message and (message.apid != "" or message.ctid != "")
Expand All @@ -411,14 +413,15 @@ def _client_connect(self):
:returns: True if connected, False otherwise
:rtype: bool
"""
logger.debug(
"Creating DLTClient (ip_address='%s', Port='%s', logfile='%s')",
self._ip_address,
self._port,
self._filename,
)
if self.verbose:
logger.debug(
"Creating DLTClient (ip_address='%s', Port='%s', logfile='%s')",
self._ip_address,
self._port,
self._filename,
)
self._client = DLTClient(servIP=self._ip_address, port=self._port, verbose=self.verbose)
connected = self._client.connect(self.timeout)
connected = self._client.connect(self.timeout, silent=not bool(self.verbose))
if connected:
logger.info("DLTClient connected to %s", self._client.servIP)
return connected
Expand All @@ -434,11 +437,22 @@ def run(self):
if not self._client_connect():
# keep trying to reconnect, until we either successfully
# connect or the stop_flag is set
if time.time() - self.last_message > 60:
# Once per minute log that we still have no DLT Connection
logger.info("DLT connection to %s missing since %s seconds",
self._ip_address, time.time() - self.last_connected)
self.last_message = time.time()
continue
try:
if self.last_connected:
logger.info("DLT connection to %s re-established after %s seconds",
self._ip_address, time() - self.last_connected)
self.last_connected = time.time()
res = py_dlt_client_main_loop(self._client, verbose=0, callback=self.handle, dumpfile=self.tracefile)
if res is False and not self.mp_stop_flag.is_set(): # main loop returned False
logger.error("DLT connection lost. Restarting DLT client")
logger.warning("DLT connection to %s lost. Restarting DLT client", self._ip_address)
self.last_connected = time.time()
self.last_message = time.time()
exception_occured = True
except KeyboardInterrupt:
exception_occured = True
Expand Down

0 comments on commit e481d16

Please sign in to comment.