| 1 | package Langertha::Knarr::Role::UpstreamHTTP; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Timed Net::Async::HTTP client for handlers that call an upstream | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 34 | our $VERSION = '1.103'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 34 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 34 | ▶ | use Net::Async::HTTP; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 34 | ▶ | use IO::Async::Loop; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 34 | has timeout => ( is => 'ro', isa => 'Num', default => 300 ); | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 34 | 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 | 34 | ); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 34 | has _http => ( is => 'ro', lazy => 1, builder => '_build_http' ); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 9 | ▶ | sub _build_http { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_http: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | my $h = Net::Async::HTTP->new; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 9 | $self->loop->add($h); | |||||||||||||||||||||||||||||||||||||||||
| 25 | 9 | return $h; | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub is_upstream_timeout { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_upstream_timeout: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my ($class, $category) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | return defined $category && ( $category eq 'timeout' || $category eq 'stall_timeout' ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $category and $category eq "timeout" || $category eq "stall_timeout"
$category eq "timeout" or $category eq "stall_timeout"
MC/DC0%0/3
defined $category and $category eq "timeout" || $category eq "stall_timeout"
defined $category
$category eq "timeout"
$category eq "stall_timeout"
Truth table0%0/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 | 9 | ▶ | sub _upstream_request_f { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_upstream_request_f: called CC=6 SCAR=22.7
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 9 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 9 | my $stream = delete $args{stream}; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 9 | ▶ | $stream = 1 if $args{on_header}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 9 | ▶ | my ($option, $secs) = $stream | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | ? ( stall_timeout => $self->stall_timeout ) | ||||||||||||||||||||||||||||||||||||||||||
| 42 | : ( timeout => $self->timeout ); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 9 | ▶ | return $self->_http->do_request(%args) unless $secs && $secs > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$secs and $secs > 0
MC/DC0%0/2
$secs and $secs > 0
$secs
$secs > 0
Truth table33%1/3
A and B
A= $secsB= $secs > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 9 | my $uri = $args{request}->uri->clone; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 9 | $uri->query(undef); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | $uri->fragment(undef); | |||||||||||||||||||||||||||||||||||||||||
| 48 | 9 | ▶ | $uri->userinfo(undef) if $uri->can('userinfo'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 9 | my $what = ref($self) . ": upstream $uri"; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | return $self->_http->do_request( %args, $option => $secs )->else( sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my ($message, $category, @details) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return Future->fail(@_) unless __PACKAGE__->is_upstream_timeout($category); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | 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 | 0 | return Future->fail( "$text\n", $category, @details ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 9 | }); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 34 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | __END__ | ||||||||||||||||||||||||||||||||||||||||||