Skip to content

Commit

Permalink
🔧 add auto retry on 502 bad gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
shroominic committed Nov 2, 2024
1 parent 71901a3 commit 473462e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/codeboxapi/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

import anyio
import httpx
from tenacity import (
retry,
retry_if_exception,
stop_after_attempt,
wait_exponential,
)

from .codebox import CodeBox
from .types import ExecChunk, RemoteFile
Expand Down Expand Up @@ -46,6 +52,14 @@ def __init__(
self.client = httpx.Client(base_url=self.url, headers=self.headers)
self.aclient = httpx.AsyncClient(base_url=self.url, headers=self.headers)

@retry(
retry=retry_if_exception(
lambda e: isinstance(e, httpx.HTTPStatusError)
and e.response.status_code == 502
),
wait=wait_exponential(multiplier=1, min=5, max=150),
stop=stop_after_attempt(3),
)
def stream_exec(
self,
code: t.Union[str, PathLike],
Expand All @@ -72,6 +86,14 @@ def stream_exec(
yield ExecChunk(type=t, content=c) # type: ignore[arg-type]
buffer = buffer[end:]

@retry(
retry=retry_if_exception(
lambda e: isinstance(e, httpx.HTTPStatusError)
and e.response.status_code == 502
),
wait=wait_exponential(multiplier=1, min=5, max=150),
stop=stop_after_attempt(3),
)
async def astream_exec(
self,
code: t.Union[str, PathLike],
Expand Down Expand Up @@ -105,6 +127,14 @@ async def astream_exec(
async for c in self.astream_exec(code, kernel, timeout, cwd):
yield c

@retry(
retry=retry_if_exception(
lambda e: isinstance(e, httpx.HTTPStatusError)
and e.response.status_code == 502
),
wait=wait_exponential(multiplier=1, min=5, max=150),
stop=stop_after_attempt(3),
)
def upload(
self,
file_name: str,
Expand All @@ -122,6 +152,14 @@ def upload(
).raise_for_status()
return RemoteFile(path=file_name, remote=self)

@retry(
retry=retry_if_exception(
lambda e: isinstance(e, httpx.HTTPStatusError)
and e.response.status_code == 502
),
wait=wait_exponential(multiplier=1, min=5, max=150),
stop=stop_after_attempt(3),
)
async def aupload(
self,
remote_file_path: str,
Expand All @@ -140,6 +178,14 @@ async def aupload(
response.raise_for_status()
return RemoteFile(path=remote_file_path, remote=self)

@retry(
retry=retry_if_exception(
lambda e: isinstance(e, httpx.HTTPStatusError)
and e.response.status_code == 502
),
wait=wait_exponential(multiplier=1, min=5, max=150),
stop=stop_after_attempt(3),
)
def stream_download(
self,
remote_file_path: str,
Expand All @@ -150,9 +196,18 @@ def stream_download(
url=f"/files/download/{remote_file_path}",
timeout=timeout,
) as response:
response.raise_for_status()
for chunk in response.iter_bytes():
yield chunk

@retry(
retry=retry_if_exception(
lambda e: isinstance(e, httpx.HTTPStatusError)
and e.response.status_code == 502
),
wait=wait_exponential(multiplier=1, min=5, max=150),
stop=stop_after_attempt(3),
)
async def astream_download(
self,
remote_file_path: str,
Expand All @@ -163,5 +218,6 @@ async def astream_download(
url=f"/files/download/{remote_file_path}",
timeout=timeout,
) as response:
response.raise_for_status()
async for chunk in response.aiter_bytes():
yield chunk

0 comments on commit 473462e

Please sign in to comment.