Skip to content

Commit

Permalink
Low lift "winner" mirror logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Jun 22, 2024
1 parent 0cfcc43 commit bbc2fc5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/adapters/beatmap_mirrors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


class BeatmapMirror(ABC):
name: ClassVar[str]
base_url: ClassVar[str]

def __init__(self, *args: Any, **kwargs: Any) -> None:
Expand Down
1 change: 1 addition & 0 deletions app/adapters/beatmap_mirrors/mino.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class MinoMirror(BeatmapMirror):
name = "mino"
base_url = "https://catboy.best"

async def fetch_beatmap_zip_data(self, beatmapset_id: int) -> bytes | None:
Expand Down
40 changes: 25 additions & 15 deletions app/adapters/beatmap_mirrors/mirror_aggregate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import asyncio
from collections.abc import Awaitable
from collections.abc import Callable
from typing import ParamSpec
from typing import TypeVar
import logging
import time

from app.adapters.beatmap_mirrors import BeatmapMirror
from app.adapters.beatmap_mirrors.mino import MinoMirror
Expand All @@ -14,18 +12,13 @@
]


P = ParamSpec("P")
R = TypeVar("R")


async def run_with_semaphore(
semaphore: asyncio.Semaphore,
coro: Callable[P, Awaitable[R]],
*args: P.args,
**kwargs: P.kwargs,
) -> R:
mirror: BeatmapMirror,
beatmapset_id: int,
) -> tuple[BeatmapMirror, bytes | None]:
async with semaphore:
return await coro(*args, **kwargs)
return (mirror, await mirror.fetch_beatmap_zip_data(beatmapset_id))


class TimedOut: ...
Expand All @@ -50,11 +43,13 @@ async def fetch_beatmap_zip_data(beatmapset_id: int) -> bytes | TimedOut | None:
global_timeout = 15
semaphore = asyncio.Semaphore(concurrency_limit)

start_time = time.time()

coroutines = [
asyncio.create_task(
run_with_semaphore(
semaphore,
mirror.fetch_beatmap_zip_data,
mirror,
beatmapset_id,
),
)
Expand All @@ -74,4 +69,19 @@ async def fetch_beatmap_zip_data(beatmapset_id: int) -> bytes | TimedOut | None:

# TODO: log which mirrors finished, and which timed out

return first_result
mirror, result = first_result
if result is None:
return None

end_time = time.time()
ms_elapsed = (end_time - start_time) * 1000

logging.info(
f"{mirror.name} was the first to finish in {ms_elapsed:.2f}ms",
extra={
"mirror_name": mirror.name,
"beatmapset_id": beatmapset_id,
"ms_elapsed": ms_elapsed,
},
)
return result
1 change: 1 addition & 0 deletions app/adapters/beatmap_mirrors/osu_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class OsuDirectMirror(BeatmapMirror):
name = "osu_direct"
base_url = "https://api.osu.direct"

async def fetch_beatmap_zip_data(self, beatmapset_id: int) -> bytes | None:
Expand Down

0 comments on commit bbc2fc5

Please sign in to comment.