Karan Adhikari commited on
Commit
854dc06
·
unverified ·
1 Parent(s): 1bcc59c

Update custom_dl.py

Browse files
Files changed (1) hide show
  1. Backend/helper/custom_dl.py +16 -38
Backend/helper/custom_dl.py CHANGED
@@ -18,11 +18,7 @@ from Backend import db
18
  from Backend.pyrofork.bot import work_loads, multi_clients, client_dc_map, client_failures, client_avg_mbps
19
 
20
  ACTIVE_STREAMS: Dict[str, Dict] = {}
21
- RECENT_STREAMS = deque(maxlen=50)
22
-
23
-
24
- def get_adaptive_chunk_size(client_index: int) -> int:
25
- return 1024 * 1024
26
 
27
  class ByteStreamer:
28
  CHUNK_SIZE = 1024 * 1024 # 1 MB
@@ -41,54 +37,36 @@ class ByteStreamer:
41
 
42
  async def _prewarm_sessions(self):
43
  common_dcs = [1, 2, 4, 5]
44
- LOGGER.debug("Pre-warming media sessions for common DCs...")
45
-
46
  for dc in common_dcs:
 
 
47
  try:
48
- if dc in self.client.media_sessions:
49
- LOGGER.debug(f"Media session for DC {dc} already exists, skipping")
50
- continue
51
-
52
- test_mode = await self.client.storage.test_mode()
53
- current_dc = await self.client.storage.dc_id()
54
-
55
- if dc == current_dc:
56
- continue
57
-
58
  auth_key = await Auth(self.client, dc, test_mode).create()
59
  session = Session(self.client, dc, auth_key, test_mode, is_media=True)
60
  session.no_updates = True
61
  session.timeout = 30
62
  session.sleep_threshold = 60
63
-
64
  await session.start()
65
-
66
- for attempt in range(6):
67
  try:
68
- exported = await self.client.invoke(
69
- raw.functions.auth.ExportAuthorization(dc_id=dc)
70
- )
71
- await session.send(
72
- raw.functions.auth.ImportAuthorization(
73
- id=exported.id, bytes=exported.bytes
74
- )
75
- )
76
  break
77
  except AuthBytesInvalid:
78
- LOGGER.debug(f"AuthBytesInvalid during pre-warm for DC {dc}; retrying...")
79
  await asyncio.sleep(0.5)
80
  except OSError:
81
- LOGGER.debug(f"OSError during pre-warm for DC {dc}; retrying...")
82
  await asyncio.sleep(1)
83
- except Exception as e:
84
- LOGGER.debug(f"Error during pre-warm for DC {dc}: {e}")
85
  break
86
-
87
- self.client.media_sessions[dc] = session
88
- LOGGER.debug(f"Pre-warmed media session for DC {dc}")
89
-
90
- except Exception as e:
91
- LOGGER.debug(f"Could not pre-warm DC {dc}: {e}")
92
  continue
93
 
94
  async def get_file_properties(self, chat_id: int, message_id: int) -> FileId:
 
18
  from Backend.pyrofork.bot import work_loads, multi_clients, client_dc_map, client_failures, client_avg_mbps
19
 
20
  ACTIVE_STREAMS: Dict[str, Dict] = {}
21
+ RECENT_STREAMS = deque(maxlen=20)
 
 
 
 
22
 
23
  class ByteStreamer:
24
  CHUNK_SIZE = 1024 * 1024 # 1 MB
 
37
 
38
  async def _prewarm_sessions(self):
39
  common_dcs = [1, 2, 4, 5]
40
+ test_mode = await self.client.storage.test_mode()
41
+ current_dc = await self.client.storage.dc_id()
42
  for dc in common_dcs:
43
+ if dc in self.client.media_sessions or dc == current_dc:
44
+ continue
45
  try:
 
 
 
 
 
 
 
 
 
 
46
  auth_key = await Auth(self.client, dc, test_mode).create()
47
  session = Session(self.client, dc, auth_key, test_mode, is_media=True)
48
  session.no_updates = True
49
  session.timeout = 30
50
  session.sleep_threshold = 60
 
51
  await session.start()
52
+ imported = False
53
+ for _ in range(6):
54
  try:
55
+ exported = await self.client.invoke(raw.functions.auth.ExportAuthorization(dc_id=dc))
56
+ await session.send(raw.functions.auth.ImportAuthorization(id=exported.id,bytes=exported.bytes,))
57
+ imported = True
 
 
 
 
 
58
  break
59
  except AuthBytesInvalid:
 
60
  await asyncio.sleep(0.5)
61
  except OSError:
 
62
  await asyncio.sleep(1)
63
+ except Exception:
 
64
  break
65
+ if imported:
66
+ self.client.media_sessions[dc] = session
67
+ else:
68
+ await session.stop()
69
+ except Exception:
 
70
  continue
71
 
72
  async def get_file_properties(self, chat_id: int, message_id: int) -> FileId: