From 5d3c633bfd2c24d869815e752eb8854e8344c7ba Mon Sep 17 00:00:00 2001 From: Ian Baker Date: Fri, 25 Oct 2024 01:04:30 -0700 Subject: [PATCH] retry http errors, fix issue with handling of empty responses --- perspective_ranker.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/perspective_ranker.py b/perspective_ranker.py index ba7f178..a731b08 100644 --- a/perspective_ranker.py +++ b/perspective_ranker.py @@ -221,6 +221,7 @@ 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( @@ -228,18 +229,24 @@ async def score(self, attributes, statement, statement_id): ) 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 @@ -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}"