Skip to content

Commit

Permalink
Tidying up stats handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtlmoon committed Sep 28, 2024
1 parent 003949e commit 1704d20
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
import time
import websockets

stats = {'connection_count': 0, 'connection_count_total': 0, 'confirmed_data_received': 0, 'special_counter':[]}
stats = {'connection_count': 0,
'connection_count_total': 0,
'confirmed_data_received': 0,
'special_counter': [],
'dropped_waited_too_long': 0,
'dropped_threshold_reached': 0,
}
connection_count_max = int(os.getenv('MAX_CONCURRENT_CHROME_PROCESSES', 10))
port_selector = PortSelector()
shutdown = False
Expand Down Expand Up @@ -225,13 +231,10 @@ async def launchPuppeteerChromeProxy(websocket, path):
closed.add_done_callback(lambda task: asyncio.ensure_future(stats_disconnect(time_at_start=now, websocket=websocket)))

svmem = psutil.virtual_memory()

stats['connection_count_total'] += 1
logger.debug(
f"WebSocket ID: {websocket.id} Got new incoming connection ID from {websocket.remote_address[0]}:{websocket.remote_address[1]} ({path})")

stats['connection_count'] += 1
stats['connection_count_total'] += 1

if stats['connection_count'] > connection_count_max:
logger.warning(
f"WebSocket ID: {websocket.id} - Throttling/waiting, max connection limit reached {stats['connection_count']} of max {connection_count_max} ({time.time() - now:.1f}s)")
Expand All @@ -244,6 +247,7 @@ async def launchPuppeteerChromeProxy(websocket, path):
logger.critical(
f"WebSocket ID: {websocket.id} - Too long waiting for memory usage to drop, dropping connection. {svmem.percent}% was > {memory_use_limit_percent}% ({time.time() - now:.1f}s)")
await close_socket(websocket)
stats['dropped_threshold_reached'] += 1
return

# Connections that joined but had to wait a long time before being processed
Expand All @@ -254,8 +258,11 @@ async def launchPuppeteerChromeProxy(websocket, path):
logger.critical(
f"WebSocket ID: {websocket.id} - Waiting for existing connection count to drop took too long! dropping connection. ({time.time() - now:.1f}s)")
await close_socket(websocket)
stats['dropped_waited_too_long'] += 1
return

stats['connection_count'] += 1

now_before_chrome_launch = time.time()

port = next(port_selector)
Expand Down

0 comments on commit 1704d20

Please sign in to comment.