| 1 | package Langertha::Knarr::Role::UpstreamHTTP; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Timed Net::Async::HTTP client for handlers that call an upstream | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 80 | our $VERSION = '1.102'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 80 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 80 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 80 | ▶ | use Net::Async::HTTP; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 80 | ▶ | use IO::Async::Loop; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 80 | has timeout => ( is => 'ro', isa => 'Num', default => 300 ); | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 80 | has stall_timeout => ( is => 'ro', isa => 'Num', default => 120 ); | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | has loop => ( | ||||||||||||||||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | default => sub { IO::Async::Loop->new }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 80 | ); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 80 | has _http => ( is => 'ro', lazy => 1, builder => '_build_http' ); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 38 | ▶ | sub _build_http { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_http: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 38 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 38 | my $h = Net::Async::HTTP->new; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 38 | $self->loop->add($h); | |||||||||||||||||||||||||||||||||||||||||
| 25 | 38 | return $h; | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 118 | ▶ | sub is_upstream_timeout { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_upstream_timeout: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 118 | my ($class, $category) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 118 | ▶ | return defined $category && ( $category eq 'timeout' || $category eq 'stall_timeout' ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $category and $category eq "timeout" || $category eq "stall_timeout"
$category eq "timeout" or $category eq "stall_timeout"
MC/DC100%3/3
defined $category and $category eq "timeout" || $category eq "stall_timeout"
defined $category
$category eq "timeout"
$category eq "stall_timeout"
Truth table100%4/4
A and B or C
A= defined $categoryB= $category eq "timeout"C= $category eq "stall_timeout"
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # Net::Async::HTTP treats a timeout of 0 as "expire now", so a disabled | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # timeout is left out rather than passed. The URL in the failure drops its | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # query and userinfo, where a key could sit. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 328 | ▶ | sub _upstream_request_f { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_upstream_request_f: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 328 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 328 | my $stream = delete $args{stream}; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 328 | ▶ | $stream = 1 if $args{on_header}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 328 | ▶ | my ($option, $secs) = $stream | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | ? ( stall_timeout => $self->stall_timeout ) | ||||||||||||||||||||||||||||||||||||||||||
| 42 | : ( timeout => $self->timeout ); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 328 | ▶ | return $self->_http->do_request(%args) unless $secs && $secs > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$secs and $secs > 0
MC/DC50%1/2
$secs and $secs > 0
$secs
$secs > 0
Truth table66%2/3
A and B
A= $secsB= $secs > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 326 | my $uri = $args{request}->uri->clone; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 326 | $uri->query(undef); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 326 | $uri->fragment(undef); | |||||||||||||||||||||||||||||||||||||||||
| 48 | 326 | ▶ | $uri->userinfo(undef) if $uri->can('userinfo'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 326 | my $what = ref($self) . ": upstream $uri"; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 43 | ▶ | return $self->_http->do_request( %args, $option => $secs )->else( sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 43 | my ($message, $category, @details) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 43 | ▶ | return Future->fail(@_) unless __PACKAGE__->is_upstream_timeout($category); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 41 | ▶ | my $text = $category eq 'timeout' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | ? "$what timed out after ${secs}s" | ||||||||||||||||||||||||||||||||||||||||||
| 56 | : "$what timed out after ${secs}s without data"; | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 41 | return Future->fail( "$text\n", $category, @details ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 326 | }); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 80 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | Langertha::Knarr::Role::UpstreamHTTP - Timed Net::Async::HTTP client for handlers that call an upstream | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | version 1.102 | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | package My::Handler; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | with 'Langertha::Knarr::Handler', 'Langertha::Knarr::Role::UpstreamHTTP'; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | my $resp = await $self->_upstream_request_f( request => $http_req ); | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # streaming: stall_timeout instead of a total timeout | ||||||||||||||||||||||||||||||||||||||||||
| 85 | await $self->_upstream_request_f( request => $http_req, on_header => sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | The HTTP client of the handlers that call an upstream themselves | ||||||||||||||||||||||||||||||||||||||||||
| 90 | (L<Langertha::Knarr::Handler::Passthrough>, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | L<Langertha::Knarr::Handler::A2AClient>, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | L<Langertha::Knarr::Handler::ACPClient>) and the timeouts it applies. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | L<Net::Async::HTTP> sets none of its own, so an upstream that accepts the | ||||||||||||||||||||||||||||||||||||||||||
| 94 | connection and never answers would hold the client's request open forever. | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | A plain request gets L</timeout> as its total time. A streaming request | ||||||||||||||||||||||||||||||||||||||||||
| 97 | (C<on_header>, or C<stream =E<gt> 1> for a stream read whole) gets | ||||||||||||||||||||||||||||||||||||||||||
| 98 | L</stall_timeout> instead: the time the upstream may stay silent, since a | ||||||||||||||||||||||||||||||||||||||||||
| 99 | long steady stream is legitimate. On expiry the future fails with | ||||||||||||||||||||||||||||||||||||||||||
| 100 | C<< <class>: upstream <url> timed out after Ns >> and the category C<timeout> | ||||||||||||||||||||||||||||||||||||||||||
| 101 | or C<stall_timeout>, which L</is_upstream_timeout> recognizes. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =head2 timeout | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | Seconds a non-streaming upstream request may take in total. Default C<300>; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | C<0> disables it. Set from C<upstream_timeout> / | ||||||||||||||||||||||||||||||||||||||||||
| 107 | C<KNARR_UPSTREAM_TIMEOUT> by C<knarr start>. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =head2 stall_timeout | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | Seconds a streaming upstream request may go without receiving a byte, | ||||||||||||||||||||||||||||||||||||||||||
| 112 | waiting for the response headers included. Default C<120>; C<0> disables | ||||||||||||||||||||||||||||||||||||||||||
| 113 | it. Set from C<upstream_stall_timeout> / C<KNARR_UPSTREAM_STALL_TIMEOUT> by | ||||||||||||||||||||||||||||||||||||||||||
| 114 | C<knarr start>. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | =head2 loop | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | Optional L<IO::Async::Loop>. Defaults to C<< IO::Async::Loop->new >>. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | =head2 is_upstream_timeout | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | my $is_timeout = Langertha::Knarr::Role::UpstreamHTTP->is_upstream_timeout($category); | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | True when a failure category from L<Net::Async::HTTP> is one of its | ||||||||||||||||||||||||||||||||||||||||||
| 125 | timeouts (C<timeout>, C<stall_timeout>). | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||
| 132 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 151 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||||||||||||||||||||||||