diff --git a/src/messenger/app.py b/src/messenger/app.py index fa5fb8a..b339b34 100644 --- a/src/messenger/app.py +++ b/src/messenger/app.py @@ -652,6 +652,8 @@ def create_app(settings: Settings | None = None) -> Starlette: await db.open() await storage.open() await staging.open() + if hasattr(sessions, "warm"): + await sessions.warm() # lab 7: TLS connections to Valkey opened before readiness log.info("dependencies_configured", extra={"fields": { "session_store": sessions.name, "session_ttl_s": settings.session_ttl_seconds, "db_pool_max": settings.db_pool_max, "db_timeout_s": settings.db_timeout_seconds, diff --git a/src/messenger/config.py b/src/messenger/config.py index 10e471a..46de462 100644 --- a/src/messenger/config.py +++ b/src/messenger/config.py @@ -137,6 +137,7 @@ class Settings: db_pool_max: int = 10 db_timeout_seconds: float = 3.0 cache_timeout_seconds: float = 0.5 + cache_pool_size: int = 8 hash_concurrency: int = 2 instance_id: str = "local" drain_seconds: float = 0.0 @@ -240,6 +241,7 @@ class Settings: db_pool_max=db_pool_max, db_timeout_seconds=_float("DB_TIMEOUT_SECONDS", 3.0, 0.5, 30.0), cache_timeout_seconds=_float("CACHE_TIMEOUT_SECONDS", 0.5, 0.05, 10.0), + cache_pool_size=_int("CACHE_POOL_SIZE", 8, 1, 200), hash_concurrency=_int("PASSWORD_HASH_CONCURRENCY", 2, 1, 16), instance_id=_instance_id(), # After SIGTERM: readiness is 503 but requests are still served for this long, diff --git a/src/messenger/sessions.py b/src/messenger/sessions.py index 4804789..a3ac8ed 100644 --- a/src/messenger/sessions.py +++ b/src/messenger/sessions.py @@ -27,7 +27,7 @@ import secrets from typing import Protocol from redis import exceptions as redis_errors -from redis.asyncio import Redis +from redis.asyncio import BlockingConnectionPool, Redis from redis.backoff import NoBackoff from redis.retry import Retry @@ -113,14 +113,23 @@ class ValkeySessionStore: # Lab 7: TLS to Valkey, server verified by the internal CA AND hostname (SAN valkey). tls = {"ssl_ca_certs": settings.tls_ca_file or None, "ssl_cert_reqs": "required", "ssl_check_hostname": True, "ssl_min_version": ssl.TLSVersion.TLSv1_2} - self.client = Redis.from_url( + # Lab 7: a BOUNDED pool, warmed at start (warm()). With TLS every new connection costs a + # handshake; a burst of 24 parallel requests on a fresh replica opened 24 connections at once + # and some handshakes missed CACHE_TIMEOUT_SECONDS (503, test_concurrent_same_key...). Now at + # most CACHE_POOL_SIZE connections exist, they are opened before the replica is ready, and a + # burst waits (<= the same timeout) for a free one instead of opening new ones. + self.pool_size = settings.cache_pool_size + pool = BlockingConnectionPool.from_url( settings.cache_url, + max_connections=self.pool_size, + timeout=self.timeout, # wait for a free connection, then 503 socket_timeout=self.timeout, socket_connect_timeout=self.timeout, - retry=Retry(NoBackoff(), 0), # no hidden retries: a failure is reported at once decode_responses=False, **tls, ) + self.client = Redis(connection_pool=pool, + retry=Retry(NoBackoff(), 0)) # no hidden retries: a failure is reported at once def _key(self, token: str) -> str: return self.PREFIX + token_hash(token).hex() @@ -159,6 +168,12 @@ class ValkeySessionStore: except Unavailable: return False + async def warm(self) -> None: + """Open the whole pool now (parallel PINGs take one connection each): the TLS handshakes + happen at start-up, before the replica reports ready, not in the first burst of requests.""" + results = await asyncio.gather(*(self.ping() for _ in range(self.pool_size))) + log.info("session_store_warmed", extra={"fields": {"connections": self.pool_size, "ok": sum(results)}}) + async def close(self) -> None: await self.client.aclose()