Skip to content

Commit 160ac76

Browse files
committed
fix: Prevent throttling race conditions by using an asyncio.Lock in the _throttle method.
1 parent 8bade66 commit 160ac76

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

amazon_creatorsapi/aio/api.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def __init__(
118118
self._credential_secret = credential_secret
119119
self._version = version
120120
self._last_query_time = time.time() - throttling
121+
self._throttle_lock = asyncio.Lock()
121122
self.tag = tag
122123
self.throttling = float(throttling)
123124

@@ -427,11 +428,16 @@ async def get_browse_nodes(
427428
return self._deserialize_browse_nodes(browse_nodes_result["browseNodes"])
428429

429430
async def _throttle(self) -> None:
430-
"""Wait for the throttling interval to elapse since the last API call."""
431-
wait_time = self.throttling - (time.time() - self._last_query_time)
432-
if wait_time > 0:
433-
await asyncio.sleep(wait_time)
434-
self._last_query_time = time.time()
431+
"""Wait for the throttling interval to elapse since the last API call.
432+
433+
Uses asyncio.Lock to prevent race conditions when multiple coroutines
434+
attempt to make concurrent requests.
435+
"""
436+
async with self._throttle_lock:
437+
wait_time = self.throttling - (time.time() - self._last_query_time)
438+
if wait_time > 0:
439+
await asyncio.sleep(wait_time)
440+
self._last_query_time = time.time()
435441

436442
async def _make_request(
437443
self,

0 commit comments

Comments
 (0)