Skip to content

Commit

Permalink
Avoid using id(self)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed May 10, 2024
1 parent adf4f10 commit f51c561
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions zyte_api/_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from collections import Counter
from datetime import timedelta
from itertools import count
from typing import Union

from aiohttp import client_exceptions
Expand All @@ -24,6 +25,8 @@

logger = logging.getLogger(__name__)

_IDS = count()


_NETWORK_ERRORS = (
asyncio.TimeoutError, # could happen while reading the response body
Expand Down Expand Up @@ -65,13 +68,13 @@ class stop_on_count(stop_base):

def __init__(self, max_count: int) -> None:
self._max_count = max_count
self._counter_name = id(self)
self._counter_id = next(_IDS)

def __call__(self, retry_state: "RetryCallState") -> bool:
if not hasattr(retry_state, "counter"):
retry_state.counter = Counter() # type: ignore
retry_state.counter[self._counter_name] += 1 # type: ignore
if retry_state.counter[self._counter_name] >= self._max_count: # type: ignore
retry_state.counter[self._counter_id] += 1 # type: ignore
if retry_state.counter[self._counter_id] >= self._max_count: # type: ignore
return True
return False

Expand All @@ -95,32 +98,32 @@ class stop_after_uninterrupted_delay(stop_base):

def __init__(self, max_delay: time_unit_type) -> None:
self._max_delay = to_seconds(max_delay)
self._timer_name = id(self)
self._timer_id = next(_IDS)

def __call__(self, retry_state: "RetryCallState") -> bool:
if not hasattr(retry_state, "uninterrupted_start_times"):
retry_state.uninterrupted_start_times = {} # type: ignore
if self._timer_name not in retry_state.uninterrupted_start_times: # type: ignore
if self._timer_id not in retry_state.uninterrupted_start_times: # type: ignore
# First time.
retry_state.uninterrupted_start_times[self._timer_name] = [ # type: ignore
retry_state.uninterrupted_start_times[self._timer_id] = [ # type: ignore
retry_state.attempt_number,
retry_state.outcome_timestamp,
]
return False
attempt_number, start_time = retry_state.uninterrupted_start_times[ # type: ignore
self._timer_name
self._timer_id
]
if retry_state.attempt_number - attempt_number > 1:
# There was a different stop reason since the last attempt,
# resetting the timer.
retry_state.uninterrupted_start_times[self._timer_name] = [ # type: ignore
retry_state.uninterrupted_start_times[self._timer_id] = [ # type: ignore
retry_state.attempt_number,
retry_state.outcome_timestamp,
]
return False
if retry_state.outcome_timestamp - start_time < self._max_delay:
# Within time, do not stop, only increase the attempt count.
retry_state.uninterrupted_start_times[self._timer_name][0] += 1 # type: ignore
retry_state.uninterrupted_start_times[self._timer_id][0] += 1 # type: ignore
return False
return True

Expand Down

0 comments on commit f51c561

Please sign in to comment.