(user, client_id, media_id=None)
| 151 | |
| 152 | |
| 153 | def check_user_stream_limits(user, client_id, media_id=None): |
| 154 | # Check user stream limits |
| 155 | if user and user.stream_limit > 0: |
| 156 | logger.debug("[stream limits]" f"[{client_id}] User {user.username} (ID: {user.id}) is requesting a stream (stream_limit: {user.stream_limit})") |
| 157 | user_limit_settings = CoreSettings.get_user_limits_settings() |
| 158 | ignore_same_channel = user_limit_settings.get("ignore_same_channel_connections", False) |
| 159 | |
| 160 | active_connections = get_user_active_connections(user.id) |
| 161 | unique_channel_count = set([conn['media_id'] for conn in active_connections]) |
| 162 | user_stream_count = len(unique_channel_count) if ignore_same_channel else len(active_connections) |
| 163 | |
| 164 | logger.debug(f"[stream limits]" f"[{client_id}] User {user.username} currently has {len(active_connections)} active connections across {len(unique_channel_count)} unique channels (counting method: {'unique channels' if ignore_same_channel else 'total connections'})") |
| 165 | |
| 166 | # If ignore_same_channel is enabled and this request is for a live channel the user |
| 167 | # is already watching, allow it through without counting against the limit. |
| 168 | # VOD is excluded: connections aren't shared so multiple VOD connections to the |
| 169 | # same content would mean multiple upstream connections. |
| 170 | live_channel_ids = {str(conn['media_id']) for conn in active_connections if conn['type'] == 'live'} |
| 171 | if ignore_same_channel and media_id and str(media_id) in live_channel_ids: |
| 172 | logger.debug(f"[stream limits][{client_id}] Same-channel reconnect for {media_id} allowed (ignore_same_channel=True)") |
| 173 | return True |
| 174 | |
| 175 | if user_stream_count >= user.stream_limit: |
| 176 | if user_limit_settings.get("terminate_on_limit_exceeded", True) == False: |
| 177 | return False |
| 178 | |
| 179 | if user_stream_count >= user.stream_limit: |
| 180 | logger.warning("[stream limits]" |
| 181 | f"[{client_id}] User {user.username} (ID: {user.id}) has reached stream limit " |
| 182 | f"({user_stream_count}/{user.stream_limit} streams), attempting to free up slot" |
| 183 | ) |
| 184 | |
| 185 | if not attempt_stream_termination(user.id, client_id, active_connections): |
| 186 | return False |
| 187 | |
| 188 | return True |
no test coverage detected