Skip to content

Commit 06c3a4e

Browse files
Copilotpaul-1
andauthored
Add HTTP keep-alive and single-retry wrapper for SiriusXM upstream requests
Agent-Logs-Url: https://github.com/paul-1/plugin-SiriusXM/sessions/6d6ed5c0-124f-44a1-897f-00cc4a3a2ea6 Co-authored-by: paul-1 <6473457+paul-1@users.noreply.github.com>
1 parent 7c52dcb commit 06c3a4e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Plugins/SiriusXM/Bin/sxm.pl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ sub new {
502502
agent => USER_AGENT,
503503
cookie_jar => $cookie_jar,
504504
timeout => 30,
505+
keep_alive => 1, # reuse TCP connections; server may still close idle sockets
505506
);
506507

507508
# Analyze and log cookie information now that $self->{ua} is ready.
@@ -1043,7 +1044,7 @@ sub make_channel_request {
10431044
unless (defined $channel_id) {
10441045
# Global request: use the UA with its global jar unchanged
10451046
main::log_trace("Global request via UA global jar: " . $request->uri);
1046-
return $self->{ua}->request($request);
1047+
return $self->_ua_request_with_retry($request);
10471048
}
10481049

10491050
# Channel request: merge cookies manually, bypass LWP auto-cookie handling
@@ -1056,7 +1057,7 @@ sub make_channel_request {
10561057
my $saved_jar = $self->{ua}->cookie_jar;
10571058
$self->{ua}->cookie_jar(HTTP::Cookies->new());
10581059

1059-
my $response = $self->{ua}->request($request);
1060+
my $response = $self->_ua_request_with_retry($request);
10601061

10611062
# Restore the global jar immediately after the request
10621063
$self->{ua}->cookie_jar($saved_jar);
@@ -1067,6 +1068,24 @@ sub make_channel_request {
10671068
return $response;
10681069
}
10691070

1071+
# Wrap $ua->request with a single retry on connection-drop style failures.
1072+
# LWP will automatically open a fresh TCP connection on the retry; keep-alive
1073+
# is best-effort and the server may close idle sockets at any time.
1074+
sub _ua_request_with_retry {
1075+
my ($self, $request) = @_;
1076+
1077+
my $response = $self->{ua}->request($request);
1078+
return $response if $response->is_success;
1079+
1080+
my $status = $response->status_line // '';
1081+
if ($status =~ /(?:timeout|read timed out|connection reset|broken pipe|closed|EOF)/i) {
1082+
main::log_warn("Connection issue ($status) - retrying once with a new TCP connection");
1083+
$response = $self->{ua}->request($request);
1084+
}
1085+
1086+
return $response;
1087+
}
1088+
10701089
# Log the names (never values) of all cookies in $jar.
10711090
# Intended for DEBUG/TRACE diagnostics only.
10721091
sub log_jar_cookie_names {

0 commit comments

Comments
 (0)