Skip to content

Commit

Permalink
retry http errors, fix issue with handling of empty responses
Browse files Browse the repository at this point in the history
  • Loading branch information
raindrift committed Oct 25, 2024
1 parent ffb8901 commit 5d3c633
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions perspective_ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,25 +221,32 @@ async def score(self, attributes, statement, statement_id):
try:
start_time = time.time()
response_json = None
error_status = status.HTTP_500_INTERNAL_SERVER_ERROR
for _ in range(0,2):
try:
response = await self.client.post(
url=PERSPECTIVE_URL, json=data, headers=headers, timeout=self.scoring_timeout
)
response.raise_for_status()
response_json = await response.json()
error_status = None # success!
except asyncio.TimeoutError:
scoring_timeouts.inc()
logger.warning(
f"Timeout ({SCORING_TIMEOUT}s) scoring statement_id {statement_id} lenth {len(statement)}"
)
error_status = status.HTTP_504_GATEWAY_TIMEOUT
continue
except aiohttp.ClientResponseError as e:
logger.error(f"Retrying after http error for statement_id {statement_id}: {e}")
error_status = status.HTTP_502_BAD_GATEWAY
continue

latency = time.time() - start_time
scoring_latency.observe(latency)

if not response_json:
raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail=f"Gave up after 3 timeouts while scoring statement_id {statement_id}")
if error_status:
raise HTTPException(status_code=error_status, detail=f"Gave up after 2 errors while scoring statement_id {statement_id}")

results = []
scorable = True
Expand All @@ -261,10 +268,6 @@ async def score(self, attributes, statement, statement_id):
result = self.ScoredStatement(statement, results, statement_id, scorable, latency)
return result

except aiohttp.ClientResponseError as e:
logger.error(f"HTTP error occurred for statement_id {statement_id}: {e}, response: {e.response.text}")
raise

except Exception as e:
logger.error(
f"Unexpected error occurred for statement_id {statement_id}: {e}"
Expand Down

0 comments on commit 5d3c633

Please sign in to comment.