Karan Adhikari commited on
Commit ·
df26afd
1
Parent(s): 3b801a6
Improve DC-aware client selection for streaming requests
Browse files
Backend/fastapi/routes/stream_routes.py
CHANGED
|
@@ -89,18 +89,20 @@ def parse_range_header(range_header: str, file_size: int):
|
|
| 89 |
|
| 90 |
|
| 91 |
def select_best_client(target_dc: int) -> int:
|
| 92 |
-
"""Pick the best available client.
|
| 93 |
|
| 94 |
Score = work_loads + 3 × client_failures
|
| 95 |
Failures are weighted 3× so a bot that has been timing out / erroring
|
| 96 |
is deprioritised even if its current workload is low.
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
"""
|
| 100 |
def _score(idx: int) -> int:
|
| 101 |
return work_loads.get(idx, 0) + 3 * client_failures.get(idx, 0)
|
| 102 |
|
| 103 |
-
# --- DC-aware selection
|
| 104 |
matching = [
|
| 105 |
idx for idx, dc in client_dc_map.items()
|
| 106 |
if dc == target_dc and idx in multi_clients
|
|
@@ -109,8 +111,8 @@ def select_best_client(target_dc: int) -> int:
|
|
| 109 |
selected = min(matching, key=_score)
|
| 110 |
LOGGER.debug("DC-match client %s (DC %s) score=%s", selected, target_dc, _score(selected))
|
| 111 |
return selected
|
| 112 |
-
# ------------------------------------------------------------------------
|
| 113 |
|
|
|
|
| 114 |
if multi_clients:
|
| 115 |
selected = min(multi_clients.keys(), key=_score)
|
| 116 |
LOGGER.debug(
|
|
@@ -259,6 +261,7 @@ async def media_streamer(
|
|
| 259 |
target_dc = file_id.dc_id
|
| 260 |
LOGGER.debug(f"File msg_id={msg_id} is in DC {target_dc}")
|
| 261 |
|
|
|
|
| 262 |
index = select_best_client(target_dc)
|
| 263 |
tg_client = multi_clients[index]
|
| 264 |
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
def select_best_client(target_dc: int) -> int:
|
| 92 |
+
"""Pick the best available client with DC-aware priority.
|
| 93 |
|
| 94 |
Score = work_loads + 3 × client_failures
|
| 95 |
Failures are weighted 3× so a bot that has been timing out / erroring
|
| 96 |
is deprioritised even if its current workload is low.
|
| 97 |
+
|
| 98 |
+
Priority:
|
| 99 |
+
1. Same-DC clients (lowest latency)
|
| 100 |
+
2. Any other available client
|
| 101 |
"""
|
| 102 |
def _score(idx: int) -> int:
|
| 103 |
return work_loads.get(idx, 0) + 3 * client_failures.get(idx, 0)
|
| 104 |
|
| 105 |
+
# --- DC-aware selection: Prefer same-DC clients ---
|
| 106 |
matching = [
|
| 107 |
idx for idx, dc in client_dc_map.items()
|
| 108 |
if dc == target_dc and idx in multi_clients
|
|
|
|
| 111 |
selected = min(matching, key=_score)
|
| 112 |
LOGGER.debug("DC-match client %s (DC %s) score=%s", selected, target_dc, _score(selected))
|
| 113 |
return selected
|
|
|
|
| 114 |
|
| 115 |
+
# Fallback: pick best overall client
|
| 116 |
if multi_clients:
|
| 117 |
selected = min(multi_clients.keys(), key=_score)
|
| 118 |
LOGGER.debug(
|
|
|
|
| 261 |
target_dc = file_id.dc_id
|
| 262 |
LOGGER.debug(f"File msg_id={msg_id} is in DC {target_dc}")
|
| 263 |
|
| 264 |
+
# Use improved DC-aware selection
|
| 265 |
index = select_best_client(target_dc)
|
| 266 |
tg_client = multi_clients[index]
|
| 267 |
|