Добавить PostgreSQL, постоянные сессии, кэш и проверки ЛР2

This commit is contained in:
2026-10-30 17:24:20 +03:00
parent 65003b6119
commit a39b4fea27
29 changed files with 2776 additions and 20 deletions
+62
View File
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
"""Probe the real bounded pool in an isolated process against the teaching database."""
import argparse
import json
import subprocess
from datetime import UTC, datetime
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
PROBE = """
import json, threading, time
from concurrent.futures import ThreadPoolExecutor
from messenger.postgres import PostgresStore
from messenger.domain import Failure
store=PostgresStore()
barrier=threading.Barrier(50)
def work(i):
barrier.wait()
start=time.monotonic()
try:
with store.connection() as conn:
conn.execute("SELECT pg_sleep(2.2)")
return {"status":200,"seconds":time.monotonic()-start}
except Failure as e:
return {"status":e.status,"seconds":time.monotonic()-start}
with ThreadPoolExecutor(max_workers=50) as executor:
rows=list(executor.map(work,range(50)))
store.close()
assert any(r["status"]==200 for r in rows)
assert any(r["status"]==503 for r in rows)
assert max(r["seconds"] for r in rows)<5
print(json.dumps({"passed":True,"clients":50,"max_pool_size":8,"max_waiting":32,
"admitted":sum(r["status"]==200 for r in rows),"rejected_503":sum(r["status"]==503 for r in rows),
"max_seconds":max(r["seconds"] for r in rows),"rows":rows}))
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output", default="evidence/pool.json")
args = parser.parse_args()
path = ROOT / args.output
if path.exists():
raise SystemExit("Choose a new evidence filename")
process = subprocess.run(
["docker", "compose", "exec", "-T", "api", "python", "-c", PROBE],
cwd=ROOT,
capture_output=True,
text=True,
timeout=25,
)
if process.returncode:
raise SystemExit("Pool probe failed; inspect source and dependency readiness")
result = json.loads(process.stdout)
result["date_utc"] = datetime.now(UTC).isoformat()
path.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
print("PASS: real bounded connection pool overload returns 503")
if __name__ == "__main__":
main()