Spaces:
Running
Running
Commit
·
4a4b50e
1
Parent(s):
f2772b4
Code Updates & SeveralFixes
Browse files
FileStream/server/__init__.py
CHANGED
@@ -10,30 +10,24 @@ from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS
|
|
10 |
|
11 |
|
12 |
# Set time to consider a client inactive (e.g., 10 minutes)
|
13 |
-
INACTIVITY_TIMEOUT =
|
14 |
|
15 |
async def clear_inactive_clients():
|
16 |
"""Clear inactive clients from ACTIVE_CLIENTS."""
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
for client in inactive_clients:
|
28 |
-
# Log and clear the inactive client
|
29 |
-
logging.info(f"Clearing inactive client: {client}")
|
30 |
-
del ACTIVE_CLIENTS[client] # Remove inactive client from ACTIVE_CLIENTS
|
31 |
|
32 |
|
33 |
async def root_route_handler(request):
|
34 |
-
|
35 |
-
ACTIVE_CLIENTS[client_id] = asyncio.get_event_loop().time() # Update last activity time
|
36 |
-
return web.json_response({"status": "alive", "message": "Server is running"})
|
37 |
|
38 |
|
39 |
def web_server():
|
|
|
10 |
|
11 |
|
12 |
# Set time to consider a client inactive (e.g., 10 minutes)
|
13 |
+
INACTIVITY_TIMEOUT = 180 # 3 minutes
|
14 |
|
15 |
async def clear_inactive_clients():
|
16 |
"""Clear inactive clients from ACTIVE_CLIENTS."""
|
17 |
+
await asyncio.sleep(INACTIVITY_TIMEOUT) # Check every INACTIVITY_TIMEOUT seconds
|
18 |
+
now = asyncio.get_event_loop().time() # Get current time
|
19 |
+
inactive_clients = [
|
20 |
+
client_id for client_id, tg_connect in ACTIVE_CLIENTS.items()
|
21 |
+
if now - tg_connect.last_activity > INACTIVITY_TIMEOUT # Compare with last_activity timestamp
|
22 |
+
]
|
23 |
+
for client in inactive_clients:
|
24 |
+
# Log and clear the inactive client
|
25 |
+
logging.info(f"** Clearing inactive client: {client.name}")
|
26 |
+
del ACTIVE_CLIENTS[client] # Remove inactive client from ACTIVE_CLIENTS
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
async def root_route_handler(request):
|
30 |
+
return web.json_response({"status": "alive", "message": "Server is running","active_clients":ACTIVE_CLIENTS,"public_ip":request.remote})
|
|
|
|
|
31 |
|
32 |
|
33 |
def web_server():
|
FileStream/utils/FileProcessors/custom_dl.py
CHANGED
@@ -214,6 +214,8 @@ class ByteStreamer:
|
|
214 |
raw.functions.upload.GetFile(location=location, offset=offset, limit=chunk_size), )
|
215 |
except (TimeoutError, AttributeError):
|
216 |
pass
|
|
|
|
|
217 |
finally:
|
218 |
logging.debug(f"Finished yielding file with {current_part} parts.")
|
219 |
WORK_LOADS[index] -= 1 # Decrease the workload for this client
|
@@ -223,17 +225,8 @@ class ByteStreamer:
|
|
223 |
Function to clean the cache to reduce memory usage.
|
224 |
This method will be called periodically to clear the cached file properties.
|
225 |
"""
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
logging.debug("Cache cleaned.")
|
231 |
|
232 |
-
@staticmethod
|
233 |
-
def is_active(last_activity: float, timeout: float = 120) -> bool:
|
234 |
-
"""
|
235 |
-
Determines if the client is active based on the last activity time.
|
236 |
-
If the client has been inactive for longer than the timeout period (default 30 minutes),
|
237 |
-
they are considered inactive.
|
238 |
-
"""
|
239 |
-
return (asyncio.get_event_loop().time() - last_activity) < timeout
|
|
|
214 |
raw.functions.upload.GetFile(location=location, offset=offset, limit=chunk_size), )
|
215 |
except (TimeoutError, AttributeError):
|
216 |
pass
|
217 |
+
except Exception as e:
|
218 |
+
logging.info(f"Error at Bytestreamer Generating Chunk : {e}")
|
219 |
finally:
|
220 |
logging.debug(f"Finished yielding file with {current_part} parts.")
|
221 |
WORK_LOADS[index] -= 1 # Decrease the workload for this client
|
|
|
225 |
Function to clean the cache to reduce memory usage.
|
226 |
This method will be called periodically to clear the cached file properties.
|
227 |
"""
|
228 |
+
await asyncio.sleep(self.clean_timer) # Wait for the cleanup interval
|
229 |
+
logging.info("*** Cleaning cached file IDs...")
|
230 |
+
self.cached_file_ids.clear() # Clear the cache
|
231 |
+
logging.debug("Cache cleaned.")
|
|
|
232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|