import json
import os
import struct
import sys
import threading
import time
DESCRIPTOR = json.loads("""__DESCRIPTOR__""")
SCENARIO = "__SCENARIO__"
MAGIC = 0xA2
CALL = 1
RESPONSE = 2
PREFIX = struct.Struct("<BBHQII")
WRITE_LOCK = threading.Lock()
STATE_LOCK = threading.Lock()
STATE = {"calls": 0, "active": 0, "peak": 0}
def read_exact(size: int) -> bytes:
data = sys.stdin.buffer.read(size)
if len(data) != size:
sys.exit(0)
return data
def write(data: bytes) -> None:
with WRITE_LOCK:
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
def respond_json(status: int, payload: bytes) -> None:
body = json.dumps({"status": status, "payload": list(payload)}).encode()
write(struct.pack("<I", len(body)) + body)
def respond_binary(request_id: int, status: int, payload: bytes) -> None:
write(PREFIX.pack(MAGIC, RESPONSE, 0, request_id, 4, len(payload)) + struct.pack("<i", status) + payload)
def handle(method: str, payload: bytes, mode: str) -> tuple[int, bytes]:
with STATE_LOCK:
STATE["calls"] += 1
calls = STATE["calls"]
if method == "sleep":
time.sleep(float(payload))
return 0, payload
if method == "hang":
time.sleep(60)
if method == "exit":
os._exit(7)
if method == "failure":
return 3, b"producer failed"
if method == "count":
return 0, str(calls).encode()
if method == "pid":
return 0, str(os.getpid()).encode()
if method == "mode":
return 0, mode.encode()
if method == "peak":
with STATE_LOCK:
return 0, str(STATE["peak"]).encode()
return 0, payload
def serve_call(request_id: int, method: str, payload: bytes) -> None:
with STATE_LOCK:
STATE["active"] += 1
STATE["peak"] = max(STATE["peak"], STATE["active"])
try:
if method == "bad_id":
respond_binary(request_id + 1000, 0, b"stray")
return
if method == "garbage":
write(b"\x00" * PREFIX.size)
return
status, body = handle(method, payload, "binary")
respond_binary(request_id, status, body)
finally:
with STATE_LOCK:
STATE["active"] -= 1
def serve_binary() -> None:
while True:
magic, kind, flags, request_id, header_len, payload_len = PREFIX.unpack(read_exact(PREFIX.size))
if magic != MAGIC or kind != CALL or flags != 0:
sys.exit(2)
header = read_exact(header_len)
payload = read_exact(payload_len)
contract_len = struct.unpack_from("<H", header)[0]
method = header[2 + contract_len :].decode()
threading.Thread(target=serve_call, args=(request_id, method, payload), daemon=True).start()
def serve_json() -> None:
while True:
size = struct.unpack("<I", read_exact(4))[0]
request = json.loads(read_exact(size))
status, body = handle(request["method"], bytes(request["payload"]), "json")
respond_json(status, body)
def main() -> None:
size = struct.unpack("<I", read_exact(4))[0]
if json.loads(read_exact(size))["op"] != "describe":
sys.exit(3)
offered = os.environ.get("ALEF_STDIO_PROTOCOLS", "").split(",")
binary = SCENARIO.startswith("binary") and "2" in offered
if binary:
DESCRIPTOR["protocol_version"] = 2
if "/" in SCENARIO:
DESCRIPTOR["max_in_flight"] = int(SCENARIO.split("/", 1)[1])
if SCENARIO == "json-with-limit":
DESCRIPTOR["max_in_flight"] = 4
respond_json(0, json.dumps(DESCRIPTOR).encode())
if binary:
serve_binary()
else:
serve_json()
main()