Files
mtusi2026lab2/src/messenger/server.py
T
glebossokin ef96551b7d
Lab 2 application / application (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
ЛР 2: PostgreSQL, Redis-сессии и проверки на оценку 4
2026-10-26 23:25:37 +03:00

332 lines
13 KiB
Python

"""Bounded HTTP worker pool; one request per connection; JSON logs omit bodies."""
from collections import Counter
from http.server import BaseHTTPRequestHandler, HTTPServer
import http.client
import json
import logging
import queue
import signal
import socket
import threading
import time
from urllib.parse import parse_qs, urlsplit
import uuid
import psycopg
from psycopg_pool import PoolTimeout, TooManyRequests
import redis
from .core import APIError, Config, Store
CODES = {400: "invalid_request", 401: "unauthorized", 404: "not_found", 409: "conflict",
413: "payload_too_large", 415: "unsupported_media_type", 429: "rate_limited",
503: "unavailable", 500: "internal_error"}
def error_body(status, message, request_id):
return {"error": {"code": CODES.get(status, "invalid_request"),
"message": message, "request_id": request_id}}
def log_event(**data):
logging.info(json.dumps(data, ensure_ascii=True))
class Server(HTTPServer):
allow_reuse_address = True
def __init__(self, config):
self.config, self.store = config, Store(config)
self.jobs = queue.Queue(maxsize=config.request_queue)
self.draining = threading.Event()
self.stats_lock = threading.Lock()
self.statuses = Counter()
self.active = 0
self.clients = set()
self.started = time.monotonic()
self.request_queue_size = config.request_queue
super().__init__((config.app_host, config.app_port), Handler)
self.threads = [threading.Thread(target=self.worker, name="http-" + str(n), daemon=True)
for n in range(config.workers)]
for thread in self.threads:
thread.start()
def get_request(self):
client, address = super().get_request()
client.settimeout(self.config.socket_timeout)
return client, address
def record(self, status):
with self.stats_lock:
self.statuses[str(status)] += 1
def process_request(self, request, address):
try:
if self.draining.is_set():
raise queue.Full()
self.jobs.put_nowait((request, address))
except queue.Full:
# Transport admission before HTTP parsing: bounded time, no extra thread.
identifier = str(uuid.uuid4())
body = json.dumps(error_body(503, "Request queue full or shutting down", identifier)).encode()
headers = ("HTTP/1.1 503 Service Unavailable\r\nContent-Type: application/json\r\n"
"Connection: close\r\nRetry-After: 1\r\nX-Request-Id: " + identifier +
"\r\nContent-Length: " + str(len(body)) + "\r\n\r\n").encode()
try:
request.settimeout(.05)
# Wait for the first request bytes: closing before a local client
# finishes sendall can otherwise discard the 503 with a TCP reset.
try:
request.recv(16384)
except OSError:
pass
request.sendall(headers + body)
request.shutdown(socket.SHUT_WR)
request.settimeout(.01)
for _ in range(5):
if not request.recv(16384):
break
except OSError:
pass
self.shutdown_request(request)
self.record(503)
log_event(event="queue_rejected", status=503, request_id=identifier)
def worker(self):
while not self.draining.is_set():
try:
request, address = self.jobs.get(timeout=.1)
except queue.Empty:
continue
with self.stats_lock:
self.active += 1
self.clients.add(request)
try:
self.finish_request(request, address)
except OSError:
pass
except Exception:
log_event(event="worker_error")
finally:
self.shutdown_request(request)
with self.stats_lock:
self.active -= 1
self.clients.discard(request)
self.jobs.task_done()
def server_close(self):
self.draining.set()
with self.stats_lock:
clients = list(self.clients)
for client in clients:
try:
client.shutdown(socket.SHUT_RDWR)
except OSError:
pass
while True:
try:
request, _ = self.jobs.get_nowait()
except queue.Empty:
break
self.shutdown_request(request)
self.jobs.task_done()
deadline = time.monotonic() + 5
for thread in self.threads:
thread.join(timeout=max(0, deadline - time.monotonic()))
self.store.close()
super().server_close()
class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
server_version = "Messenger"
sys_version = ""
def setup(self):
super().setup()
self.request_id = str(uuid.uuid4())
self.received = time.monotonic()
def log_message(self, *_):
pass
def parse_request(self):
original = self.rfile
class HeaderBudget:
remaining = 16384
def readline(self, limit=-1):
line = original.readline(min(limit, self.remaining + 1) if limit >= 0 else self.remaining + 1)
self.remaining -= len(line)
if self.remaining < 0:
raise http.client.LineTooLong("header section")
return line
self.rfile = HeaderBudget()
try:
return super().parse_request()
finally:
self.rfile = original
def send_error(self, code, message=None, explain=None):
self.reply(code if code in CODES else 400, error_body(
code if code in CODES else 400, "Invalid HTTP request", self.request_id))
def reply(self, status, body):
raw = b'' if status == 204 else json.dumps(body, ensure_ascii=False, allow_nan=False).encode('utf-8')
content_type = 'application/json'
self.send_response(status)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(raw)))
self.send_header("X-Request-Id", self.request_id)
self.send_header("X-Content-Type-Options", "nosniff")
self.send_header("Connection", "close")
if status == 401:
challenge = ('Basic realm="messenger", charset="UTF-8"'
if urlsplit(self.path).path == '/api/v1/auth/sessions' else 'Bearer realm="messenger"')
self.send_header('WWW-Authenticate', challenge)
if status in (429, 503):
self.send_header("Retry-After", "1")
self.end_headers()
self.close_connection = True
self.wfile.write(raw)
self.server.record(status)
# No URL query, header, username, text, or exception detail in logs.
log_event(event="request", method=self.command, status=status,
request_id=self.request_id, elapsed_ms=round(1000 * (time.monotonic() - self.received), 3))
def handle_expect_100(self):
# Do not acknowledge a large unauthorized request before checking identity.
return True
def body(self):
if self.headers.get_all("Transfer-Encoding"):
raise APIError(400, "Transfer-Encoding is unsupported")
sizes = self.headers.get_all("Content-Length", [])
if len(sizes) != 1 or not sizes[0].isascii() or not sizes[0].isdigit() or len(sizes[0]) > 10:
raise APIError(400, "One valid Content-Length is required")
size = int(sizes[0])
if size > self.server.config.max_body_bytes:
raise APIError(413, "Request body exceeds byte limit")
if self.headers.get_content_type() != "application/json":
raise APIError(415, "Expected application/json")
if self.headers.get("Expect", "").lower() == "100-continue":
self.send_response_only(100)
self.end_headers()
raw = self.rfile.read(size)
if len(raw) != size:
raise APIError(400, "Incomplete request body")
def pairs(values):
result = {}
for key, value in values:
if key in result:
raise ValueError("duplicate key")
result[key] = value
return result
def constant(_):
raise ValueError("non JSON constant")
try:
return json.loads(raw.decode("utf-8"), object_pairs_hook=pairs, parse_constant=constant)
except (ValueError, UnicodeError, RecursionError):
raise APIError(400, "Invalid JSON") from None
def dispatch(self):
try:
url = urlsplit(self.path)
except ValueError:
raise APIError(400, "Invalid request target") from None
path, method = url.path, self.command
store = self.server.store
if self.server.draining.is_set():
raise APIError(503, "Shutting down")
if method == 'GET' and path == '/health/live':
return 200, {'status': 'ok'}
if method == 'GET' and path == '/health/ready':
store.ready()
return 200, {'status': 'ok'}
if path == '/api/v1/users' and method == 'POST':
return 201, store.create_user(self.body())
headers = self.headers.get_all('Authorization', [])
authorization = headers[0] if len(headers) == 1 else None
if path == '/api/v1/auth/sessions' and method == 'POST':
if self.headers.get('Transfer-Encoding') or self.headers.get('Content-Length', '0') != '0':
raise APIError(400, 'Login takes no request body')
return 201, store.login(authorization)
actor, session_key = store.auth(authorization)
if method == 'DELETE' and path == '/api/v1/auth/sessions/current':
store.cache.delete(session_key)
return 204, None
if method == 'GET' and path == '/api/v1/users/me':
return 200, store.me(actor)
parts = path.strip("/").split("/")
chat_id = None
if len(parts) >= 4 and parts[:3] == ["api", "v1", "chats"]:
chat_id = store.chat_access(actor, parts[3])
if method == "POST" and path == "/api/v1/chats":
return 201, store.create_chat(actor, self.body())
if method == "POST" and chat_id and len(parts) == 5 and parts[4] == "messages":
return 201, store.create_message(actor, chat_id, self.body())
if method == "GET":
if chat_id and len(parts) == 4:
return 200, store.chat(chat_id)
if path == "/api/v1/chats" or (chat_id and len(parts) == 5 and parts[4] == "messages"):
try:
query = parse_qs(url.query, keep_blank_values=True, strict_parsing=True, max_num_fields=8)
except ValueError:
raise APIError(400, "Invalid query") from None
return 200, store.page(actor, chat_id, query)
raise APIError(404, "Object not found")
def handle_api(self):
try:
status, result = self.dispatch()
except APIError as error:
status = error.status
result = error_body(status, error.message, self.request_id)
except (psycopg.OperationalError, psycopg.InterfaceError, psycopg.errors.QueryCanceled,
psycopg.errors.LockNotAvailable, PoolTimeout, TooManyRequests, redis.RedisError):
status, result = 503, error_body(503, 'Dependency temporarily unavailable', self.request_id)
except (TimeoutError, socket.timeout):
status, result = 400, error_body(400, "Request read timeout", self.request_id)
except Exception:
status, result = 500, error_body(500, "Internal error", self.request_id)
log_event(event="internal_error", request_id=self.request_id)
try:
self.reply(status, result)
except OSError:
pass
do_GET = do_POST = do_PUT = do_DELETE = do_PATCH = do_HEAD = do_OPTIONS = handle_api
def main():
logging.basicConfig(level=logging.INFO, format="%(message)s")
# Pool diagnostics may include connection details; application emits sanitized errors.
logging.getLogger('psycopg.pool').disabled = True
config = Config.from_env()
server = Server(config)
def stop(signum, _):
if not server.draining.is_set():
server.draining.set()
log_event(event="shutdown_started", signal=signum)
threading.Thread(target=server.shutdown, daemon=True).start()
signal.signal(signal.SIGTERM, stop)
signal.signal(signal.SIGINT, stop)
log_event(event="started", port=config.app_port, workers=config.workers)
try:
server.serve_forever(poll_interval=.1)
finally:
server.server_close()
log_event(event="shutdown_complete")
if __name__ == "__main__":
main()