Reserve bounded upload buffers without starving readiness and enforce lease deadlines

This commit is contained in:
2026-11-16 13:05:47 +03:00
parent 5fc30acb1c
commit 4776d41dfe
2 changed files with 24 additions and 3 deletions
+6 -2
View File
@@ -41,14 +41,18 @@ def refresh():
raise Failure(503, "Сервис секретов временно недоступен")
try:
role = os.environ["BAO_ROLE"]
auth_started = time.monotonic()
auth = request("auth/cert/login", data={"name": role})["auth"]
deadline = auth_started + min(float(auth["lease_duration"]), 30) - 1
values = request("secret/data/services/" + role, token=auth["client_token"])["data"][
"data"
]
# Never keep credentials usable beyond the auth token's short lease.
if time.monotonic() >= deadline:
raise ValueError("Service credential lease already expired")
# Deadline starts before auth, never after a slow KV read.
_values = values
_retry_after = 0.0
_valid_until = time.monotonic() + min(float(auth["lease_duration"]), 30) - 1
_valid_until = deadline
except (OSError, ValueError, TypeError, KeyError, urllib.error.URLError):
_values = {}
_valid_until = 0
+18 -1
View File
@@ -208,12 +208,19 @@ class HTTPHandler(BaseHTTPRequestHandler):
if not permitted:
self.send_error(403)
return
has_body_slot = False
body = None
try:
length = int(self.headers.get("Content-Length", "0"))
body_limit = MAX_BODY if cfg.get("s3") else 1024 * 1024
if length < 0 or length > body_limit or self.headers.get("Transfer-Encoding"):
self.send_error(413)
return
if length:
has_body_slot = self.server.body_slots.acquire(blocking=False)
if not has_body_slot:
self.send_error(503)
return
if self.headers.get("Expect", "").lower() == "100-continue":
self.wfile.write(b"HTTP/1.1 100 Continue\r\n\r\n")
self.wfile.flush()
@@ -242,18 +249,28 @@ class HTTPHandler(BaseHTTPRequestHandler):
self.wfile.write(chunk)
except (OSError, ValueError, http.client.HTTPException):
self.close_connection = True
finally:
if has_body_slot:
body = None
self.server.body_slots.release()
class HTTPServer(Boundary, ThreadingHTTPServer):
daemon_threads = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Bound large request buffers, while HEAD/readiness and streamed GETs
# do not compete for the two upload-body slots.
self.body_slots = threading.BoundedSemaphore(2)
def serve(cfg):
cls = HTTPServer if cfg.get("http") else TCPServer
handler = HTTPHandler if cfg.get("http") else TCPHandler
with cls(("0.0.0.0", cfg["port"]), handler) as server:
server.cfg = cfg
server.slots = threading.BoundedSemaphore(2 if cfg.get("http") else 32)
server.slots = threading.BoundedSemaphore(16 if cfg.get("http") else 32)
server.serve_forever(poll_interval=0.2)