forked from mtusi2026/mtusi2026lab7
Проверять соединение PostgreSQL перед рабочей транзакцией
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"date": "2026-11-27T09:30:53.303280+00:00",
|
||||||
|
"runtime_source": "0076659224dd21792bd2eb7e5c1a44ce92b4de82",
|
||||||
|
"scope": "Isolated diagnostic comparison, not a full clean verification. Checked variant adds only ConnectionPool.check_connection at construction inside this probe process.",
|
||||||
|
"intervention": "PostgreSQL terminates only two fresh probe sessions selected by unique application_name; business sessions and jobs unchanged",
|
||||||
|
"results": {
|
||||||
|
"baseline": {
|
||||||
|
"query_success": false,
|
||||||
|
"error_type": "DomainError",
|
||||||
|
"seconds": 0.0014,
|
||||||
|
"connections_lost": 0
|
||||||
|
},
|
||||||
|
"checked": {
|
||||||
|
"query_success": true,
|
||||||
|
"error_type": null,
|
||||||
|
"seconds": 0.0091,
|
||||||
|
"connections_lost": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"complete": true
|
||||||
|
}
|
||||||
@@ -39,6 +39,68 @@ def snapshot():
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def closed_peer_checkout():
|
||||||
|
"""A server-closed idle socket is replaced before business SQL begins."""
|
||||||
|
name = "checkout_probe_" + uuid4().hex
|
||||||
|
code = """
|
||||||
|
import json,sys,time
|
||||||
|
from src.pipeline import database
|
||||||
|
db=database()
|
||||||
|
try:
|
||||||
|
with db.connection() as conn:
|
||||||
|
conn.execute("SELECT set_config('application_name',%s,false)",(sys.argv[1],))
|
||||||
|
pid=conn.execute("SELECT pg_backend_pid() pid").fetchone()["pid"]
|
||||||
|
print(json.dumps({"pid":pid}),flush=True)
|
||||||
|
sys.stdin.readline()
|
||||||
|
start=time.monotonic()
|
||||||
|
with db.connection() as conn:
|
||||||
|
assert conn.execute("SELECT 1 n").fetchone()["n"]==1
|
||||||
|
print(json.dumps({"query_success":True,"seconds":time.monotonic()-start,
|
||||||
|
"connections_lost":db.pool.get_stats().get("connections_lost",0)}),flush=True)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
"""
|
||||||
|
process = subprocess.Popen(
|
||||||
|
[
|
||||||
|
"docker",
|
||||||
|
"compose",
|
||||||
|
"exec",
|
||||||
|
"-T",
|
||||||
|
"worker",
|
||||||
|
"python",
|
||||||
|
"-c",
|
||||||
|
code,
|
||||||
|
name,
|
||||||
|
],
|
||||||
|
cwd=ROOT,
|
||||||
|
stdin=subprocess.PIPE,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
# Database.open has its own finite startup bound. No session credentials
|
||||||
|
# or diagnostic exception messages are copied into evidence.
|
||||||
|
pid = int(json.loads(process.stdout.readline())["pid"])
|
||||||
|
assert (
|
||||||
|
sql(
|
||||||
|
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity "
|
||||||
|
f"WHERE pid={pid} AND application_name='{name}'"
|
||||||
|
)
|
||||||
|
== "t"
|
||||||
|
)
|
||||||
|
output, _ = process.communicate(input="continue\n", timeout=15)
|
||||||
|
assert process.returncode == 0, "Closed-peer checkout probe failed"
|
||||||
|
result = json.loads(output)
|
||||||
|
assert result["query_success"] and result["connections_lost"] >= 1
|
||||||
|
assert result["seconds"] < 8
|
||||||
|
return result
|
||||||
|
finally:
|
||||||
|
if process.poll() is None:
|
||||||
|
process.kill()
|
||||||
|
process.wait(timeout=5)
|
||||||
|
|
||||||
|
|
||||||
def exercise(client, session, chat, action, *, seconds=15):
|
def exercise(client, session, chat, action, *, seconds=15):
|
||||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||||
observations, confirmed, errors, reads = [], {}, 0, 0
|
observations, confirmed, errors, reads = [], {}, 0, 0
|
||||||
@@ -148,6 +210,7 @@ def main():
|
|||||||
initial = snapshot()
|
initial = snapshot()
|
||||||
apis = [c for c in initial if c["role"] == "api"]
|
apis = [c for c in initial if c["role"] == "api"]
|
||||||
assert len(apis) == 2 and len({c["pid"] for c in apis}) == 2
|
assert len(apis) == 2 and len({c["pid"] for c in apis}) == 2
|
||||||
|
report["closed_peer_pool_checkout"] = closed_peer_checkout()
|
||||||
report["before"] = initial
|
report["before"] = initial
|
||||||
with httpx.Client(
|
with httpx.Client(
|
||||||
base_url=BASE,
|
base_url=BASE,
|
||||||
|
|||||||
@@ -43,6 +43,10 @@ class Database:
|
|||||||
},
|
},
|
||||||
min_size=1,
|
min_size=1,
|
||||||
max_size=size,
|
max_size=size,
|
||||||
|
# TLS gateways retire established sessions. Validate an idle
|
||||||
|
# connection before handing it to a business transaction; a broken
|
||||||
|
# checkout must not consume a lease or retry an uncertain COMMIT.
|
||||||
|
check=ConnectionPool.check_connection,
|
||||||
timeout=1,
|
timeout=1,
|
||||||
max_lifetime=30,
|
max_lifetime=30,
|
||||||
max_idle=15,
|
max_idle=15,
|
||||||
|
|||||||
Reference in New Issue
Block a user