Skip to content

Commit 31ccf70

Browse files
Aigars MahinovsAigars Mahinovs
authored andcommitted
Reduce reconnection message spam
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
1 parent 3c3a8da commit 31ccf70

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

dlt/dlt.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,8 @@ def connect(self, timeout=None):
929929
connected = None
930930
error_count = 0
931931
if not self.is_udp_multicast:
932-
logger.info("Connecting DLTClient using TCP Connection")
932+
if self.verbose:
933+
logger.info("Connecting DLTClient using TCP Connection")
933934
if timeout:
934935
end_time = time.time() + timeout
935936
while time.time() < end_time:
@@ -939,7 +940,7 @@ def connect(self, timeout=None):
939940
(ctypes.string_at(self.servIP), self.port), timeout=timeout_remaining
940941
)
941942
except IOError as exc:
942-
if error_count < MAX_LOG_IN_ROW:
943+
if error_count < MAX_LOG_IN_ROW and self.verbose:
943944
logger.debug(
944945
"DLT client connect failed to connect to %s:%s : %s", self.servIP, self.port, exc
945946
)
@@ -966,14 +967,16 @@ def connect(self, timeout=None):
966967
# connection loss in the main_loop below as described at
967968
# http://stefan.buettcher.org/cs/conn_closed.html
968969
self._connected_socket = socket.fromfd(self.sock, socket.AF_INET6, socket.SOCK_STREAM)
969-
if error_count > MAX_LOG_IN_ROW:
970+
if error_count > MAX_LOG_IN_ROW and self.verbose:
970971
logger.debug("Surpressed %d messages for failed connection attempts", error_count - MAX_LOG_IN_ROW)
971972

972973
else:
973-
logger.info("Connecting DLTClient using UDP Connection")
974+
if self.verbose:
975+
logger.info("Connecting DLTClient using UDP Connection")
974976
connected = dltlib.dlt_client_connect(ctypes.byref(self), self.verbose)
975977

976-
logger.info("DLT Connection return: %s", connected)
978+
if self.verbose:
979+
logger.info("DLT Connection return: %s", connected)
977980
return connected == DLT_RETURN_OK
978981

979982
def disconnect(self):

dlt/dlt_broker_handlers.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ def __init__(
400400
self.timeout = client_cfg.get("timeout", DLT_CLIENT_TIMEOUT)
401401
self._client = None
402402
self.tracefile = None
403+
self.last_connected = time.time()
404+
self.last_message = time.time() - 120.0
403405

404406
def is_valid_message(self, message):
405407
return message and (message.apid != "" or message.ctid != "")
@@ -411,12 +413,13 @@ def _client_connect(self):
411413
:returns: True if connected, False otherwise
412414
:rtype: bool
413415
"""
414-
logger.debug(
415-
"Creating DLTClient (ip_address='%s', Port='%s', logfile='%s')",
416-
self._ip_address,
417-
self._port,
418-
self._filename,
419-
)
416+
if self.verbose:
417+
logger.debug(
418+
"Creating DLTClient (ip_address='%s', Port='%s', logfile='%s')",
419+
self._ip_address,
420+
self._port,
421+
self._filename,
422+
)
420423
self._client = DLTClient(servIP=self._ip_address, port=self._port, verbose=self.verbose)
421424
connected = self._client.connect(self.timeout)
422425
if connected:
@@ -434,11 +437,28 @@ def run(self):
434437
if not self._client_connect():
435438
# keep trying to reconnect, until we either successfully
436439
# connect or the stop_flag is set
440+
if time.time() - self.last_message > 60:
441+
# Once per minute log that we still have no DLT Connection
442+
logger.info(
443+
"DLT connection to %s missing since %s seconds",
444+
self._ip_address,
445+
time.time() - self.last_connected,
446+
)
447+
self.last_message = time.time()
437448
continue
438449
try:
450+
if self.last_connected:
451+
logger.info(
452+
"DLT connection to %s re-established after %s seconds",
453+
self._ip_address,
454+
time.time() - self.last_connected,
455+
)
456+
self.last_connected = time.time()
439457
res = py_dlt_client_main_loop(self._client, verbose=0, callback=self.handle, dumpfile=self.tracefile)
440458
if res is False and not self.mp_stop_flag.is_set(): # main loop returned False
441-
logger.error("DLT connection lost. Restarting DLT client")
459+
logger.warning("DLT connection to %s lost. Restarting DLT client", self._ip_address)
460+
self.last_connected = time.time()
461+
self.last_message = time.time()
442462
exception_occured = True
443463
except KeyboardInterrupt:
444464
exception_occured = True

0 commit comments

Comments
 (0)