Files
mtusi2026lab6/app/config.py
T

181 lines
6.8 KiB
Python

from dataclasses import dataclass
from pathlib import Path
import os
import socket
def read_value(name: str, default: str | None = None) -> str:
file_name = os.getenv(f"{name}_FILE")
if file_name:
return Path(file_name).read_text(encoding="utf-8").strip()
value = os.getenv(name, default)
if value is None:
raise RuntimeError(f"{name} is required.")
return value
def read_positive_int(name: str, default: str) -> int:
value = int(os.getenv(name, default))
if value < 1:
raise RuntimeError(f"{name} must be positive.")
return value
def read_positive_float(name: str, default: str) -> float:
value = float(os.getenv(name, default))
if value <= 0:
raise RuntimeError(f"{name} must be positive.")
return value
@dataclass(frozen=True)
class Settings:
app_host: str
app_port: int
database_url: str
session_ttl_seconds: int
# PostgreSQL connection pool and timeouts.
db_pool_size: int
db_max_overflow: int
db_pool_timeout_seconds: float
db_connect_timeout_seconds: float
db_statement_timeout_ms: int
# Valkey is a session cache; PostgreSQL stays the source of truth.
cache_url: str | None
session_cache_ttl_seconds: int
cache_timeout_seconds: float
cache_max_connections: int
cache_retry_after_seconds: float
# Lab3: X-Instance-Id is the container hostname (short Docker ID), stable
# for the process lifetime; scripts/stop-one.sh maps it to a container.
instance_id: str
idempotency_ttl_seconds: int
# Graceful shutdown: readiness answers 503 for shutdown_drain_seconds so
# the LB drops this replica, then uvicorn closes the listener and waits
# for in-flight requests up to shutdown_timeout_seconds.
shutdown_drain_seconds: float
shutdown_timeout_seconds: int
# Lab4: attachments. Metadata lives in PostgreSQL, bytes in a private
# S3 bucket; the API uses its own restricted credentials (not root).
s3_endpoint: str
s3_region: str
s3_bucket: str
s3_access_key_id: str
s3_secret_access_key: str
s3_object_prefix: str
s3_connect_timeout_seconds: float
s3_read_timeout_seconds: float
# Deadline of one whole S3 call (DNS + retries included), below the LB
# "timeout server" (10 s): a stopped S3 container makes Docker DNS hang
# ~8 s per lookup, which connect_timeout does not cover.
s3_operation_timeout_seconds: float
# PUT gets operation timeout + size / this rate: 10 MiB under load is
# slower than a HEAD. HAProxy allows 40 s for an upload POST.
s3_put_min_bytes_per_second: int
max_upload_bytes: int
max_image_pixels: int
# One upload may not take unlimited RAM/time: the body is spooled to a
# temporary file (only upload_spool_memory_bytes stay in RAM), receiving
# it is limited by upload_timeout_seconds, and at most
# max_concurrent_uploads uploads per replica run at once.
upload_spool_memory_bytes: int
upload_timeout_seconds: float
max_concurrent_uploads: int
upload_slot_wait_seconds: float
# Janitor: an upload intent older than upload_grace_seconds means the
# request died between S3 PUT and COMMIT; its object is deleted.
upload_grace_seconds: int
janitor_interval_seconds: float
# Experiments only (see REPORT.md): "" | crash_after_s3_put | db_error_after_s3_put
fault_injection: str
def load_settings() -> Settings:
session_store = os.getenv("SESSION_STORE", "postgres").lower()
if session_store not in {"postgres", "valkey", "redis"}:
raise RuntimeError("SESSION_STORE must be postgres, valkey or redis.")
cache_url = None
if session_store != "postgres":
cache_url = read_value("CACHE_URL")
return Settings(
app_host=os.getenv("APP_HOST", "0.0.0.0"),
app_port=int(os.getenv("APP_PORT", "8080")),
database_url=read_value("DATABASE_URL"),
session_ttl_seconds=read_positive_int("SESSION_TTL_SECONDS", "3600"),
db_pool_size=read_positive_int("DB_POOL_SIZE", "10"),
db_max_overflow=int(os.getenv("DB_MAX_OVERFLOW", "5")),
db_pool_timeout_seconds=read_positive_float("DB_POOL_TIMEOUT_SECONDS", "3"),
db_connect_timeout_seconds=read_positive_float(
"DB_CONNECT_TIMEOUT_SECONDS", "3"
),
db_statement_timeout_ms=read_positive_int("DB_STATEMENT_TIMEOUT_MS", "5000"),
cache_url=cache_url,
session_cache_ttl_seconds=read_positive_int(
"SESSION_CACHE_TTL_SECONDS", "60"
),
cache_timeout_seconds=read_positive_float("CACHE_TIMEOUT_SECONDS", "0.25"),
cache_max_connections=read_positive_int("CACHE_MAX_CONNECTIONS", "20"),
cache_retry_after_seconds=read_positive_float(
"CACHE_RETRY_AFTER_SECONDS", "5"
),
instance_id=os.getenv("INSTANCE_ID") or socket.gethostname(),
idempotency_ttl_seconds=read_positive_int(
"IDEMPOTENCY_TTL_SECONDS", "86400"
),
shutdown_drain_seconds=read_positive_float(
"SHUTDOWN_DRAIN_SECONDS", "3"
),
shutdown_timeout_seconds=read_positive_int(
"SHUTDOWN_TIMEOUT_SECONDS", "8"
),
s3_endpoint=read_value("S3_ENDPOINT", "http://s3:9000"),
s3_region=os.getenv("S3_REGION", "us-east-1"),
s3_bucket=read_value("S3_BUCKET", "messenger-attachments"),
s3_access_key_id=read_value("S3_ACCESS_KEY_ID"),
s3_secret_access_key=read_value("S3_SECRET_ACCESS_KEY"),
s3_object_prefix=os.getenv("S3_OBJECT_PREFIX", "originals/"),
s3_connect_timeout_seconds=read_positive_float(
"S3_CONNECT_TIMEOUT_SECONDS", "2"
),
s3_read_timeout_seconds=read_positive_float(
"S3_READ_TIMEOUT_SECONDS", "5"
),
s3_operation_timeout_seconds=read_positive_float(
"S3_OPERATION_TIMEOUT_SECONDS", "6"
),
s3_put_min_bytes_per_second=read_positive_int(
"S3_PUT_MIN_BYTES_PER_SECOND", "1048576"
),
max_upload_bytes=read_positive_int("MAX_UPLOAD_BYTES", "10485760"),
max_image_pixels=read_positive_int("MAX_IMAGE_PIXELS", "40000000"),
upload_spool_memory_bytes=read_positive_int(
"UPLOAD_SPOOL_MEMORY_BYTES", "1048576"
),
upload_timeout_seconds=read_positive_float(
"UPLOAD_TIMEOUT_SECONDS", "60"
),
max_concurrent_uploads=read_positive_int("MAX_CONCURRENT_UPLOADS", "8"),
upload_slot_wait_seconds=read_positive_float(
"UPLOAD_SLOT_WAIT_SECONDS", "10"
),
upload_grace_seconds=read_positive_int("UPLOAD_GRACE_SECONDS", "900"),
janitor_interval_seconds=read_positive_float(
"JANITOR_INTERVAL_SECONDS", "60"
),
fault_injection=os.getenv("FAULT_INJECTION", ""),
)
settings = load_settings()