fix: stabilize proxy verification readiness

This commit is contained in:
2026-09-22 15:15:32 +08:00
parent 30fdad44c2
commit ca707761fc

View File

@@ -103,14 +103,21 @@ def request(connection: http.client.HTTPSConnection, path: str) -> tuple[int, di
return response.status, dict(response.headers), body
def header(headers: dict[str, str], name: str) -> str:
for key, value in headers.items():
if key.lower() == name.lower():
return value
return ""
def verify_keep_alive(edge_port: int) -> None:
context = ssl._create_unverified_context()
connection = http.client.HTTPSConnection("127.0.0.1", edge_port, context=context, timeout=3)
expected = b'{"code":"OK","message":"success","data":{"status":"ok"},"requestId":"proxyCheck"}'
expected = b'{"code":"OK","message":"success","data":{"status":"ok"},"requestId":"proxyCheck"}\n'
try:
for attempt in range(5):
status, headers, body = request(connection, "/health")
if status != 200 or headers.get("X-Request-ID") != "proxyCheck" or body != expected:
if status != 200 or header(headers, "X-Request-ID") != "proxyCheck" or body != expected:
raise AssertionError(f"unexpected proxied response: status={status} body={body!r}")
if attempt < 4 and getattr(connection, "sock", None) is None:
raise AssertionError("Nginx did not keep the TLS client connection alive")
@@ -118,6 +125,27 @@ def verify_keep_alive(edge_port: int) -> None:
connection.close()
def wait_for_proxy(edge_port: int, timeout: float) -> None:
deadline = time.monotonic() + timeout
last_error = "proxy did not return a healthy response"
while time.monotonic() < deadline:
connection = None
try:
context = ssl._create_unverified_context()
connection = http.client.HTTPSConnection("127.0.0.1", edge_port, context=context, timeout=2)
status, headers, body = request(connection, "/health")
if status == 200 and header(headers, "X-Request-ID") == "proxyCheck" and body.endswith(b"\n"):
return
last_error = f"status={status} body={body!r}"
except (OSError, http.client.HTTPException) as error:
last_error = str(error)
finally:
if connection is not None:
connection.close()
time.sleep(0.2)
raise RuntimeError(f"Nginx proxy did not become healthy: {last_error}")
def verify_timeout(edge_port: int) -> None:
context = ssl._create_unverified_context()
connection = http.client.HTTPSConnection("127.0.0.1", edge_port, context=context, timeout=3)
@@ -190,6 +218,7 @@ def main() -> None:
else:
raise RuntimeError("Nginx did not start listening")
wait_for_proxy(edge_port, 30)
verify_keep_alive(edge_port)
verify_timeout(edge_port)
print("PASS Nginx TLS proxy, Keep-Alive and upstream timeout", flush=True)