Skip to content

Commit

Permalink
fix: Retry on timeout (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-tutor authored Oct 10, 2024
1 parent d97985c commit 0ce5417
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 35 deletions.
7 changes: 1 addition & 6 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
from aiosocks import Socks5Addr

from tcp_modbus_aio.client import TCPModbusClient
from tcp_modbus_aio.exceptions import (
ModbusCommunicationFailureError,
ModbusCommunicationTimeoutError,
)
from tcp_modbus_aio.exceptions import ModbusCommunicationFailureError
from tcp_modbus_aio.typed_functions import ReadCoils

DIGITAL_IN_COILS = list(range(8))
Expand All @@ -32,8 +29,6 @@ async def example() -> None:
)
assert response is not None, "we expect a response from ReadCoils"
print(response.data) # noqa: T201
except ModbusCommunicationTimeoutError as e:
print(f"{type(e).__name__}({e})")
except ModbusCommunicationFailureError as e:
print(f"{type(e).__name__}({e})")

Expand Down
25 changes: 4 additions & 21 deletions tcp_modbus_aio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from tcp_modbus_aio.exceptions import (
ModbusCommunicationFailureError,
ModbusCommunicationTimeoutError,
ModbusConcurrencyError,
ModbusNotConnectedError,
)
from tcp_modbus_aio.ping import ping_ip
Expand Down Expand Up @@ -57,7 +57,6 @@ class TCPModbusClient:
KEEPALIVE_MAX_FAILS: ClassVar = 5

PING_LOOP_PERIOD: ClassVar = 1
CONSECUTIVE_TIMEOUTS_TO_RECONNECT: ClassVar = 5
COOLDOWN_BEFORE_RECONNECTING_SEC: ClassVar = 0.01

def __init__(
Expand Down Expand Up @@ -90,9 +89,6 @@ def __init__(
self._reader: asyncio.StreamReader | None = None
self._writer: asyncio.StreamWriter | None = None

# Number of current consecutive modbus calls that resulted in a timeout
self._consecutive_timeouts = 0

# Last ping time in seconds from ping loop, or None if the last ping failed
self._last_ping: float | None = None

Expand Down Expand Up @@ -192,7 +188,7 @@ async def _get_tcp_connection(
)
if self.logger is not None:
self.logger.warning(f"[{self}][_get_tcp_connection] {msg}")
raise ModbusCommunicationTimeoutError(msg)
raise ModbusCommunicationFailureError(msg)
except OSError:
msg = f"Cannot connect to TCP modbus device at {self.host}:{self.port}"
if self.logger is not None:
Expand Down Expand Up @@ -447,7 +443,7 @@ async def send_modbus_message(
self._comms_lock.acquire(), time_budget_remaining
)
except asyncio.TimeoutError:
raise ModbusCommunicationTimeoutError(
raise ModbusConcurrencyError(
f"Failed to acquire lock to send request {msg_str} to modbus device {self.host}"
)
time_budget_remaining -= lock_t()
Expand Down Expand Up @@ -515,20 +511,7 @@ async def send_modbus_message(
return None

raise
except asyncio.TimeoutError as e:
self._consecutive_timeouts += 1
if self._consecutive_timeouts >= self.CONSECUTIVE_TIMEOUTS_TO_RECONNECT:
if self.logger is not None:
self.logger.warning(
f"[{self}][send_modbus_message] {self._consecutive_timeouts} consecutive timeouts, "
"clearing connection"
)
self.clear_tcp_connection()

raise ModbusCommunicationTimeoutError(
f"Request {msg_str} timed out to {self.host}:{self.port}"
) from e
except (OSError, EOFError) as e:
except (OSError, EOFError, asyncio.TimeoutError) as e:
if self.logger is not None:
self.logger.warning(
f"[{self}][send_modbus_message] {type(e).__name__}({e}) while sending request {msg_str}, "
Expand Down
10 changes: 2 additions & 8 deletions tcp_modbus_aio/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
class ModbusCommunicationFailureError(ModbusError):
"""Generic modbus communication error."""

pass


class ModbusCommunicationTimeoutError(ModbusCommunicationFailureError):
"""Timeout in communicating with modbus device."""

pass
class ModbusConcurrencyError(ModbusError):
"""Too many concurrent requests"""


class ModbusNotConnectedError(ModbusCommunicationFailureError):
"""Modbus not connected error."""

pass

0 comments on commit 0ce5417

Please sign in to comment.