refactor: replace multiple hget calls with hmget where fields are known

- channel_status.py: fold output_format and output_profile_id into the
  existing hmget, reducing per-client Redis calls from 3 to 1
- utils.py: collapse 2x and 3x hget per key in scan_iter loops
  (get_user_active_connections) to single hmget calls
- views.py: replace 3x hget on channel metadata in the worker-join path
  of stream_ts with a single hmget
This commit is contained in:
Jonathan Caicedo 2026-05-20 22:12:09 -04:00
parent e2074c1921
commit 6787e4912f
3 changed files with 16 additions and 19 deletions

View file

@ -463,13 +463,18 @@ class ChannelStatus:
# Fetch only the fields we need in one round-trip (hmget returns a list
# in the same order as the requested keys; values are None if absent)
ua, ip, connected_at, user_id = proxy_server.redis_client.hmget(
client_key, 'user_agent', 'ip_address', 'connected_at', 'user_id'
ua, ip, connected_at, user_id, output_format, raw_profile_id = (
proxy_server.redis_client.hmget(
client_key,
'user_agent', 'ip_address', 'connected_at', 'user_id',
'output_format', 'output_profile_id',
)
)
client_info = {
'client_id': client_id,
'user_agent': ua,
'output_format': output_format or 'mpegts',
}
if ip:
@ -481,10 +486,6 @@ class ChannelStatus:
if user_id:
client_info['user_id'] = user_id
output_format = proxy_server.redis_client.hget(client_key, 'output_format')
client_info['output_format'] = output_format or 'mpegts'
raw_profile_id = proxy_server.redis_client.hget(client_key, 'output_profile_id')
if raw_profile_id and raw_profile_id not in ('None', '0', ''):
client_info['output_profile_id'] = int(raw_profile_id)
else:

View file

@ -468,14 +468,11 @@ def stream_ts(request, channel_id, user=None, force_output_format=None):
if proxy_server.redis_client:
metadata_key = RedisKeys.channel_metadata(channel_id)
url_bytes = proxy_server.redis_client.hget(
metadata_key, ChannelMetadataField.URL
)
ua_bytes = proxy_server.redis_client.hget(
metadata_key, ChannelMetadataField.USER_AGENT
)
profile_bytes = proxy_server.redis_client.hget(
metadata_key, ChannelMetadataField.STREAM_PROFILE
url_bytes, ua_bytes, profile_bytes = proxy_server.redis_client.hmget(
metadata_key,
ChannelMetadataField.URL,
ChannelMetadataField.USER_AGENT,
ChannelMetadataField.STREAM_PROFILE,
)
if url_bytes:

View file

@ -98,8 +98,7 @@ def get_user_active_connections(user_id):
channel_id = parts[2]
client_id = parts[4]
client_user_id = redis_client.hget(key, 'user_id')
connected_at = redis_client.hget(key, 'connected_at')
client_user_id, connected_at = redis_client.hmget(key, 'user_id', 'connected_at')
logger.debug(f"[stream limits] user_id = {user_id}")
logger.debug(f"[stream limits] channel_id = {channel_id}")
@ -124,9 +123,9 @@ def get_user_active_connections(user_id):
if len(parts) >= 2:
client_id = parts[1]
client_user_id = redis_client.hget(key, 'user_id')
connected_at = redis_client.hget(key, 'created_at')
content_uuid = redis_client.hget(key, 'content_uuid')
client_user_id, connected_at, content_uuid = redis_client.hmget(
key, 'user_id', 'created_at', 'content_uuid'
)
logger.debug(f"[stream limits] user_id = {user_id}")
logger.debug(f"[stream limits] client_id = {client_id}")