File size: 1,703 Bytes
97f9e03
 
e566133
97f9e03
 
32585f3
 
 
97f9e03
 
 
 
2a429c7
97f9e03
 
2a429c7
97f9e03
2a429c7
97f9e03
2a429c7
faa9726
97f9e03
faa9726
 
97f9e03
 
 
 
 
2a429c7
e566133
 
 
97f9e03
 
e566133
 
 
 
 
 
 
 
 
e3d3fc0
e566133
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import asyncio
import logging
from aiohttp import web

#-----------------------Local Imports-------------------------#
from .routes_api import api
from .routes_main import routes
from .routes_app import sub_app
from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS


# Set time to consider a client inactive (e.g., 10 minutes)
INACTIVITY_TIMEOUT = 240  # 4 minutes

async def clear_inactive_clients():
    """Clear inactive clients from ACTIVE_CLIENTS."""
    while True:
        await asyncio.sleep(INACTIVITY_TIMEOUT)  # Check every INACTIVITY_TIMEOUT seconds

        now = asyncio.get_event_loop().time()  # Get current time
        
        inactive_clients = [
            client_id for client_id, tg_connect in ACTIVE_CLIENTS.items()
            if now - tg_connect.last_activity > INACTIVITY_TIMEOUT  # Compare with last_activity timestamp
        ]

        for client in inactive_clients:
            # Log and clear the inactive client
            logging.info(f"Clearing inactive client: {client}")
            del ACTIVE_CLIENTS[client]  # Remove inactive client from ACTIVE_CLIENTS


async def root_route_handler(request):
    client_id = request.remote  # You can use the client's IP or any identifier
    ACTIVE_CLIENTS[client_id] = asyncio.get_event_loop().time()  # Update last activity time
    return web.json_response({"status": "alive", "message": "Server is running"})


def web_server():
    web_app = web.Application(client_max_size=500)
    web_app.router.add_get('/', root_route_handler)
    web_app.add_routes(routes)
    web_app.add_subapp('/app', sub_app)
    web_app.add_subapp('/api', api)
    # Return the app to be used with AppRunner
    return web_app