initial commit before level 3
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""Connection pool. The URL is never written to the log."""
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
|
||||
import asyncpg
|
||||
|
||||
logger = logging.getLogger('messenger')
|
||||
|
||||
_ATTEMPTS = 40
|
||||
|
||||
|
||||
def _pool_size(name: str, default: int) -> int:
|
||||
raw = os.getenv(name, '').strip()
|
||||
return int(raw) if raw else default
|
||||
|
||||
|
||||
async def open_pool(database_url: str) -> asyncpg.Pool:
|
||||
delay = 0.5
|
||||
last_type = 'error'
|
||||
min_size = _pool_size('DB_POOL_MIN', 1)
|
||||
max_size = max(min_size, _pool_size('DB_POOL_MAX', 10))
|
||||
for attempt in range(1, _ATTEMPTS + 1):
|
||||
pool = None
|
||||
try:
|
||||
pool = await asyncpg.create_pool(
|
||||
database_url,
|
||||
min_size=min_size,
|
||||
max_size=max_size,
|
||||
timeout=5,
|
||||
# Uploads are capped at 10 MiB; give that write room past the old 5 s JSON timeout.
|
||||
command_timeout=30,
|
||||
server_settings={'statement_timeout': '30000'},
|
||||
)
|
||||
async with pool.acquire() as conn:
|
||||
await conn.execute('SELECT 1')
|
||||
return pool
|
||||
except Exception as exc:
|
||||
last_type = type(exc).__name__
|
||||
if pool is not None:
|
||||
await pool.close()
|
||||
if attempt == _ATTEMPTS:
|
||||
break
|
||||
logger.warning('database is not ready (%s), retrying', last_type)
|
||||
await asyncio.sleep(delay)
|
||||
delay = min(delay * 1.5, 2)
|
||||
raise RuntimeError('database did not become reachable (%s)' % last_type)
|
||||
Reference in New Issue
Block a user