Skip to content

Commit

Permalink
sleep on 429
Browse files Browse the repository at this point in the history
  • Loading branch information
denniswittich committed Dec 3, 2024
1 parent b382199 commit a79c235
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions learning_loop_node/loop_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

logging.basicConfig(level=logging.INFO)

SLEEP_TIME_ON_429 = 5


class LoopCommunicationException(Exception):
"""Raised when there's an unexpected answer from the learning loop."""
Expand Down Expand Up @@ -97,7 +99,11 @@ async def get(self, path: str, requires_login: bool = True, api_prefix: str = '/
return await self._get(path, api_prefix)

async def _get(self, path: str, api_prefix: str) -> httpx.Response:
return await self.async_client.get(api_prefix+path)
response = await self.async_client.get(api_prefix+path)
while response.status_code == 429:
await asyncio.sleep(SLEEP_TIME_ON_429)
response = await self.async_client.get(api_prefix+path)
return response

async def put(self, path: str, files: Optional[List[str]] = None, requires_login: bool = True, api_prefix: str = '/api', **kwargs) -> httpx.Response:
if requires_login:
Expand All @@ -121,6 +127,9 @@ async def _put(self, path: str, files: Optional[List[str]], api_prefix: str, **k
try:
file_list = [('files', fh) for fh in file_handles] # Use file handles
response = await self.async_client.put(api_prefix+path, files=file_list)
while response.status_code == 429:
await asyncio.sleep(SLEEP_TIME_ON_429)
response = await self.async_client.put(api_prefix+path, files=file_list)
finally:
for fh in file_handles:
fh.close() # Ensure all files are closed
Expand All @@ -134,7 +143,11 @@ async def post(self, path: str, requires_login: bool = True, api_prefix: str = '
return await self._post(path, api_prefix, **kwargs)

async def _post(self, path, api_prefix='/api', **kwargs) -> httpx.Response:
return await self.async_client.post(api_prefix+path, **kwargs)
response = await self.async_client.post(api_prefix+path, **kwargs)
while response.status_code == 429:
await asyncio.sleep(SLEEP_TIME_ON_429)
response = await self.async_client.post(api_prefix+path, **kwargs)
return response

async def delete(self, path: str, requires_login: bool = True, api_prefix: str = '/api', **kwargs) -> httpx.Response:
if requires_login:
Expand All @@ -143,4 +156,8 @@ async def delete(self, path: str, requires_login: bool = True, api_prefix: str =
return await self._delete(path, api_prefix, **kwargs)

async def _delete(self, path, api_prefix, **kwargs) -> httpx.Response:
return await self.async_client.delete(api_prefix+path, **kwargs)
response = await self.async_client.delete(api_prefix+path, **kwargs)
while response.status_code == 429:
await asyncio.sleep(SLEEP_TIME_ON_429)
response = await self.async_client.delete(api_prefix+path, **kwargs)
return response

0 comments on commit a79c235

Please sign in to comment.