Skip to content

Commit

Permalink
Merge pull request #913 from julep-ai/x/fix-cozo-retry
Browse files Browse the repository at this point in the history
fix(agents-api): Fix cozo query retry logic
  • Loading branch information
creatorrr authored Nov 29, 2024
2 parents 8aa82e5 + 1cb5932 commit a390974
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
40 changes: 36 additions & 4 deletions agents-api/agents_api/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

import pandas as pd
from fastapi import HTTPException
from httpcore import NetworkError, TimeoutException
from httpx import RequestError
from pydantic import BaseModel
from requests.exceptions import ConnectionError, Timeout

from ..common.utils.cozo import uuid_int_list_to_uuid4
from ..env import do_verify_developer, do_verify_developer_owns_resource
Expand Down Expand Up @@ -254,7 +257,21 @@ def wrapper(*args: P.args, client=None, **kwargs: P.kwargs) -> pd.DataFrame:

debug and print(repr(e))

if "busy" in (str(e) + str(getattr(e, "resp", e))).lower():
pretty_error = repr(e).lower()
cozo_busy = ("busy" in pretty_error) or (
"when executing against relation '_" in pretty_error
)
connection_error = isinstance(
e,
(
ConnectionError,
Timeout,
TimeoutException,
NetworkError,
RequestError,
),
)
if cozo_busy or connection_error:
raise HTTPException(
status_code=429, detail="Resource busy. Please try again later."
) from e
Expand Down Expand Up @@ -321,9 +338,10 @@ def is_resource_busy(e: Exception) -> bool:
return isinstance(e, HTTPException) and e.status_code == 429

@retry(
stop=stop_after_attempt(4),
wait=wait_exponential(multiplier=1, min=4, max=10),
stop=stop_after_attempt(6),
wait=wait_exponential(multiplier=1.2, min=3, max=10),
retry=retry_if_exception(is_resource_busy),
reraise=True,
)
@wraps(func)
async def wrapper(
Expand Down Expand Up @@ -367,7 +385,21 @@ async def wrapper(

debug and print(repr(e))

if "busy" in (str(e) + str(getattr(e, "resp", e))).lower():
pretty_error = repr(e).lower()
cozo_busy = ("busy" in pretty_error) or (
"when executing against relation '_" in pretty_error
)
connection_error = isinstance(
e,
(
ConnectionError,
Timeout,
TimeoutException,
NetworkError,
RequestError,
),
)
if cozo_busy or connection_error:
raise HTTPException(
status_code=429, detail="Resource busy. Please try again later."
) from e
Expand Down
2 changes: 1 addition & 1 deletion agents-api/gunicorn_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
debug = os.getenv("AGENTS_API_DEBUG", "false").lower() == "true"

# Gunicorn config variables
workers = (multiprocessing.cpu_count() - 1) if not debug else 1
workers = (multiprocessing.cpu_count() // 2) if not debug else 1
worker_class = "uvicorn.workers.UvicornWorker"
bind = "0.0.0.0:8080"
keepalive = 120
Expand Down

0 comments on commit a390974

Please sign in to comment.