Skip to content

Commit

Permalink
Remove max_rertries as class attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
xjules committed Dec 6, 2024
1 parent 312e0a0 commit ca1e52c
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/_ert/forward_model_runner/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@ def __init__(
url: str,
token: Optional[str] = None,
cert: Optional[Union[str, bytes]] = None,
max_retries: int = 10,
connection_timeout: float = 5.0,
dealer_name: Optional[str] = None,
) -> None:
if max_retries is None:
max_retries = self.DEFAULT_MAX_RETRIES
self._connection_timeout = connection_timeout
self.url = url
self.token = token
Expand All @@ -89,7 +86,6 @@ def __init__(
self.socket.curve_publickey = client_public
self.socket.curve_serverkey = token.encode("utf-8")

self._max_retries = max_retries
self.loop = new_event_loop()
self._receiver_task: Optional[asyncio.Task[None]] = None

Check failure on line 90 in src/_ert/forward_model_runner/client.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Attribute "_receiver_task" already defined on line 58

Expand All @@ -105,7 +101,7 @@ async def connect(self) -> None:
raise

def send(
self, messages: str | list[str], max_retries: Optional[int] = None
self, messages: str | list[str], max_retries: int = DEFAULT_MAX_RETRIES
) -> None:
self.loop.run_until_complete(self._send(messages, max_retries))

Expand All @@ -128,16 +124,15 @@ async def _receiver(self) -> None:
self.socket.connect(self.url)

async def _send(
self, messages: str | list[str], max_retries: Optional[int] = None
self, messages: str | list[str], max_retries: int = DEFAULT_MAX_RETRIES
) -> None:
self._ack_event.clear()
if isinstance(messages, str):
messages = [messages]

retries = max_retries or self._max_retries
backoff = 1

while retries > 0:
while max_retries > 0:
try:
await self.socket.send_multipart(
[b""] + [message.encode("utf-8") for message in messages]
Expand All @@ -161,9 +156,9 @@ async def _send(
self.term()
raise

retries -= 1
if retries > 0:
logger.info(f"Retrying... ({retries} attempts left)")
max_retries -= 1
if max_retries > 0:
logger.info(f"Retrying... ({max_retries} attempts left)")
await asyncio.sleep(backoff)
backoff = min(backoff * 2, 10) # Exponential backoff

Expand Down

0 comments on commit ca1e52c

Please sign in to comment.