diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ac26d8d..f21283b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,6 +8,18 @@ on: jobs: test: runs-on: ubuntu-latest + services: + redis: + image: redis:7-alpine + ports: + - 6379:6379 + options: >- + --health-cmd "redis-cli ping" + --health-interval 5s + --health-timeout 3s + --health-retries 10 + env: + TEST_REDIS_URL: redis://localhost:6379 steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 diff --git a/README.md b/README.md index 437dd81..7cf36dd 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Redis 없이 인메모리 캐시로 API만 실행하려면 다음 명령을 사 CACHE_BACKEND=memory docker compose up -d mcstatus-api ``` -인메모리 캐시는 프로세스별로 독립적이며 재시작하면 초기화됩니다. 만료 항목은 해당 키를 다시 조회할 때 삭제되며, 용량 제한은 없습니다. +인메모리 캐시는 프로세스별로 독립적이며 재시작하면 초기화됩니다. 최대 10,000개를 보관하고, 저장 시 만료 항목과 가장 오래 사용하지 않은 항목을 정리합니다. ```bash # Check status and logs @@ -49,7 +49,6 @@ docker compose down | 400 | 잘못된 대상 주소 | | 502 | DNS 환경 또는 서버 응답 오류 | | 500 | API 내부 오류 | -| 304 | 조건부 요청의 캐시 적중. 이전 응답 재사용 | `status.latency`는 API 서버에서 Minecraft 서버까지의 status 응답 시간(ms)이며, 측정값이 없으면 `null`입니다. `timestamp`는 조회 완료 시각(UTC)입니다. HTTP 오류를 Minecraft 서버의 오프라인 상태로 판단하지 마세요. @@ -57,6 +56,9 @@ docker compose down - DNS 조회와 status 요청을 합쳐 3초로 제한합니다. - 접속 가능·불가 결과를 선택한 백엔드에 60초 캐싱합니다. 캐시 응답은 기존 측정값과 시각을 유지합니다. +- 같은 프로세스에서 같은 주소의 동시 요청은 하나의 조회 결과를 공유하며, 신규 조회를 공유한 응답은 모두 `MISS`로 표시합니다. 다른 주소는 병렬 조회하며, 워커·컨테이너 간 중복 조회는 막지 않습니다. - 4xx/5xx 오류는 캐싱하지 않습니다. -- `X-FastAPI-Cache: HIT/MISS`로 캐시 사용 여부를 확인할 수 있습니다. +- `X-Cache: HIT/MISS/BYPASS`로 캐시 재사용·신규 저장·우회를 구분합니다. +- `X-Cache-Created-At`은 캐시 저장 시각(UTC)이며 HIT에서도 유지됩니다. 저장하지 않은 응답에는 포함하지 않습니다. +- 요청의 캐시 지시어와 조건부 요청 헤더는 무시하며, 항상 서버의 60초 TTL을 따릅니다. - Redis 읽기·쓰기 실패는 로그에 기록하며, 캐시 없이 실제 조회 결과를 반환합니다. diff --git a/app/cache.py b/app/cache.py new file mode 100644 index 0000000..adaad48 --- /dev/null +++ b/app/cache.py @@ -0,0 +1,196 @@ +import asyncio +import hashlib +import json +import logging +import time +from collections import OrderedDict +from dataclasses import dataclass +from datetime import datetime, timezone +from typing import Awaitable, Callable, Protocol + +from fastapi import Response +from fastapi.encoders import jsonable_encoder +from fastapi.responses import JSONResponse +from redis import asyncio as aioredis + +logger = logging.getLogger(__name__) +CACHE_PREFIX = "mcstatus-api:status:v1:" + + +@dataclass(frozen=True) +class CacheEntry: + body: bytes + created_at: datetime + + def encode(self) -> str: + return json.dumps({ + "body": self.body.decode("utf-8"), + "created_at": self.created_at.isoformat(), + }) + + @classmethod + def decode(cls, value: bytes) -> "CacheEntry": + data = json.loads(value) + created_at = datetime.fromisoformat(data["created_at"]) + if created_at.tzinfo is None: + raise ValueError("Cache creation time must include a timezone") + + return cls( + body=data["body"].encode("utf-8"), + created_at=created_at, + ) + + +class CacheBackend(Protocol): + async def get(self, key: str) -> CacheEntry | None: + ... + + async def set(self, key: str, entry: CacheEntry, expire: int) -> None: + ... + + +class MemoryCacheBackend: + def __init__( + self, + max_entries: int = 10_000, + clock: Callable[[], float] = time.monotonic, + ): + if max_entries < 1: + raise ValueError("max_entries must be positive") + + self._store: OrderedDict[str, tuple[float, CacheEntry]] = OrderedDict() + self._max_entries = max_entries + self._clock = clock + + async def get(self, key: str) -> CacheEntry | None: + value = self._store.get(key) + if value is None: + return None + + deadline, entry = value + if deadline <= self._clock(): + del self._store[key] + return None + + self._store.move_to_end(key) + return entry + + async def set(self, key: str, entry: CacheEntry, expire: int) -> None: + now = self._clock() + expired_keys = [ + stored_key + for stored_key, (deadline, _) in self._store.items() + if deadline <= now + ] + for expired_key in expired_keys: + del self._store[expired_key] + + self._store[key] = (now + expire, entry) + self._store.move_to_end(key) + while len(self._store) > self._max_entries: + self._store.popitem(last=False) + + +class RedisCacheBackend: + def __init__(self, redis: aioredis.Redis): + self.redis = redis + + async def get(self, key: str) -> CacheEntry | None: + value = await self.redis.get(CACHE_PREFIX + key) + if value is None: + return None + + return CacheEntry.decode(value) + + async def set(self, key: str, entry: CacheEntry, expire: int) -> None: + await self.redis.set(CACHE_PREFIX + key, entry.encode(), ex=expire) + + +class StatusCache: + def __init__(self, backend: CacheBackend, expire: int = 60): + self.backend = backend + self.expire = expire + self._inflight: dict[str, asyncio.Task[Response]] = {} + + async def respond( + self, + address: str, + query: Callable[[str], Awaitable[object]], + ) -> Response: + address_hash = hashlib.sha256(address.encode("utf-8")) + cache_key = address_hash.hexdigest() + task = self._inflight.get(cache_key) + if task is None or task.done(): + task = asyncio.create_task(self._respond(address, cache_key, query)) + self._inflight[cache_key] = task + task.add_done_callback(lambda completed: self._finish_query(cache_key, completed)) + + # A disconnected client must not cancel the lookup shared by other clients. + return await asyncio.shield(task) + + def _finish_query(self, cache_key: str, task: asyncio.Task[Response]) -> None: + if self._inflight.get(cache_key) is task: + del self._inflight[cache_key] + + # Observe failures even when all clients stopped waiting for this task. + if not task.cancelled(): + task.exception() + + async def close(self) -> None: + tasks = list(self._inflight.values()) + for task in tasks: + task.cancel() + + await asyncio.gather(*tasks, return_exceptions=True) + + async def _respond( + self, + address: str, + cache_key: str, + query: Callable[[str], Awaitable[object]], + ) -> Response: + read_failed = False + + try: + entry = await self.backend.get(cache_key) + except Exception: + logger.warning("Cache read failed; querying server directly", exc_info=True) + read_failed = True + else: + if entry is not None: + return self.cached_response(entry, "HIT") + + result = await query(address) + body = JSONResponse(jsonable_encoder(result)).body + if read_failed: + return self.bypass_response(body) + + entry = CacheEntry(body=body, created_at=datetime.now(timezone.utc)) + try: + await self.backend.set(cache_key, entry, self.expire) + except Exception: + logger.warning("Cache write failed; returning uncached result", exc_info=True) + return self.bypass_response(body) + + return self.cached_response(entry, "MISS") + + @staticmethod + def bypass_response(body: bytes) -> Response: + return Response( + content=body, + media_type="application/json", + headers={ + "X-Cache": "BYPASS", + }, + ) + + @staticmethod + def cached_response( + entry: CacheEntry, + cache_status: str, + ) -> Response: + headers = { + "X-Cache": cache_status, + "X-Cache-Created-At": entry.created_at.isoformat(), + } + return Response(content=entry.body, media_type="application/json", headers=headers) diff --git a/app/main.py b/app/main.py index 32bb7d9..3b48253 100644 --- a/app/main.py +++ b/app/main.py @@ -10,17 +10,15 @@ from dns.exception import DNSException from dns.resolver import NXDOMAIN, NoAnswer -from fastapi import FastAPI, HTTPException, Request +from fastapi import FastAPI, HTTPException, Request, Response from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse -from fastapi_cache import FastAPICache -from fastapi_cache.backends.inmemory import InMemoryBackend -from fastapi_cache.backends.redis import RedisBackend -from fastapi_cache.decorator import cache from mcstatus import JavaServer from mcstatus.address import Address from redis import asyncio as aioredis +from app.cache import MemoryCacheBackend, RedisCacheBackend, StatusCache + CACHE_TTL_SECONDS = 60 QUERY_TIMEOUT_SECONDS = 3 @@ -33,25 +31,27 @@ @asynccontextmanager -async def lifespan(_: FastAPI) -> AsyncIterator[None]: +async def lifespan(application: FastAPI) -> AsyncIterator[None]: cache_backend = os.getenv("CACHE_BACKEND", "redis") redis = None if cache_backend == "memory": - backend = InMemoryBackend() + backend = MemoryCacheBackend() elif cache_backend == "redis": redis_url = os.getenv("REDIS_URL", "redis://localhost") redis = aioredis.from_url(redis_url) - backend = RedisBackend(redis) + backend = RedisCacheBackend(redis) else: raise ValueError("CACHE_BACKEND must be 'memory' or 'redis'") + status_cache = StatusCache(backend, expire=CACHE_TTL_SECONDS) try: - FastAPICache.init(backend, prefix="mcstatus-api-cache") + application.state.status_cache = status_cache yield finally: + await status_cache.close() if redis is not None: - await redis.close() + await redis.close(close_connection_pool=True) app = FastAPI(lifespan=lifespan) @@ -71,7 +71,6 @@ async def internal_error_handler(request: Request, exc: Exception) -> JSONRespon return JSONResponse( status_code=500, content={"detail": {"code": "INTERNAL_ERROR", "message": "Internal server error"}}, - headers={"Cache-Control": "no-store"}, ) @@ -112,21 +111,25 @@ async def query_status(address: str): @app.get( "/java/{address}", + response_model=JavaServerResponse, responses={ 400: {"description": "Invalid target address (INVALID_ADDRESS)"}, 502: {"description": "Protocol or lookup infrastructure error (UPSTREAM_ERROR)"}, 500: {"description": "Internal API error (INTERNAL_ERROR)"}, }, ) -@cache(expire=CACHE_TTL_SECONDS, namespace="java-status") -async def get_java_server(address: str) -> JavaServerResponse: +async def get_java_server(address: str, request: Request) -> Response: + status_cache = request.app.state.status_cache + return await status_cache.respond(address, build_java_server_response) + + +async def build_java_server_response(address: str) -> JavaServerResponse: try: Address.parse_address(address, default_port=JavaServer.DEFAULT_PORT) except ValueError as exc: raise HTTPException( status_code=400, detail={"code": "INVALID_ADDRESS", "message": "Invalid Minecraft server address"}, - headers={"Cache-Control": "no-store"}, ) from exc try: @@ -153,7 +156,6 @@ async def get_java_server(address: str) -> JavaServerResponse: raise HTTPException( status_code=502, detail={"code": "UPSTREAM_ERROR", "message": "Failed to query Minecraft server status"}, - headers={"Cache-Control": "no-store"}, ) from exc # Preserve the status structure and serialize invalid latency as null. diff --git a/poetry.lock b/poetry.lock index baee8b2..c86d369 100644 --- a/poetry.lock +++ b/poetry.lock @@ -184,31 +184,6 @@ uvicorn = {version = ">=0.12.0", extras = ["standard"], optional = true, markers all = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.5)", "httpx (>=0.23.0)", "itsdangerous (>=1.1.0)", "jinja2 (>=3.1.5)", "orjson (>=3.2.1)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "pyyaml (>=5.3.1)", "ujson (>=4.0.1,!=4.0.2,!=4.1.0,!=4.2.0,!=4.3.0,!=5.0.0,!=5.1.0)", "uvicorn[standard] (>=0.12.0)"] standard = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.5)", "httpx (>=0.23.0)", "jinja2 (>=3.1.5)", "python-multipart (>=0.0.18)", "uvicorn[standard] (>=0.12.0)"] -[[package]] -name = "fastapi-cache2" -version = "0.2.2" -description = "Cache for FastAPI" -optional = false -python-versions = "<4.0,>=3.8" -groups = ["main"] -files = [ - {file = "fastapi_cache2-0.2.2-py3-none-any.whl", hash = "sha256:e1fae86d8eaaa6c8501dfe08407f71d69e87cc6748042d59d51994000532846c"}, - {file = "fastapi_cache2-0.2.2.tar.gz", hash = "sha256:71bf4450117dc24224ec120be489dbe09e331143c9f74e75eb6f576b78926026"}, -] - -[package.dependencies] -fastapi = "*" -pendulum = ">=3.0.0,<4.0.0" -redis = {version = ">=4.2.0rc1,<5.0.0", optional = true, markers = "extra == \"redis\" or extra == \"all\""} -typing-extensions = ">=4.1.0" -uvicorn = "*" - -[package.extras] -all = ["aiobotocore (>=2.13.1,<3.0.0)", "aiomcache (>=0.8.2,<0.9.0)", "redis (>=4.2.0rc1,<5.0.0)"] -dynamodb = ["aiobotocore (>=2.13.1,<3.0.0)"] -memcache = ["aiomcache (>=0.8.2,<0.9.0)"] -redis = ["redis (>=4.2.0rc1,<5.0.0)"] - [[package]] name = "fastapi-cli" version = "0.0.7" @@ -501,106 +476,6 @@ files = [ {file = "mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba"}, ] -[[package]] -name = "pendulum" -version = "3.0.0" -description = "Python datetimes made easy" -optional = false -python-versions = ">=3.8" -groups = ["main"] -files = [ - {file = "pendulum-3.0.0-cp310-cp310-macosx_10_12_x86_64.whl", hash = "sha256:2cf9e53ef11668e07f73190c805dbdf07a1939c3298b78d5a9203a86775d1bfd"}, - {file = "pendulum-3.0.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:fb551b9b5e6059377889d2d878d940fd0bbb80ae4810543db18e6f77b02c5ef6"}, - {file = "pendulum-3.0.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6c58227ac260d5b01fc1025176d7b31858c9f62595737f350d22124a9a3ad82d"}, - {file = "pendulum-3.0.0-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:60fb6f415fea93a11c52578eaa10594568a6716602be8430b167eb0d730f3332"}, - {file = "pendulum-3.0.0-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b69f6b4dbcb86f2c2fe696ba991e67347bcf87fe601362a1aba6431454b46bde"}, - {file = "pendulum-3.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:138afa9c373ee450ede206db5a5e9004fd3011b3c6bbe1e57015395cd076a09f"}, - {file = "pendulum-3.0.0-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:83d9031f39c6da9677164241fd0d37fbfc9dc8ade7043b5d6d62f56e81af8ad2"}, - {file = "pendulum-3.0.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:0c2308af4033fa534f089595bcd40a95a39988ce4059ccd3dc6acb9ef14ca44a"}, - {file = "pendulum-3.0.0-cp310-none-win_amd64.whl", hash = "sha256:9a59637cdb8462bdf2dbcb9d389518c0263799189d773ad5c11db6b13064fa79"}, - {file = "pendulum-3.0.0-cp311-cp311-macosx_10_12_x86_64.whl", hash = "sha256:3725245c0352c95d6ca297193192020d1b0c0f83d5ee6bb09964edc2b5a2d508"}, - {file = "pendulum-3.0.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:6c035f03a3e565ed132927e2c1b691de0dbf4eb53b02a5a3c5a97e1a64e17bec"}, - {file = "pendulum-3.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:597e66e63cbd68dd6d58ac46cb7a92363d2088d37ccde2dae4332ef23e95cd00"}, - {file = "pendulum-3.0.0-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:99a0f8172e19f3f0c0e4ace0ad1595134d5243cf75985dc2233e8f9e8de263ca"}, - {file = "pendulum-3.0.0-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:77d8839e20f54706aed425bec82a83b4aec74db07f26acd039905d1237a5e1d4"}, - {file = "pendulum-3.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:afde30e8146292b059020fbc8b6f8fd4a60ae7c5e6f0afef937bbb24880bdf01"}, - {file = "pendulum-3.0.0-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:660434a6fcf6303c4efd36713ca9212c753140107ee169a3fc6c49c4711c2a05"}, - {file = "pendulum-3.0.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:dee9e5a48c6999dc1106eb7eea3e3a50e98a50651b72c08a87ee2154e544b33e"}, - {file = "pendulum-3.0.0-cp311-none-win_amd64.whl", hash = "sha256:d4cdecde90aec2d67cebe4042fd2a87a4441cc02152ed7ed8fb3ebb110b94ec4"}, - {file = "pendulum-3.0.0-cp311-none-win_arm64.whl", hash = "sha256:773c3bc4ddda2dda9f1b9d51fe06762f9200f3293d75c4660c19b2614b991d83"}, - {file = "pendulum-3.0.0-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:409e64e41418c49f973d43a28afe5df1df4f1dd87c41c7c90f1a63f61ae0f1f7"}, - {file = "pendulum-3.0.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:a38ad2121c5ec7c4c190c7334e789c3b4624798859156b138fcc4d92295835dc"}, - {file = "pendulum-3.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:fde4d0b2024b9785f66b7f30ed59281bd60d63d9213cda0eb0910ead777f6d37"}, - {file = "pendulum-3.0.0-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:4b2c5675769fb6d4c11238132962939b960fcb365436b6d623c5864287faa319"}, - {file = "pendulum-3.0.0-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:8af95e03e066826f0f4c65811cbee1b3123d4a45a1c3a2b4fc23c4b0dff893b5"}, - {file = "pendulum-3.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2165a8f33cb15e06c67070b8afc87a62b85c5a273e3aaa6bc9d15c93a4920d6f"}, - {file = "pendulum-3.0.0-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:ad5e65b874b5e56bd942546ea7ba9dd1d6a25121db1c517700f1c9de91b28518"}, - {file = "pendulum-3.0.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:17fe4b2c844bbf5f0ece69cfd959fa02957c61317b2161763950d88fed8e13b9"}, - {file = "pendulum-3.0.0-cp312-none-win_amd64.whl", hash = "sha256:78f8f4e7efe5066aca24a7a57511b9c2119f5c2b5eb81c46ff9222ce11e0a7a5"}, - {file = "pendulum-3.0.0-cp312-none-win_arm64.whl", hash = "sha256:28f49d8d1e32aae9c284a90b6bb3873eee15ec6e1d9042edd611b22a94ac462f"}, - {file = "pendulum-3.0.0-cp37-cp37m-macosx_10_12_x86_64.whl", hash = "sha256:d4e2512f4e1a4670284a153b214db9719eb5d14ac55ada5b76cbdb8c5c00399d"}, - {file = "pendulum-3.0.0-cp37-cp37m-macosx_11_0_arm64.whl", hash = "sha256:3d897eb50883cc58d9b92f6405245f84b9286cd2de6e8694cb9ea5cb15195a32"}, - {file = "pendulum-3.0.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2e169cc2ca419517f397811bbe4589cf3cd13fca6dc38bb352ba15ea90739ebb"}, - {file = "pendulum-3.0.0-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f17c3084a4524ebefd9255513692f7e7360e23c8853dc6f10c64cc184e1217ab"}, - {file = "pendulum-3.0.0-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:826d6e258052715f64d05ae0fc9040c0151e6a87aae7c109ba9a0ed930ce4000"}, - {file = "pendulum-3.0.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d2aae97087872ef152a0c40e06100b3665d8cb86b59bc8471ca7c26132fccd0f"}, - {file = "pendulum-3.0.0-cp37-cp37m-musllinux_1_1_aarch64.whl", hash = "sha256:ac65eeec2250d03106b5e81284ad47f0d417ca299a45e89ccc69e36130ca8bc7"}, - {file = "pendulum-3.0.0-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:a5346d08f3f4a6e9e672187faa179c7bf9227897081d7121866358af369f44f9"}, - {file = "pendulum-3.0.0-cp37-none-win_amd64.whl", hash = "sha256:235d64e87946d8f95c796af34818c76e0f88c94d624c268693c85b723b698aa9"}, - {file = "pendulum-3.0.0-cp38-cp38-macosx_10_12_x86_64.whl", hash = "sha256:6a881d9c2a7f85bc9adafcfe671df5207f51f5715ae61f5d838b77a1356e8b7b"}, - {file = "pendulum-3.0.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:d7762d2076b9b1cb718a6631ad6c16c23fc3fac76cbb8c454e81e80be98daa34"}, - {file = "pendulum-3.0.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4e8e36a8130819d97a479a0e7bf379b66b3b1b520e5dc46bd7eb14634338df8c"}, - {file = "pendulum-3.0.0-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7dc843253ac373358ffc0711960e2dd5b94ab67530a3e204d85c6e8cb2c5fa10"}, - {file = "pendulum-3.0.0-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:0a78ad3635d609ceb1e97d6aedef6a6a6f93433ddb2312888e668365908c7120"}, - {file = "pendulum-3.0.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b30a137e9e0d1f751e60e67d11fc67781a572db76b2296f7b4d44554761049d6"}, - {file = "pendulum-3.0.0-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:c95984037987f4a457bb760455d9ca80467be792236b69d0084f228a8ada0162"}, - {file = "pendulum-3.0.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:d29c6e578fe0f893766c0d286adbf0b3c726a4e2341eba0917ec79c50274ec16"}, - {file = "pendulum-3.0.0-cp38-none-win_amd64.whl", hash = "sha256:deaba8e16dbfcb3d7a6b5fabdd5a38b7c982809567479987b9c89572df62e027"}, - {file = "pendulum-3.0.0-cp39-cp39-macosx_10_12_x86_64.whl", hash = "sha256:b11aceea5b20b4b5382962b321dbc354af0defe35daa84e9ff3aae3c230df694"}, - {file = "pendulum-3.0.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:a90d4d504e82ad236afac9adca4d6a19e4865f717034fc69bafb112c320dcc8f"}, - {file = "pendulum-3.0.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:825799c6b66e3734227756fa746cc34b3549c48693325b8b9f823cb7d21b19ac"}, - {file = "pendulum-3.0.0-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ad769e98dc07972e24afe0cff8d365cb6f0ebc7e65620aa1976fcfbcadc4c6f3"}, - {file = "pendulum-3.0.0-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:a6fc26907eb5fb8cc6188cc620bc2075a6c534d981a2f045daa5f79dfe50d512"}, - {file = "pendulum-3.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0c717eab1b6d898c00a3e0fa7781d615b5c5136bbd40abe82be100bb06df7a56"}, - {file = "pendulum-3.0.0-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:3ddd1d66d1a714ce43acfe337190be055cdc221d911fc886d5a3aae28e14b76d"}, - {file = "pendulum-3.0.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:822172853d7a9cf6da95d7b66a16c7160cb99ae6df55d44373888181d7a06edc"}, - {file = "pendulum-3.0.0-cp39-none-win_amd64.whl", hash = "sha256:840de1b49cf1ec54c225a2a6f4f0784d50bd47f68e41dc005b7f67c7d5b5f3ae"}, - {file = "pendulum-3.0.0-pp310-pypy310_pp73-macosx_10_12_x86_64.whl", hash = "sha256:3b1f74d1e6ffe5d01d6023870e2ce5c2191486928823196f8575dcc786e107b1"}, - {file = "pendulum-3.0.0-pp310-pypy310_pp73-macosx_11_0_arm64.whl", hash = "sha256:729e9f93756a2cdfa77d0fc82068346e9731c7e884097160603872686e570f07"}, - {file = "pendulum-3.0.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e586acc0b450cd21cbf0db6bae386237011b75260a3adceddc4be15334689a9a"}, - {file = "pendulum-3.0.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:22e7944ffc1f0099a79ff468ee9630c73f8c7835cd76fdb57ef7320e6a409df4"}, - {file = "pendulum-3.0.0-pp310-pypy310_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:fa30af36bd8e50686846bdace37cf6707bdd044e5cb6e1109acbad3277232e04"}, - {file = "pendulum-3.0.0-pp310-pypy310_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:440215347b11914ae707981b9a57ab9c7b6983ab0babde07063c6ee75c0dc6e7"}, - {file = "pendulum-3.0.0-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:314c4038dc5e6a52991570f50edb2f08c339debdf8cea68ac355b32c4174e820"}, - {file = "pendulum-3.0.0-pp37-pypy37_pp73-macosx_10_12_x86_64.whl", hash = "sha256:5acb1d386337415f74f4d1955c4ce8d0201978c162927d07df8eb0692b2d8533"}, - {file = "pendulum-3.0.0-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a789e12fbdefaffb7b8ac67f9d8f22ba17a3050ceaaa635cd1cc4645773a4b1e"}, - {file = "pendulum-3.0.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:860aa9b8a888e5913bd70d819306749e5eb488e6b99cd6c47beb701b22bdecf5"}, - {file = "pendulum-3.0.0-pp37-pypy37_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:5ebc65ea033ef0281368217fbf59f5cb05b338ac4dd23d60959c7afcd79a60a0"}, - {file = "pendulum-3.0.0-pp37-pypy37_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:d9fef18ab0386ef6a9ac7bad7e43ded42c83ff7ad412f950633854f90d59afa8"}, - {file = "pendulum-3.0.0-pp38-pypy38_pp73-macosx_10_12_x86_64.whl", hash = "sha256:1c134ba2f0571d0b68b83f6972e2307a55a5a849e7dac8505c715c531d2a8795"}, - {file = "pendulum-3.0.0-pp38-pypy38_pp73-macosx_11_0_arm64.whl", hash = "sha256:385680812e7e18af200bb9b4a49777418c32422d05ad5a8eb85144c4a285907b"}, - {file = "pendulum-3.0.0-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:9eec91cd87c59fb32ec49eb722f375bd58f4be790cae11c1b70fac3ee4f00da0"}, - {file = "pendulum-3.0.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4386bffeca23c4b69ad50a36211f75b35a4deb6210bdca112ac3043deb7e494a"}, - {file = "pendulum-3.0.0-pp38-pypy38_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:dfbcf1661d7146d7698da4b86e7f04814221081e9fe154183e34f4c5f5fa3bf8"}, - {file = "pendulum-3.0.0-pp38-pypy38_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:04a1094a5aa1daa34a6b57c865b25f691848c61583fb22722a4df5699f6bf74c"}, - {file = "pendulum-3.0.0-pp38-pypy38_pp73-win_amd64.whl", hash = "sha256:5b0ec85b9045bd49dd3a3493a5e7ddfd31c36a2a60da387c419fa04abcaecb23"}, - {file = "pendulum-3.0.0-pp39-pypy39_pp73-macosx_10_12_x86_64.whl", hash = "sha256:0a15b90129765b705eb2039062a6daf4d22c4e28d1a54fa260892e8c3ae6e157"}, - {file = "pendulum-3.0.0-pp39-pypy39_pp73-macosx_11_0_arm64.whl", hash = "sha256:bb8f6d7acd67a67d6fedd361ad2958ff0539445ef51cbe8cd288db4306503cd0"}, - {file = "pendulum-3.0.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:fd69b15374bef7e4b4440612915315cc42e8575fcda2a3d7586a0d88192d0c88"}, - {file = "pendulum-3.0.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dc00f8110db6898360c53c812872662e077eaf9c75515d53ecc65d886eec209a"}, - {file = "pendulum-3.0.0-pp39-pypy39_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:83a44e8b40655d0ba565a5c3d1365d27e3e6778ae2a05b69124db9e471255c4a"}, - {file = "pendulum-3.0.0-pp39-pypy39_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:1a3604e9fbc06b788041b2a8b78f75c243021e0f512447806a6d37ee5214905d"}, - {file = "pendulum-3.0.0-pp39-pypy39_pp73-win_amd64.whl", hash = "sha256:92c307ae7accebd06cbae4729f0ba9fa724df5f7d91a0964b1b972a22baa482b"}, - {file = "pendulum-3.0.0.tar.gz", hash = "sha256:5d034998dea404ec31fae27af6b22cff1708f830a1ed7353be4d1019bb9f584e"}, -] - -[package.dependencies] -python-dateutil = ">=2.6" -tzdata = ">=2020.1" - -[package.extras] -test = ["time-machine (>=2.6.0) ; implementation_name != \"pypy\""] - [[package]] name = "pydantic" version = "2.10.6" @@ -750,21 +625,6 @@ files = [ [package.extras] windows-terminal = ["colorama (>=0.4.6)"] -[[package]] -name = "python-dateutil" -version = "2.9.0.post0" -description = "Extensions to the standard Python datetime module" -optional = false -python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" -groups = ["main"] -files = [ - {file = "python-dateutil-2.9.0.post0.tar.gz", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3"}, - {file = "python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427"}, -] - -[package.dependencies] -six = ">=1.5" - [[package]] name = "python-dotenv" version = "1.0.1" @@ -944,18 +804,6 @@ files = [ {file = "shellingham-1.5.4.tar.gz", hash = "sha256:8dbca0739d487e5bd35ab3ca4b36e11c4078f3a234bfce294b0a0291363404de"}, ] -[[package]] -name = "six" -version = "1.17.0" -description = "Python 2 and 3 compatibility utilities" -optional = false -python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" -groups = ["main"] -files = [ - {file = "six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274"}, - {file = "six-1.17.0.tar.gz", hash = "sha256:ff70335d468e7eb6ec65b95b99d3a2836546063f63acc5171de367e834932a81"}, -] - [[package]] name = "sniffio" version = "1.3.1" @@ -1016,18 +864,6 @@ files = [ {file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"}, ] -[[package]] -name = "tzdata" -version = "2025.1" -description = "Provider of IANA time zone data" -optional = false -python-versions = ">=2" -groups = ["main"] -files = [ - {file = "tzdata-2025.1-py2.py3-none-any.whl", hash = "sha256:7e127113816800496f027041c570f50bcd464a020098a3b6b199517772303639"}, - {file = "tzdata-2025.1.tar.gz", hash = "sha256:24894909e88cdb28bd1636c6887801df64cb485bd593f2fd83ef29075a81d694"}, -] - [[package]] name = "uvicorn" version = "0.34.0" @@ -1274,4 +1110,4 @@ files = [ [metadata] lock-version = "2.1" python-versions = ">=3.10,<3.11" -content-hash = "c9573b6eaf4b3e8efef0a40814b3690bebbd8bc44059cf8d32bb41bf0b4342bf" +content-hash = "89f23764b1bd8d0b18f0e380384abd05ec93cc0c4756fb6cccd2defee05da3b2" diff --git a/pyproject.toml b/pyproject.toml index c525d17..c9b3fc1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ requires-python = ">=3.10" dependencies = [ "uvicorn (>=0.34.0,<0.35.0)", "mcstatus (>=11.1.1,<12.0.0)", - "fastapi-cache2[redis] (>=0.2.2,<0.3.0)", + "redis (>=4.6.0,<5.0.0)", "fastapi[standard] (>=0.115.8,<0.116.0)" ] diff --git a/requirements.txt b/requirements.txt index d5d5c2c..d581bc9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,6 @@ colorama==0.4.6 ; (platform_system == "Windows" or sys_platform == "win32") and dnspython==2.7.0 ; python_version == "3.10" email-validator==2.2.0 ; python_version == "3.10" exceptiongroup==1.2.2 ; python_version == "3.10" -fastapi-cache2==0.2.2 ; python_version == "3.10" fastapi-cli==0.0.7 ; python_version == "3.10" fastapi==0.115.8 ; python_version == "3.10" h11==0.14.0 ; python_version == "3.10" @@ -21,11 +20,9 @@ markdown-it-py==3.0.0 ; python_version == "3.10" markupsafe==3.0.2 ; python_version == "3.10" mcstatus==11.1.1 ; python_version == "3.10" mdurl==0.1.2 ; python_version == "3.10" -pendulum==3.0.0 ; python_version == "3.10" pydantic-core==2.27.2 ; python_version == "3.10" pydantic==2.10.6 ; python_version == "3.10" pygments==2.19.1 ; python_version == "3.10" -python-dateutil==2.9.0.post0 ; python_version == "3.10" python-dotenv==1.0.1 ; python_version == "3.10" python-multipart==0.0.20 ; python_version == "3.10" pyyaml==6.0.2 ; python_version == "3.10" @@ -34,12 +31,10 @@ rich-toolkit==0.13.2 ; python_version == "3.10" rich==13.9.4 ; python_version == "3.10" setuptools==75.8.0 ; python_version == "3.10" shellingham==1.5.4 ; python_version == "3.10" -six==1.17.0 ; python_version == "3.10" sniffio==1.3.1 ; python_version == "3.10" starlette==0.45.3 ; python_version == "3.10" typer==0.15.1 ; python_version == "3.10" typing-extensions==4.12.2 ; python_version == "3.10" -tzdata==2025.1 ; python_version == "3.10" uvicorn==0.34.0 ; python_version == "3.10" uvloop==0.21.0 ; sys_platform != "win32" and sys_platform != "cygwin" and platform_python_implementation != "PyPy" and python_version == "3.10" watchfiles==1.0.4 ; python_version == "3.10" diff --git a/tests/test_cache.py b/tests/test_cache.py new file mode 100644 index 0000000..81fa97e --- /dev/null +++ b/tests/test_cache.py @@ -0,0 +1,106 @@ +import os +import unittest +import uuid +from datetime import datetime, timezone +from unittest.mock import AsyncMock, MagicMock + +from redis import asyncio as aioredis + +from app.cache import CACHE_PREFIX, CacheEntry, MemoryCacheBackend, RedisCacheBackend + + +class MemoryCacheTests(unittest.IsolatedAsyncioTestCase): + async def asyncSetUp(self): + self.now = 1000.0 + self.backend = MemoryCacheBackend(max_entries=2, clock=lambda: self.now) + self.entry = CacheEntry(body=b'{"result":true}', created_at=datetime.now(timezone.utc)) + + async def test_entry_expires_at_deadline_without_extending_on_reads(self): + await self.backend.set("server", self.entry, 60) + self.now += 59.5 + entry = await self.backend.get("server") + self.assertEqual(entry, self.entry) + + self.now += 0.5 + self.assertIsNone(await self.backend.get("server")) + self.assertEqual(self.backend._store, {}) + + async def test_capacity_evicts_least_recently_used_entry(self): + await self.backend.set("first", self.entry, 60) + await self.backend.set("second", self.entry, 60) + await self.backend.get("first") + await self.backend.set("third", self.entry, 60) + + self.assertIsNone(await self.backend.get("second")) + self.assertEqual(await self.backend.get("first"), self.entry) + self.assertEqual(await self.backend.get("third"), self.entry) + + async def test_writes_remove_expired_entries_before_evicting_live_entries(self): + await self.backend.set("expired", self.entry, 1) + await self.backend.set("live", self.entry, 60) + self.now += 1 + await self.backend.set("new", self.entry, 60) + + self.assertNotIn("expired", self.backend._store) + self.assertEqual(await self.backend.get("live"), self.entry) + self.assertEqual(len(self.backend._store), 2) + + async def test_instances_do_not_share_entries(self): + await self.backend.set("server", self.entry, 60) + other = MemoryCacheBackend() + self.assertIsNone(await other.get("server")) + + +class RedisCacheTests(unittest.IsolatedAsyncioTestCase): + async def asyncSetUp(self): + self.redis = MagicMock() + self.redis.set = AsyncMock() + self.redis.get = AsyncMock() + self.backend = RedisCacheBackend(self.redis) + self.entry = CacheEntry(body=b'{"result":true}', created_at=datetime.now(timezone.utc)) + + async def test_roundtrip_preserves_body_and_creation_time(self): + await self.backend.set("server", self.entry, 60) + self.redis.set.assert_awaited_once_with( + CACHE_PREFIX + "server", + self.entry.encode(), + ex=60, + ) + + encoded_entry = self.entry.encode() + self.redis.get.return_value = encoded_entry.encode("utf-8") + entry = await self.backend.get("server") + self.assertEqual(entry, self.entry) + self.redis.get.assert_awaited_once_with(CACHE_PREFIX + "server") + + async def test_missing_key_is_a_cache_miss(self): + self.redis.get.return_value = None + self.assertIsNone(await self.backend.get("server")) + + +@unittest.skipUnless(os.getenv("TEST_REDIS_URL"), "TEST_REDIS_URL is not configured") +class LiveRedisCacheTests(unittest.IsolatedAsyncioTestCase): + async def test_separate_clients_share_entry_metadata_and_expiration(self): + redis_url = os.environ["TEST_REDIS_URL"] + writer = aioredis.from_url(redis_url) + reader = aioredis.from_url(redis_url) + self.addAsyncCleanup(writer.close, close_connection_pool=True) + self.addAsyncCleanup(reader.close, close_connection_pool=True) + key_id = uuid.uuid4() + key = key_id.hex + self.addAsyncCleanup(writer.delete, CACHE_PREFIX + key) + entry = CacheEntry(body=b'{"result":true}', created_at=datetime.now(timezone.utc)) + write_backend = RedisCacheBackend(writer) + read_backend = RedisCacheBackend(reader) + + await write_backend.set(key, entry, 60) + cached = await read_backend.get(key) + self.assertEqual(cached, entry) + self.assertGreater(await writer.pttl(CACHE_PREFIX + key), 0) + + await writer.pexpire(CACHE_PREFIX + key, 0) + self.assertIsNone(await read_backend.get(key)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_cache_backend.py b/tests/test_cache_backend.py index 8ac091a..af6da29 100644 --- a/tests/test_cache_backend.py +++ b/tests/test_cache_backend.py @@ -3,25 +3,23 @@ from unittest.mock import AsyncMock, MagicMock, patch import httpx -from fastapi_cache import FastAPICache -from fastapi_cache.backends.inmemory import InMemoryBackend -from fastapi_cache.backends.redis import RedisBackend from mcstatus import JavaServer from mcstatus.status_response import JavaStatusResponse from app import main as api +from app.cache import MemoryCacheBackend, RedisCacheBackend class CacheBackendTests(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self): - FastAPICache.reset() - self.addCleanup(FastAPICache.reset) - memory_backend = InMemoryBackend() - await memory_backend.clear(namespace="mcstatus-api-cache") - self.addAsyncCleanup( - memory_backend.clear, - namespace="mcstatus-api-cache", + cache_patch = patch.object( + api.app.state, + "status_cache", + None, + create=True, ) + cache_patch.start() + self.addCleanup(cache_patch.stop) async def test_memory_caches_responses_without_creating_redis_client(self): status = JavaStatusResponse.build( @@ -42,7 +40,7 @@ async def test_memory_caches_responses_without_creating_redis_client(self): patch.object(api.JavaServer, "async_lookup", lookup), ): async with api.lifespan(api.app): - self.assertIsInstance(FastAPICache.get_backend(), InMemoryBackend) + self.assertIsInstance(api.app.state.status_cache.backend, MemoryCacheBackend) async with httpx.AsyncClient( transport=httpx.ASGITransport(app=api.app), base_url="http://test", @@ -54,8 +52,8 @@ async def test_memory_caches_responses_without_creating_redis_client(self): self.assertEqual(first.status_code, 200) self.assertTrue(first.json()["result"]) - self.assertEqual(first.headers["X-FastAPI-Cache"], "MISS") - self.assertEqual(cached.headers["X-FastAPI-Cache"], "HIT") + self.assertEqual(first.headers["X-Cache"], "MISS") + self.assertEqual(cached.headers["X-Cache"], "HIT") self.assertEqual(cached.json(), first.json()) lookup.assert_awaited_once() @@ -69,7 +67,6 @@ async def test_redis_default_and_explicit_selection_close_client(self): ] for environment, expected_url in configurations: with self.subTest(environment=environment): - FastAPICache.reset() redis = MagicMock() redis.close = AsyncMock() @@ -82,11 +79,11 @@ async def test_redis_default_and_explicit_selection_close_client(self): ) as factory, ): async with api.lifespan(api.app): - self.assertIsInstance(FastAPICache.get_backend(), RedisBackend) + self.assertIsInstance(api.app.state.status_cache.backend, RedisCacheBackend) factory.assert_called_once_with(expected_url) redis.close.assert_not_awaited() - redis.close.assert_awaited_once() + redis.close.assert_awaited_once_with(close_connection_pool=True) async def test_invalid_backend_fails_startup_without_creating_redis_client(self): with ( @@ -99,7 +96,7 @@ async def test_invalid_backend_fails_startup_without_creating_redis_client(self) factory.assert_not_called() - self.assertIsNone(FastAPICache._backend) + self.assertIsNone(api.app.state.status_cache) if __name__ == "__main__": diff --git a/tests/test_server_status.py b/tests/test_server_status.py index 54ef08d..7e8fefd 100644 --- a/tests/test_server_status.py +++ b/tests/test_server_status.py @@ -3,17 +3,17 @@ import logging import socket import unittest +from datetime import datetime from unittest.mock import AsyncMock, patch import httpx from dns.resolver import NXDOMAIN, NoNameservers from fastapi.encoders import jsonable_encoder -from fastapi_cache import FastAPICache -from fastapi_cache.backends.inmemory import InMemoryBackend from mcstatus import JavaServer from mcstatus.status_response import JavaStatusResponse from app import main as api +from app.cache import MemoryCacheBackend, StatusCache RAW_STATUS = { "version": {"name": "1.21.1", "protocol": 767}, @@ -23,16 +23,14 @@ ADDRESS = "example.com:25565" -class TestCache(InMemoryBackend): - """Advance time explicitly to verify expiration through the real cache decorator.""" +class TestCache(MemoryCacheBackend): + """Advance time explicitly to test expiration without waiting.""" def __init__(self): - self._store = {} - self._lock = asyncio.Lock() self.now = 1000 + super().__init__(clock=self.current_time) - @property - def _now(self): + def current_time(self): return self.now @@ -66,10 +64,15 @@ async def asyncSetUp(self): log_level = root_logger.level root_logger.setLevel(logging.WARNING) self.addCleanup(root_logger.setLevel, log_level) - FastAPICache.reset() - self.addCleanup(FastAPICache.reset) self.backend = TestCache() - FastAPICache.init(self.backend, prefix="test-cache") + cache_patch = patch.object( + api.app.state, + "status_cache", + StatusCache(self.backend), + create=True, + ) + cache_patch.start() + self.addCleanup(cache_patch.stop) self.status = JavaStatusResponse.build(RAW_STATUS, latency=12.75) self.server = JavaServer("127.0.0.1", 25565) self.server.async_status = AsyncMock(return_value=self.status) @@ -106,8 +109,9 @@ async def test_online_preserves_existing_fields_and_latency(self): async def test_unknown_latency_is_null_but_server_stays_online(self): for latency in (None, float("nan"), float("inf"), -1, "unknown", True): with self.subTest(latency=latency): + self.backend._store.clear() self.status.latency = latency - response = await self.request(headers={"Cache-Control": "no-store"}) + response = await self.request() self.assertEqual(response.status_code, 200) self.assertTrue(response.json()["result"]) self.assertIsNone(response.json()["status"]["latency"]) @@ -121,8 +125,9 @@ async def test_measured_zero_is_valid(self): async def test_connection_failure_and_timeout_are_offline(self): for error in (ConnectionRefusedError("refused"), TimeoutError("timed out"), NXDOMAIN()): with self.subTest(error=type(error).__name__): + self.backend._store.clear() self.lookup.side_effect = error - response = await self.request(headers={"Cache-Control": "no-store"}) + response = await self.request() self.assertEqual(response.status_code, 200) data = response.json() self.assertFalse(data["result"]) @@ -134,7 +139,7 @@ async def test_unknown_api_error_is_not_offline_or_cached(self): response = await self.request() self.assertEqual(response.status_code, 500) self.assertEqual(response.json()["detail"]["code"], "INTERNAL_ERROR") - self.assertEqual(response.headers["Cache-Control"], "no-store") + self.assertNotIn("Cache-Control", response.headers) self.assertNotIn("result", response.json()) self.assertNotIn("private", response.text) self.assertEqual(self.backend._store, {}) @@ -150,7 +155,7 @@ async def test_query_errors_are_not_offline_or_cached(self): response = await self.request() self.assertEqual(response.status_code, 502) self.assertEqual(response.json()["detail"]["code"], "UPSTREAM_ERROR") - self.assertEqual(response.headers["Cache-Control"], "no-store") + self.assertNotIn("Cache-Control", response.headers) self.assertNotIn("result", response.json()) self.assertEqual(self.backend._store, {}) @@ -166,19 +171,21 @@ async def test_invalid_address_is_bad_request(self): async def test_cache_reuses_latency_timestamp_and_expires(self): first = await self.request() - self.assertEqual(first.headers["X-FastAPI-Cache"], "MISS") - self.assertEqual(first.headers["Cache-Control"], "max-age=60") + self.assertEqual(first.headers["X-Cache"], "MISS") + created_at = first.headers["X-Cache-Created-At"] + self.assertIsNotNone(datetime.fromisoformat(created_at).tzinfo) self.status.latency = 99 self.backend.now += 59 cached = await self.request() - self.assertEqual(cached.headers["X-FastAPI-Cache"], "HIT") - self.assertEqual(cached.headers["Cache-Control"], "max-age=1") + self.assertEqual(cached.headers["X-Cache"], "HIT") + self.assertEqual(cached.headers["X-Cache-Created-At"], created_at) self.assertEqual(cached.json(), first.json()) self.lookup.assert_awaited_once() self.backend.now += 2 refreshed = await self.request() - self.assertEqual(refreshed.headers["X-FastAPI-Cache"], "MISS") + self.assertEqual(refreshed.headers["X-Cache"], "MISS") + self.assertNotEqual(refreshed.headers["X-Cache-Created-At"], created_at) self.assertEqual(refreshed.json()["status"]["latency"], 99) self.assertNotEqual(refreshed.json()["timestamp"], first.json()["timestamp"]) self.assertEqual(self.lookup.await_count, 2) @@ -189,7 +196,7 @@ async def test_offline_is_cached_then_online_after_expiry(self): self.lookup.side_effect = None cached = await self.request() self.assertEqual(cached.json(), offline.json()) - self.assertEqual(cached.headers["X-FastAPI-Cache"], "HIT") + self.assertEqual(cached.headers["X-Cache"], "HIT") self.lookup.assert_awaited_once() self.backend.now += 61 @@ -204,30 +211,62 @@ async def test_null_latency_survives_cache_roundtrip(self): self.assertIsNone(cached.json()["status"]["latency"]) self.lookup.assert_awaited_once() - async def test_conditional_cache_hit_returns_304(self): - first = await self.request() - cached = await self.request(headers={"If-None-Match": first.headers["ETag"]}) - self.assertEqual(cached.status_code, 304) - self.assertEqual(cached.content, b"") - self.lookup.assert_awaited_once() + async def test_request_cache_headers_do_not_change_server_cache_policy(self): + for directive in ("no-cache", "no-store", "max-age=0, No-Cache, No-Store"): + with self.subTest(directive=directive): + self.backend._store.clear() + self.lookup.reset_mock() + self.status.latency = 12.75 + headers = { + "Cache-Control": directive, + "Pragma": "no-cache", + "If-None-Match": "*", + "If-Modified-Since": "Wed, 01 Jan 2020 00:00:00 GMT", + } + first = await self.request(headers=headers) + self.assertEqual(first.status_code, 200) + self.assertEqual(first.headers["X-Cache"], "MISS") + self.status.latency = 99 + cached = await self.request(headers=headers) + + self.assertEqual(cached.status_code, 200) + self.assertEqual(cached.headers["X-Cache"], "HIT") + self.assertEqual(cached.json(), first.json()) + self.assertEqual( + cached.headers["X-Cache-Created-At"], + first.headers["X-Cache-Created-At"], + ) + self.lookup.assert_awaited_once() + + for response in (first, cached): + self.assertEqual( + set(response.headers), + {"content-type", "content-length", "x-cache", "x-cache-created-at"}, + ) async def test_redis_read_failure_falls_back_to_actual_probe(self): - with patch.object(self.backend, "get_with_ttl", AsyncMock(side_effect=ConnectionError())): - with self.assertLogs("fastapi_cache.decorator", level="WARNING"): + with patch.object(self.backend, "get", AsyncMock(side_effect=ConnectionError())): + with self.assertLogs("app.cache", level="WARNING"): response = await self.request() self.assertEqual(response.status_code, 200) self.assertTrue(response.json()["result"]) + self.assertEqual(response.headers["X-Cache"], "BYPASS") + self.assertNotIn("Cache-Control", response.headers) + self.assertNotIn("X-Cache-Created-At", response.headers) self.lookup.assert_awaited_once() async def test_redis_write_failure_keeps_actual_result(self): with patch.object(self.backend, "set", AsyncMock(side_effect=ConnectionError())): - with self.assertLogs("fastapi_cache.decorator", level="WARNING"): + with self.assertLogs("app.cache", level="WARNING"): response = await self.request() self.assertEqual(response.status_code, 200) self.assertTrue(response.json()["result"]) self.assertEqual(response.json()["status"]["latency"], 12.75) + self.assertEqual(response.headers["X-Cache"], "BYPASS") + self.assertNotIn("Cache-Control", response.headers) + self.assertNotIn("X-Cache-Created-At", response.headers) async def test_dns_is_included_in_total_timeout(self): async def stalled_lookup(*args, **kwargs): @@ -262,7 +301,7 @@ async def test_system_dns_name_absence_is_offline_and_cached(self): self.assertFalse(response.json()["result"]) self.assertIsNone(response.json()["status"]) self.assertEqual(cached.json(), response.json()) - self.assertEqual(cached.headers["X-FastAPI-Cache"], "HIT") + self.assertEqual(cached.headers["X-Cache"], "HIT") connection.assert_awaited_once() async def test_temporary_system_dns_failure_is_not_offline_or_cached(self): @@ -275,7 +314,7 @@ async def test_temporary_system_dns_failure_is_not_offline_or_cached(self): self.assertEqual(response.status_code, 502) self.assertEqual(response.json()["detail"]["code"], "UPSTREAM_ERROR") - self.assertEqual(response.headers["Cache-Control"], "no-store") + self.assertNotIn("Cache-Control", response.headers) self.assertEqual(self.backend._store, {}) async def request_real_status(self, raw): @@ -322,7 +361,7 @@ async def test_real_tcp_eof_is_offline_and_cached(self): cached = await self.request() self.assertEqual(cached.json(), response.json()) - self.assertEqual(cached.headers["X-FastAPI-Cache"], "HIT") + self.assertEqual(cached.headers["X-Cache"], "HIT") self.lookup.assert_awaited_once() async def test_real_malformed_status_is_upstream_error_and_not_cached(self): @@ -336,7 +375,7 @@ async def test_real_malformed_status_is_upstream_error_and_not_cached(self): response = await self.request_real_status(raw) self.assertEqual(response.status_code, 502) self.assertEqual(response.json()["detail"]["code"], "UPSTREAM_ERROR") - self.assertEqual(response.headers["Cache-Control"], "no-store") + self.assertNotIn("Cache-Control", response.headers) self.assertEqual(self.backend._store, {}) async def test_real_minecraft_status_exchange_measures_latency_without_ping(self): diff --git a/tests/test_singleflight.py b/tests/test_singleflight.py new file mode 100644 index 0000000..a28de1f --- /dev/null +++ b/tests/test_singleflight.py @@ -0,0 +1,159 @@ +import asyncio +import unittest +from unittest.mock import AsyncMock + +from app.cache import MemoryCacheBackend, StatusCache + + +class SingleflightTests(unittest.IsolatedAsyncioTestCase): + async def asyncSetUp(self): + self.backend = MemoryCacheBackend() + self.cache = StatusCache(self.backend) + self.started = asyncio.Event() + self.release = asyncio.Event() + self.query = AsyncMock(side_effect=self.query_server) + self.requests = [] + self.addAsyncCleanup(self.cache.close) + self.addAsyncCleanup(self.cancel_requests) + + async def query_server(self, address): + self.started.set() + await self.release.wait() + return {"result": True, "address": address} + + def request(self, address="example.com"): + task = asyncio.create_task(self.cache.respond(address, self.query)) + self.requests.append(task) + return task + + async def cancel_requests(self): + for task in self.requests: + task.cancel() + await asyncio.gather(*self.requests, return_exceptions=True) + + async def test_same_address_shares_lookup_and_cache_write(self): + self.backend.set = AsyncMock(wraps=self.backend.set) + leader = self.request() + await asyncio.wait_for(self.started.wait(), timeout=1) + followers = [self.request() for _ in range(10)] + await asyncio.sleep(0) + self.release.set() + responses = await asyncio.gather(leader, *followers) + + self.query.assert_awaited_once_with("example.com") + self.backend.set.assert_awaited_once() + self.assertEqual(self.cache._inflight, {}) + first = responses[0] + for response in responses: + self.assertEqual(response.body, first.body) + self.assertEqual(response.headers["X-Cache"], "MISS") + self.assertEqual( + response.headers["X-Cache-Created-At"], + first.headers["X-Cache-Created-At"], + ) + + cached = await self.cache.respond("example.com", self.query) + self.assertEqual(cached.headers["X-Cache"], "HIT") + self.query.assert_awaited_once() + + async def test_different_addresses_query_in_parallel(self): + both_started = asyncio.Event() + addresses = set() + + async def query(address): + addresses.add(address) + if len(addresses) == 2: + both_started.set() + await self.release.wait() + return {"address": address} + + self.query.side_effect = query + first = self.request("first.example.com") + second = self.request("second.example.com") + await asyncio.wait_for(both_started.wait(), timeout=1) + self.release.set() + await asyncio.gather(first, second) + self.assertEqual(self.query.await_count, 2) + + async def test_canceling_one_request_does_not_cancel_shared_lookup(self): + leader = self.request() + await asyncio.wait_for(self.started.wait(), timeout=1) + follower = self.request() + await asyncio.sleep(0) + leader.cancel() + with self.assertRaises(asyncio.CancelledError): + await leader + + self.assertFalse(follower.done()) + self.release.set() + response = await asyncio.wait_for(follower, timeout=1) + self.assertEqual(response.status_code, 200) + self.query.assert_awaited_once() + self.assertEqual(self.cache._inflight, {}) + + async def test_shared_failure_is_not_cached_and_next_request_retries(self): + async def fail(address): + await self.query_server(address) + raise RuntimeError("query failed") + + self.query.side_effect = fail + first = self.request() + await asyncio.wait_for(self.started.wait(), timeout=1) + second = self.request() + await asyncio.sleep(0) + self.release.set() + errors = await asyncio.gather(first, second, return_exceptions=True) + self.assertTrue(all(isinstance(error, RuntimeError) for error in errors)) + self.query.assert_awaited_once() + self.assertEqual(self.cache._inflight, {}) + self.assertEqual(self.backend._store, {}) + + self.query.side_effect = self.query_server + recovered = await self.cache.respond("example.com", self.query) + self.assertEqual(recovered.status_code, 200) + self.assertEqual(self.query.await_count, 2) + + async def test_shared_lookup_is_released_after_all_waiters_cancel(self): + leader = self.request() + await asyncio.wait_for(self.started.wait(), timeout=1) + shared_task = next(iter(self.cache._inflight.values())) + leader.cancel() + with self.assertRaises(asyncio.CancelledError): + await leader + + self.assertFalse(shared_task.cancelled()) + self.release.set() + await asyncio.wait_for(asyncio.shield(shared_task), timeout=1) + self.assertEqual(self.cache._inflight, {}) + cached = await self.cache.respond("example.com", self.query) + self.assertEqual(cached.headers["X-Cache"], "HIT") + self.query.assert_awaited_once() + + async def test_shutdown_cancels_and_releases_pending_lookups(self): + request = self.request() + await asyncio.wait_for(self.started.wait(), timeout=1) + await self.cache.close() + with self.assertRaises(asyncio.CancelledError): + await request + self.assertEqual(self.cache._inflight, {}) + self.assertEqual(self.backend._store, {}) + + async def test_cache_outage_still_shares_lookup(self): + self.backend.get = AsyncMock(side_effect=ConnectionError("cache unavailable")) + with self.assertLogs("app.cache", level="WARNING"): + first = self.request() + await asyncio.wait_for(self.started.wait(), timeout=1) + second = self.request() + await asyncio.sleep(0) + self.release.set() + responses = await asyncio.gather(first, second) + + self.query.assert_awaited_once() + for response in responses: + self.assertEqual(response.headers["X-Cache"], "BYPASS") + self.assertNotIn("X-Cache-Created-At", response.headers) + self.assertEqual(self.cache._inflight, {}) + + +if __name__ == "__main__": + unittest.main()