ЛР3: балансировка, отказоустойчивость и идемпотентность

This commit is contained in:
2026-10-23 18:54:18 +03:00
parent 3f7c306f4f
commit 57b4e8b5ec
20 changed files with 659 additions and 39 deletions
+6 -1
View File
@@ -1,4 +1,6 @@
import logging
import os
import socket
from uuid import uuid4
from starlette.datastructures import MutableHeaders
@@ -11,6 +13,7 @@ logger = logging.getLogger('app')
class RequestContext:
def __init__(self, app) -> None:
self.app = app
self.instance_id = os.getenv('INSTANCE_ID') or socket.gethostname()
async def __call__(self, scope, receive, send) -> None:
if scope['type'] != 'http':
@@ -25,7 +28,9 @@ class RequestContext:
nonlocal started
if message['type'] == 'http.response.start':
started = True
MutableHeaders(scope=message)['X-Request-Id'] = request_id
headers = MutableHeaders(scope=message)
headers['X-Request-Id'] = request_id
headers['X-Instance-Id'] = self.instance_id
await send(message)
try: