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

Improve resiliency of performance calculation requests #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 15 additions & 9 deletions app/api/score_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,21 @@ async def submit_score(
if score_exists:
return Response(b"error: no")

score.pp, score.sr = await app.usecases.performance.calculate_performance(
beatmap_id=beatmap.id,
beatmap_md5=beatmap.md5,
mode=score.mode,
mods=score.mods.value,
max_combo=score.max_combo,
accuracy=score.acc,
miss_count=score.nmiss,
)
try:
score.pp, score.sr = await app.usecases.performance.calculate_performance(
beatmap_id=beatmap.id,
beatmap_md5=beatmap.md5,
mode=score.mode,
mods=score.mods.value,
max_combo=score.max_combo,
accuracy=score.acc,
miss_count=score.nmiss,
)
except Exception:
logging.exception(
"Failed to calculate performance for score",
extra={"score_id": score.id},
)

# calculate the score's status
if score.passed:
Expand Down
35 changes: 19 additions & 16 deletions app/usecases/performance.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import annotations

import hashlib
import logging
from typing import TypedDict

from tenacity import retry
from tenacity import stop_after_attempt
from tenacity import wait_exponential

import app.state
import config
from app.constants.mode import Mode
from app.objects.path import Path
from app.reliability import retry_if_exception_network_related


class PerformanceScore(TypedDict):
Expand All @@ -20,25 +22,31 @@ class PerformanceScore(TypedDict):
miss_count: int


@retry(
retry=retry_if_exception_network_related(),
wait=wait_exponential(),
stop=stop_after_attempt(10),
reraise=True,
)
async def calculate_performances(
scores: list[PerformanceScore],
) -> list[tuple[float, float]]:
response = await app.state.services.http_client.post(
f"{config.PERFORMANCE_SERVICE_URL}/api/v1/calculate",
json=scores,
)
if not response.is_success:
logging.error(
"Performance service returned non-2xx code on calculate_performances",
extra={"status": response.status_code},
)
return [(0.0, 0.0)] * len(scores)
response.raise_for_status()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i agree with moving to response.raise_for_status() to hard fail requests rather than setting 0


data = response.json()
return [(result["pp"], result["stars"]) for result in data]


# TODO: split sr & pp calculations
@retry(
retry=retry_if_exception_network_related(),
wait=wait_exponential(),
stop=stop_after_attempt(10),
reraise=True,
)
async def calculate_performance(
*,
beatmap_id: int,
Expand All @@ -63,12 +71,7 @@ async def calculate_performance(
},
],
)
if response.status_code != 200:
logging.error(
"Performance service returned non-2xx code on calculate_performance",
extra={"status": response.status_code},
)
return 0.0, 0.0
response.raise_for_status()

data = response.json()[0]
return data["pp"], data["stars"]