HAProxy + 2 API-реплики, общие PostgreSQL/Valkey, обязательный Idempotency-Key, graceful drain по SIGTERM, stop-one/restart/rolling-update, серии отказов и REPORT.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
"""Настройки из переменных окружения. Секреты читаются только из файлов (*_FILE)."""
|
|
import os
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
|
|
def _int(name: str, default: int, low: int, high: int) -> int:
|
|
value = int(os.environ.get(name, default))
|
|
if not low <= value <= high:
|
|
raise ValueError(f'{name} must be in [{low}, {high}]')
|
|
return value
|
|
|
|
|
|
def _secret(name: str) -> str | None:
|
|
"""Значение из файла, путь к которому лежит в переменной NAME_FILE."""
|
|
path = os.environ.get(name + '_FILE')
|
|
if not path:
|
|
return None
|
|
return Path(path).read_text(encoding='utf-8').strip()
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Settings:
|
|
host: str
|
|
port: int
|
|
session_ttl: int
|
|
session_store: str
|
|
session_cache_ttl: int
|
|
database_url: str
|
|
cache_url: str | None
|
|
db_pool_max: int
|
|
db_statement_timeout_ms: int
|
|
db_acquire_timeout: float
|
|
cache_timeout: float
|
|
hash_workers: int
|
|
idempotency_ttl: int
|
|
drain_seconds: float
|
|
|
|
|
|
def load() -> Settings:
|
|
database_url = _secret('DATABASE_URL') or os.environ.get('DATABASE_URL')
|
|
if not database_url:
|
|
raise RuntimeError('DATABASE_URL_FILE is not configured')
|
|
store = os.environ.get('SESSION_STORE', 'postgres')
|
|
if store not in ('postgres', 'postgres+valkey'):
|
|
raise ValueError('SESSION_STORE must be postgres or postgres+valkey')
|
|
cache_url = _secret('CACHE_URL') if store == 'postgres+valkey' else None
|
|
return Settings(
|
|
host=os.environ.get('APP_HOST', '0.0.0.0'),
|
|
port=_int('APP_PORT', 8080, 1, 65535),
|
|
session_ttl=_int('SESSION_TTL_SECONDS', 3600, 1, 30 * 24 * 3600),
|
|
session_store=store,
|
|
session_cache_ttl=_int('SESSION_CACHE_TTL_SECONDS', 60, 1, 3600),
|
|
database_url=database_url,
|
|
cache_url=cache_url,
|
|
db_pool_max=_int('DB_POOL_MAX', 10, 1, 100),
|
|
db_statement_timeout_ms=_int('DB_STATEMENT_TIMEOUT_MS', 5000, 100, 600000),
|
|
db_acquire_timeout=_int('DB_ACQUIRE_TIMEOUT_MS', 2000, 100, 60000) / 1000,
|
|
cache_timeout=_int('CACHE_TIMEOUT_MS', 300, 10, 10000) / 1000,
|
|
hash_workers=_int('PASSWORD_HASH_WORKERS', 4, 1, 32),
|
|
idempotency_ttl=_int('IDEMPOTENCY_TTL_SECONDS', 86400, 86400, 30 * 24 * 3600), # контракт: не менее 24 ч
|
|
drain_seconds=_int('SHUTDOWN_DRAIN_MS', 3000, 0, 60000) / 1000,
|
|
)
|