Skip to content

Commit

Permalink
Do not retry in case of an nginx timeout error (#35)
Browse files Browse the repository at this point in the history
When the client request timeouts, the nginx returns an HTML error page.

We should not retry when that happens (which would fail the respone.json() call
as it is not a json).

This PR pulls the parsing of the response body out of the retry logic.
  • Loading branch information
mdumandag authored Oct 25, 2024
1 parent 4bb79df commit 7f3d779
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions upstash_vector/http.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import asyncio
import os
import time
import asyncio
from platform import python_version
from typing import Any, Dict

from httpx import Client, AsyncClient
from platform import python_version

from upstash_vector import __version__
from upstash_vector.errors import UpstashError
Expand Down Expand Up @@ -41,7 +42,7 @@ def execute_with_parameters(

for attempts_left in range(max(0, retries), -1, -1):
try:
response = client.post(url=url, headers=headers, json=payload).json()
response = client.post(url=url, headers=headers, json=payload)
break

except Exception as e:
Expand All @@ -53,10 +54,11 @@ def execute_with_parameters(
assert last_error is not None
raise last_error

if "error" in response:
raise UpstashError(response["error"])
body = response.json()
if "error" in body:
raise UpstashError(body["error"])

return response["result"]
return body["result"]


async def execute_with_parameters_async(
Expand All @@ -72,8 +74,7 @@ async def execute_with_parameters_async(

for attempts_left in range(max(0, retries), -1, -1):
try:
resp = await client.post(url=url, headers=headers, json=payload)
response = resp.json()
response = await client.post(url=url, headers=headers, json=payload)
break

except Exception as e:
Expand All @@ -85,7 +86,8 @@ async def execute_with_parameters_async(
assert last_error is not None
raise last_error

if "error" in response:
raise UpstashError(response["error"])
body = response.json()
if "error" in body:
raise UpstashError(body["error"])

return response["result"]
return body["result"]

0 comments on commit 7f3d779

Please sign in to comment.