forked from mtusi2026/mtusi2026lab3
Answer readiness within the balancer check timeout
The readiness ping waited up to one second for a pooled connection and one more second for SELECT 1, while HAProxy gives a check one second. With a busy pool the balancer saw a check timeout instead of an explicit 503. The ping now waits 0.4 s for a connection and 0.4 s for the query, so the worst answer, 0.8 s, still arrives inside the check. Other queries keep the pool acquire and command timeouts. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1ae4b770ae
commit
6e4c387e50
@@ -16,6 +16,8 @@ FAILURES = (
|
|||||||
asyncpg.exceptions.LockNotAvailableError,
|
asyncpg.exceptions.LockNotAvailableError,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
PING_TIMEOUT_SECONDS = 0.4
|
||||||
|
|
||||||
type Connection = asyncpg.Connection
|
type Connection = asyncpg.Connection
|
||||||
|
|
||||||
|
|
||||||
@@ -57,16 +59,16 @@ class Database:
|
|||||||
await self._pool.close()
|
await self._pool.close()
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def connection(self) -> AsyncIterator[Connection]:
|
async def connection(self, wait: float | None = None) -> AsyncIterator[Connection]:
|
||||||
try:
|
try:
|
||||||
async with self._pool.acquire(timeout=self._acquire_timeout) as connection:
|
async with self._pool.acquire(timeout=wait or self._acquire_timeout) as connection:
|
||||||
yield connection
|
yield connection
|
||||||
except FAILURES as error:
|
except FAILURES as error:
|
||||||
raise Unavailable("PostgreSQL недоступен") from error
|
raise Unavailable("PostgreSQL недоступен") from error
|
||||||
|
|
||||||
async def ping(self) -> None:
|
async def ping(self) -> None:
|
||||||
async with self.connection() as connection:
|
async with self.connection(PING_TIMEOUT_SECONDS) as connection:
|
||||||
await connection.fetchval("SELECT 1", timeout=1)
|
await connection.fetchval("SELECT 1", timeout=PING_TIMEOUT_SECONDS)
|
||||||
|
|
||||||
|
|
||||||
class PostgresUnitOfWork:
|
class PostgresUnitOfWork:
|
||||||
|
|||||||
Reference in New Issue
Block a user