Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry http errors, fix issue with handling of empty responses #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading