Skip to content

Commit 677aaa8

Browse files
committed
Discard a request if the caller already left
No need to perform some inference that won't be fetched anyway Signed-off-by: Raphael Glon <[email protected]>
1 parent 7f7255a commit 677aaa8

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

api_inference_community/routes.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import base64
22
import io
3+
import ipaddress
34
import logging
45
import os
56
import time
67
from typing import Any, Dict
78

9+
import psutil
810
from api_inference_community.validation import (
911
AUDIO,
1012
AUDIO_INPUTS,
@@ -27,8 +29,69 @@
2729
logger = logging.getLogger(__name__)
2830

2931

32+
def already_left(request: Request) -> bool:
33+
"""
34+
Check if the caller has already left without waiting for the answer to come. This can help during burst to relieve
35+
the pressure on the worker by cancelling jobs whose results don't matter as they won't be fetched anyway
36+
:param request:
37+
:return: bool
38+
"""
39+
# NOTE(rg): Starlette method request.is_disconnected is totally broken, consumes the payload, does not return
40+
# the correct status. So we use the good old way to identify if the caller is still there.
41+
# In any case, if we are not sure, we return False
42+
logger.info("Checking if request caller already left")
43+
try:
44+
client = request.client
45+
host = client.host
46+
if not host:
47+
return False
48+
49+
port = int(client.port)
50+
host = ipaddress.ip_address(host)
51+
52+
if port <= 0 or port > 65535:
53+
logger.warning("Unexpected source port format for caller %s", port)
54+
return False
55+
counter = 0
56+
for connection in psutil.net_connections(kind="tcp"):
57+
counter += 1
58+
if connection.status != "ESTABLISHED":
59+
continue
60+
if not connection.laddr:
61+
continue
62+
if int(connection.laddr.port) != port:
63+
continue
64+
if (
65+
not connection.laddr.ip
66+
or ipaddress.ip_address(connection.laddr.ip) != host
67+
):
68+
continue
69+
logger.info(
70+
"Found caller connection still established, caller is most likely still there, %s",
71+
connection,
72+
)
73+
return False
74+
except Exception as e:
75+
logger.warning(
76+
"Unexpected error while checking if caller already left, assuming still there"
77+
)
78+
logger.exception(e)
79+
return False
80+
81+
logger.info(
82+
"%d connections checked. No connection found matching to the caller, probably left",
83+
counter,
84+
)
85+
return True
86+
87+
3088
async def pipeline_route(request: Request) -> Response:
3189
start = time.time()
90+
91+
if already_left(request):
92+
logger.info("Discarding request as the caller already left")
93+
return Response(status_code=204)
94+
3295
payload = await request.body()
3396
task = os.environ["TASK"]
3497
if os.getenv("DEBUG", "0") in {"1", "true"}:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ parameterized>=0.8.1
55
pillow>=8.2.0
66
huggingface_hub>=0.20.2
77
datasets>=2.2
8+
psutil>=6.0.0
89
pytest
910
httpx
1011
uvicorn

0 commit comments

Comments
 (0)