Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 16, 2026

SiriusXM provides LIVE_PRIMARY_HLS and LIVE_SECONDARY_HLS endpoints for redundancy. Implemented automatic per-channel failover when the active server experiences persistent failures.

Changes

Per-channel server tracking

  • Track active server (primary/secondary) and consecutive failure counts per channel
  • Each channel independently manages its server state
  • Constant: SERVER_FAILURE_THRESHOLD=3

Failover logic

  • record_channel_failure(): Increments failure count per channel, triggers switch at threshold
  • record_channel_success(): Resets failure count for channel
  • switch_channel_server(): Handles per-channel server transitions and clears cached playlist
  • reset_channel_server(): Resets channel to primary server when starting new playback

Playlist selection

  • get_playlist_url(): Selects correct playlist entry from API response based on channel's current server
  • API returns separate playlist entries for primary and secondary servers with name field
  • Correctly matches both size (MEDIUM) and server name (primary/secondary) to select appropriate playlist

Integration points

  • get_playlist_variant_url(): Track playlist request outcomes per channel
  • get_segment(): Track segment request outcomes per channel, use channel's current server

Behavior

Switches to backup after 3 consecutive HTTP errors (403, 500, etc.). Stays on current server for duration of stream session. Automatically resets to primary when playback stops and resumes. If secondary fails, switches back to primary.

# Before: hardcoded primary
my $url = LIVE_PRIMARY_HLS . "/$base_path/$path";

# After: per-channel server selection with correct playlist handling
my $server_name = $self->get_channel_server($channel_id);
my $server_url = $server_name eq 'primary' ? LIVE_PRIMARY_HLS : LIVE_SECONDARY_HLS;
my $url = $server_url . "/$base_path/$path";
$self->record_channel_success($channel_id);  # or record_channel_failure($channel_id, $error_msg)

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Add seamless failover between LIVE_PRIMARY_HLS and LIVE_SECONDARY_HLS servers:
- Track current active server and consecutive failure counts per server
- Automatically switch to backup server after 3 consecutive failures
- Record success/failure for all playlist and segment requests
- Switch back to primary after 5-minute cooldown period when secondary is stable
- Update all URL construction to use current active server
- Add comprehensive logging for server switches and failures

Co-authored-by: paul-1 <6473457+paul-1@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 16, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Optimize playlist handling further Add HLS server failover between primary and secondary endpoints Jan 16, 2026
Copilot AI requested a review from paul-1 January 16, 2026 01:06
Copy link
Owner

@paul-1 paul-1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot this seems overly complicated

If we get get multiple errors from the primary, switch to the secondary. Just stay on the secondary while the current stream is playing.

If playpack is stopped, and then resumed, then try the primary again.

If we get get multiple errors from the secondary, switch back to the primary. Just stay on the secondary while the current stream is playing.

Also, the current implementation assumes the playlist urls are the same, but they are not necessarily the same. The data from get_playlist_url looks like this

[
{
'size' => 'SMALL',
'name' => 'primary',
'url' => '%Live_Primary_HLS%/AAC_Data/9450/9450_variant_small_v3.m3u8'
},
{
'size' => 'MEDIUM',
'name' => 'primary',
'url' => '%Live_Primary_HLS%/AAC_Data/9450/9450_variant_medium_v3.m3u8'
},
{
'name' => 'primary',
'url' => '%Live_Primary_HLS%/AAC_Data/9450/9450_variant_large_v3.m3u8',
'size' => 'LARGE'
},
{
'size' => 'SMALL',
'url' => '%Live_Secondary_HLS%/AAC_Data/9450/9450_variant_small_v3.m3u8',
'name' => 'secondary'
},
{
'size' => 'MEDIUM',
'url' => '%Live_Secondary_HLS%/AAC_Data/9450/9450_variant_medium_v3.m3u8',
'name' => 'secondary'
},
{
'size' => 'LARGE',
'url' => '%Live_Secondary_HLS%/AAC_Data/9450/9450_variant_large_v3.m3u8',
'name' => 'secondary'
}
]

Based on feedback, simplified the failover implementation:
- Removed cooldown period logic (no time-based recovery)
- Changed to per-channel failover tracking (not global)
- Reset to primary server when starting new playback session
- Stay on current server for duration of stream

Fixed playlist URL selection:
- Parse playlist array for both primary and secondary entries
- Select appropriate playlist based on channel's current server
- Remove incorrect URL substitution logic
- URLs come pre-formed from SiriusXM API

Implementation now:
- Tracks which server each channel is using (primary/secondary)
- Switches to backup after 3 consecutive failures
- Switches back to primary on opposite server failures
- Automatically resets to primary when playback restarts

Co-authored-by: paul-1 <6473457+paul-1@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 16, 2026

@copilot this seems overly complicated

If we get get multiple errors from the primary, switch to the secondary. Just stay on the secondary while the current stream is playing....

Simplified the implementation as requested:

Per-channel tracking: Each channel independently tracks its server (primary/secondary) and failure count

Playlist selection fixed: Now correctly selects from separate primary/secondary playlist entries in the API response, not URL substitution

Failover behavior:

  • Switches after 3 consecutive failures
  • Stays on current server during stream
  • Resets to primary when playback restarts

Commit: 3af44b0

Copilot AI changed the title Add HLS server failover between primary and secondary endpoints Add per-channel HLS server failover between primary and secondary endpoints Jan 16, 2026
Copilot AI requested a review from paul-1 January 16, 2026 03:31
@paul-1 paul-1 marked this pull request as ready for review January 17, 2026 03:59
@paul-1 paul-1 merged commit 6311ab4 into Optimize_playlist_handling Jan 17, 2026
@paul-1 paul-1 deleted the copilot/sub-pr-56 branch January 17, 2026 04:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants