| 1 | package Discord::Client::REST::RateLimiter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use v5.26; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use JSON::PP (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 22 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # Internal scheduler. One request per bucket avoids speculative quota use and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # out-of-order updates; unrelated buckets can still make progress concurrently. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 148 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 148 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # Keep route aliases, quota state and pending jobs local to this REST scheduler. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 148 | return bless { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | %args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | jobs => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | routes => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | buckets => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | active => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | global_until => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | stopped => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | counters => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 151 | ▶ | sub submit { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
submit: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 151 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # Reject work before allocating queue or bucket capacity. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 151 | my $future = $self->{loop}->new_future; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 151 | ▶ | return $future->fail( 'REST client is closed', 'shutdown' ) if $self->{stopped}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | return $future->fail( 'Bot authentication is invalid', 'authentication' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 150 | ▶ | if $self->{auth_failed} && $args{auth}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"auth_failed"} and $args{"auth"}
MC/DC100%2/2
$self->{"auth_failed"} and $args{"auth"}
$self->{"auth_failed"}
$args{"auth"}
Truth table100%3/3
A and B
A= $self->{"auth_failed"}B= $args{"auth"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | return $future->fail( 'REST request capacity exceeded', 'queue_full' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 149 | ▶ | if @{ $self->{jobs} } >= $self->{max_pending}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # Reclaim only inactive, expired route entries before admitting a new route. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 148 | $self->_prune; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 148 | ▶ | if ( !exists $self->{routes}{ $args{route} } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | return $future->fail( 'REST bucket capacity exceeded', 'queue_full' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 128 | ▶ | if keys( %{ $self->{routes} } ) >= $self->{max_buckets}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 126 | $self->{routes}{ $args{route} } = { active => 0, blocked => 0 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # Keep a request in its queue position across rate-limit retries. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 146 | my $job = { %args, future => $future, retries => 0 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 146 | push @{ $self->{jobs} }, $job; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 146 | $self->_emit('request_queued'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # Release scheduling capacity before cancelling transport work that may call back. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | $future->on_cancel( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 10 | $self->_remove($job); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | $self->_emit('request_cancelled'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | my $transport_future = delete $job->{transport_future}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 10 | ▶ | $transport_future->cancel if $transport_future && !$transport_future->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$transport_future and not $transport_future->is_ready
MC/DC50%1/2
$transport_future and not $transport_future->is_ready
$transport_future
not $transport_future->is_ready
Truth table66%2/3
A and not(B)
A= $transport_futureB= not $transport_future->is_ready
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 10 | $self->_pump; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 146 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 146 | $self->_pump; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 146 | return $future; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 641 | ▶ | sub _root { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_root: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 641 | my ($bucket) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # Merged routes share one bucket; compress parent chains while resolving it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 641 | my @parents; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 641 | ▶ | while ( $bucket->{parent} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | push @parents, $bucket; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | $bucket = $bucket->{parent}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 641 | $_->{parent} = $bucket for @parents; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 641 | return $bucket; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 148 | ▶ | sub _prune { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prune: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 148 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 148 | ▶ | return if keys( %{ $self->{routes} } ) < $self->{max_buckets}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # Active requests and unexpired limits must survive capacity-driven eviction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | my %used = map { $_->{route} => 1 } @{ $self->{jobs} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | my $now = $self->{clock}->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | for my $route ( keys %{ $self->{routes} } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | my $bucket = _root( $self->{routes}{$route} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | if $used{$route} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | || $bucket->{active} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | || $bucket->{blocked} > $now | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | ▶ | || ( $bucket->{reset} // 0 ) > $now; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition72%8/11
$used{$route} or $bucket->{"active"} or $bucket->{"blocked"} > $now or ($bucket->{"reset"} // 0) > $now
$used{$route} or $bucket->{"active"} or $bucket->{"blocked"} > $now
$used{$route} or $bucket->{"active"}
$bucket->{"reset"} // 0
MC/DC0%0/4
$used{$route} or $bucket->{"active"} or $bucket->{"blocked"} > $now or ($bucket->{"reset"} // 0) > $now
$used{$route}
$bucket->{"active"}
$bucket->{"blocked"} > $now
$bucket->{"reset"}
Truth table40%2/5
A or B or C or D
A= $used{$route}B= $bucket->{"active"}C= $bucket->{"blocked"} > $nowD= $bucket->{"reset"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | delete $self->{routes}{$route}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # Discard server bucket aliases only when no surviving route refers to them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | my %live = map { _root($_) => 1 } values %{ $self->{routes} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | for my $key ( keys %{ $self->{buckets} } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | delete $self->{buckets}{$key} unless $live{ _root( $self->{buckets}{$key} ) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 309 | ▶ | sub _pump { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pump: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 309 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 309 | ▶ | return if $self->{pumping} || $self->{stopped}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"pumping"} or $self->{"stopped"}
MC/DC50%1/2
$self->{"pumping"} or $self->{"stopped"}
$self->{"pumping"}
$self->{"stopped"}
Truth table66%2/3
A or B
A= $self->{"pumping"}B= $self->{"stopped"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # Completion callbacks can re-enter scheduling; the outer pump owns progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 308 | local $self->{pumping} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 308 | ▶ | if ( my $timer = delete $self->{timer} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 12 | ▶ | $timer->cancel unless $timer->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 308 | my $earliest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # Restart after each send: mock transports may finish synchronously and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # callers may enqueue or cancel from completion callbacks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 308 | ▶ | while ( $self->{active} < $self->{max_in_flight} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 449 | my $candidate; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 449 | ( $candidate, $earliest ) = $self->_next_ready_job( $self->{clock}->() ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 449 | ▶ | last unless $candidate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 150 | $self->_send($candidate); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # One timer wakes the scheduler at the earliest known bucket or global deadline. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 308 | ▶ | if ( defined $earliest && !$self->{stopped} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $earliest and not $self->{"stopped"}
MC/DC50%1/2
defined $earliest and not $self->{"stopped"}
defined $earliest
not $self->{"stopped"}
Truth table66%2/3
A and not(B)
A= defined $earliestB= not $self->{"stopped"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 25 | my $delay = $earliest - $self->{clock}->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 25 | ▶ | $delay = 0 if $delay < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 25 | my $timer = $self->{delay}->($delay); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 25 | $self->{timer} = $timer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | $timer->on_done( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 12 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 12 | delete $self->{timer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 12 | $self->_pump; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 25 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 449 | ▶ | sub _next_ready_job { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_next_ready_job: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 449 | my ( $self, $now ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 449 | my $earliest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # Keep FIFO order within each bucket while allowing unrelated buckets to progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # Active buckets wake the pump on completion; timed limits contribute a wake-up deadline. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 449 | for my $job ( @{ $self->{jobs} } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 363 | ▶ | next if $job->{active}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 189 | my $bucket = _root( $self->{routes}{ $job->{route} } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 189 | ▶ | next if $bucket->{active}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 181 | my $available_at = $self->_bucket_available_at( $bucket, $job->{global_scope}, $now ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 181 | ▶ | return ( $job, $earliest ) if $available_at <= $now; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 31 | ▶ | $earliest = $available_at if !defined($earliest) || $available_at < $earliest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $earliest or $available_at < $earliest
MC/DC50%1/2
not defined $earliest or $available_at < $earliest
not defined $earliest
$available_at < $earliest
Truth table66%2/3
not(A) or B
A= not defined $earliestB= $available_at < $earliest
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 299 | return ( undef, $earliest ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 181 | ▶ | sub _bucket_available_at { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bucket_available_at: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 181 | my ( $self, $bucket, $global_scope, $now ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # An expired window invalidates its remaining-quota estimate. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 181 | ▶ | if ( defined( $bucket->{reset} ) && $bucket->{reset} <= $now ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $bucket->{"reset"} and $bucket->{"reset"} <= $now
MC/DC100%2/2
defined $bucket->{"reset"} and $bucket->{"reset"} <= $now
defined $bucket->{"reset"}
$bucket->{"reset"} <= $now
Truth table100%3/3
A and B
A= defined $bucket->{"reset"}B= $bucket->{"reset"} <= $now
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 4 | delete @{$bucket}{qw(reset remaining)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # Every applicable limit must have elapsed before the request may start. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 181 | my $available_at = $bucket->{blocked}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 181 | ▶ | my $global_reset_at = $self->{global_until}{$global_scope} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"global_until"}{$global_scope} // 0
MC/DC100%1/1
$self->{"global_until"}{$global_scope} // 0
$self->{"global_until"}{$global_scope}
Truth table100%2/2
A
A= $self->{"global_until"}{$global_scope}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 181 | ▶ | $available_at = $global_reset_at if $global_reset_at > $available_at; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 181 | ▶ | if ( defined( $bucket->{remaining} ) && $bucket->{remaining} <= 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $bucket->{"remaining"} and $bucket->{"remaining"} <= 0
MC/DC100%2/2
defined $bucket->{"remaining"} and $bucket->{"remaining"} <= 0
defined $bucket->{"remaining"}
$bucket->{"remaining"} <= 0
Truth table100%3/3
A and B
A= defined $bucket->{"remaining"}B= $bucket->{"remaining"} <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # Without a reset time, permit a probe instead of blocking the route forever. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | $available_at = $bucket->{reset} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 9 | ▶ | if defined( $bucket->{reset} ) && $bucket->{reset} > $available_at; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bucket->{"reset"} and $bucket->{"reset"} > $available_at
MC/DC50%1/2
defined $bucket->{"reset"} and $bucket->{"reset"} > $available_at
defined $bucket->{"reset"}
$bucket->{"reset"} > $available_at
Truth table66%2/3
A and B
A= defined $bucket->{"reset"}B= $bucket->{"reset"} > $available_at
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 181 | return $available_at; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 150 | ▶ | sub _send { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_send: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 150 | my ( $self, $job ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # Reserve both bucket and client capacity before invoking the transport. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 150 | my $bucket = _root( $self->{routes}{ $job->{route} } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 150 | $job->{bucket} = $bucket; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 150 | $job->{active} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 150 | ++$bucket->{active}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 150 | ++$self->{active}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 150 | $self->_emit('http_started'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 150 | ▶ | return if $job->{removed} || $self->{stopped}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$job->{"removed"} or $self->{"stopped"}
MC/DC0%0/2
$job->{"removed"} or $self->{"stopped"}
$job->{"removed"}
$self->{"stopped"}
Truth table33%1/3
A or B
A= $job->{"removed"}B= $self->{"stopped"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 150 | ▶ | --$bucket->{remaining} if defined( $bucket->{remaining} ) && $bucket->{remaining} > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $bucket->{"remaining"} and $bucket->{"remaining"} > 0
MC/DC100%2/2
defined $bucket->{"remaining"} and $bucket->{"remaining"} > 0
defined $bucket->{"remaining"}
$bucket->{"remaining"} > 0
Truth table100%3/3
A and B
A= defined $bucket->{"remaining"}B= $bucket->{"remaining"} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # A transport exception is an uncertain failure and must not trigger a retry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 150 | my $transport_future = eval { $job->{send}->() }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 150 | ▶ | if ( !$transport_future || !eval { $transport_future->isa('Future') } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$transport_future and eval {
do {
$transport_future->isa("Future")
}
}
MC/DC0%0/2
$transport_future and eval {
do {
$transport_future->isa("Future")
}
}
$transport_future
eval {
do {
$transport_future->isa("Future")
}
}
Truth table33%1/3
A and B
A= $transport_futureB= eval {
do {
$transport_future->isa("Future")
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | $self->_finish( $job, 'HTTP transport failed', 'transport' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 150 | $job->{transport_future} = $transport_future; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # Release the active slot before processing quota updates or notifying callers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | $transport_future->on_ready( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 150 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 150 | delete $job->{transport_future}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 150 | ▶ | return if $job->{removed}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 141 | $self->_release($job); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 141 | $self->_complete_transport( $job, $transport_future ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 141 | $self->_pump; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 150 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 141 | ▶ | sub _complete_transport { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_complete_transport: called CC=9 SCAR=22.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 141 | my ( $self, $job, $transport_future ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # Cancellation and transport failures are uncertain deliveries, not retry opportunities. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 141 | ▶ | return $self->_finish( $job, 'HTTP transport was cancelled', 'transport' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | if $transport_future->is_cancelled; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 141 | ▶ | if ( $transport_future->is_failed ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 8 | my ( undef, $category, $response ) = $transport_future->failure; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # Injected clients may fail on non-2xx HTTP responses. Preserve those responses | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # so authentication and 429 handling still pass through the normal response path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | return $self->_response( $job, $response ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 8 | ▶ | if ref($response) && eval { $response->isa('HTTP::Response') }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $response and eval {
do {
$response->isa("HTTP::Response")
}
}
MC/DC50%1/2
ref $response and eval {
do {
$response->isa("HTTP::Response")
}
}
ref $response
eval {
do {
$response->isa("HTTP::Response")
}
}
Truth table66%2/3
A and B
A= ref $responseB= eval {
do {
$response->isa("HTTP::Response")
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 7 | ▶ | my $failure_category = ( $category // '' ) =~ /\A(?:timeout|stall_timeout)\z/ ? 'timeout' : 'transport'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$category // ""
MC/DC100%1/1
$category // ""
$category
Truth table100%2/2
A
A= $category
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 7 | return $self->_finish( $job, 'HTTP request failed', $failure_category ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # A successful transport Future must still contain the expected response object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 133 | my ($response) = $transport_future->result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | return $self->_finish( $job, 'Invalid HTTP transport response', 'transport' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 133 | ▶ | unless ref($response) && eval { $response->isa('HTTP::Response') }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $response and eval {
do {
$response->isa("HTTP::Response")
}
}
MC/DC0%0/2
ref $response and eval {
do {
$response->isa("HTTP::Response")
}
}
ref $response
eval {
do {
$response->isa("HTTP::Response")
}
}
Truth table33%1/3
A and B
A= ref $responseB= eval {
do {
$response->isa("HTTP::Response")
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 133 | return $self->_response( $job, $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 421 | ▶ | sub _number { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_number: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 421 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | # Accept nonnegative decimal header syntax without coercing arbitrary strings to numbers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 421 | ▶ | unless defined($value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $value and not ref $value and $value =~ /\A\d+(?:\.\d+)?\z/u
defined $value and not ref $value
MC/DC33%1/3
defined $value and not ref $value and $value =~ /\A\d+(?:\.\d+)?\z/u
defined $value
not ref $value
$value =~ /\A\d+(?:\.\d+)?\z/u
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A\d+(?:\.\d+)?\z/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | && !ref($value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | && $value =~ /\A\d+(?:\.\d+)?\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 33 | return 0 + $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 134 | ▶ | sub _response { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_response: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 134 | my ( $self, $job, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 134 | my $now = $self->{clock}->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 134 | $self->_emit( 'http_response', status => $response->code ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 134 | ▶ | return if $job->{removed} || $self->{stopped}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$job->{"removed"} or $self->{"stopped"}
MC/DC0%0/2
$job->{"removed"} or $self->{"stopped"}
$job->{"removed"}
$self->{"stopped"}
Truth table33%1/3
A or B
A= $job->{"removed"}B= $self->{"stopped"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | # Learn shared buckets and limits even when the HTTP status indicates a failure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 134 | my $bucket = $self->_response_bucket( $job, $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 134 | $self->_update_bucket_limits( $bucket, $response, $now ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # Authentication and rate-limit responses change scheduling, not just the result. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 134 | ▶ | if ( $response->code == 401 && $job->{auth} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$response->code == 401 and $job->{"auth"}
MC/DC100%2/2
$response->code == 401 and $job->{"auth"}
$response->code == 401
$job->{"auth"}
Truth table100%3/3
A and B
A= $response->code == 401B= $job->{"auth"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 3 | $self->_invalidate_authentication( $job, $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 131 | ▶ | if ( $response->code == 429 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 13 | $self->_handle_rate_limit( $job, $bucket, $response, $now ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 13 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 118 | $self->_finish( $job, undef, undef, $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 134 | ▶ | sub _response_bucket { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_response_bucket: called CC=15 SCAR=29.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 134 | my ( $self, $job, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 134 | my $bucket = _root( $self->{routes}{ $job->{route} } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # The server bucket hash is scoped by major resource IDs, including webhook tokens. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 134 | my $hash = $response->header('X-RateLimit-Bucket'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 134 | ▶ | return $bucket unless defined($hash) && length($hash); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $hash and length $hash
MC/DC50%1/2
defined $hash and length $hash
defined $hash
length $hash
Truth table66%2/3
A and B
A= defined $hashB= length $hash
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 10 | my $key = $hash . ':' . $job->{major}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 10 | my $known = $self->{buckets}{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # Merge learned aliases conservatively while other requests may still be active. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 10 | ▶ | if ( $known && _root($known) != $bucket ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$known and _root($known) != $bucket
MC/DC100%2/2
$known and _root($known) != $bucket
$known
_root($known) != $bucket
Truth table100%3/3
A and B
A= $knownB= _root($known) != $bucket
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | $known = _root($known); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 2 | $known->{active} += $bucket->{active}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 2 | for my $field (qw(blocked reset)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | $known->{$field} = $bucket->{$field} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | if defined( $bucket->{$field} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 4 | ▶ | && ( !defined( $known->{$field} ) || $bucket->{$field} > $known->{$field} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $bucket->{$field} and !defined($known->{$field}) || $bucket->{$field} > $known->{$field}
not defined $known->{$field} or $bucket->{$field} > $known->{$field}
MC/DC0%0/3
defined $bucket->{$field} and !defined($known->{$field}) || $bucket->{$field} > $known->{$field}
defined $bucket->{$field}
not defined $known->{$field}
$bucket->{$field} > $known->{$field}
Truth table50%2/4
A and not(B) or C
A= defined $bucket->{$field}B= not defined $known->{$field}C= $bucket->{$field} > $known->{$field}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 2 | ▶ | if ( defined $bucket->{remaining} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | $known->{remaining} = $bucket->{remaining} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | if !defined( $known->{remaining} ) || $bucket->{remaining} < $known->{remaining}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $known->{"remaining"} or $bucket->{"remaining"} < $known->{"remaining"}
MC/DC0%0/2
not defined $known->{"remaining"} or $bucket->{"remaining"} < $known->{"remaining"}
not defined $known->{"remaining"}
$bucket->{"remaining"} < $known->{"remaining"}
Truth table0%0/3
not(A) or B
A= not defined $known->{"remaining"}B= $bucket->{"remaining"} < $known->{"remaining"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 2 | $bucket->{parent} = $known; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 2 | $bucket = $known; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | $_ = _root($_) for values %{ $self->{routes} }, values %{ $self->{buckets} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | $self->{buckets}{$key} = $bucket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | if exists( $self->{buckets}{$key} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 10 | ▶ | || keys( %{ $self->{buckets} } ) < $self->{max_buckets}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $self->{"buckets"}{$key} or keys %{$$self{"buckets"};} < $self->{"max_buckets"}
MC/DC0%0/2
exists $self->{"buckets"}{$key} or keys %{$$self{"buckets"};} < $self->{"max_buckets"}
exists $self->{"buckets"}{$key}
keys %{$$self{"buckets"};} < $self->{"max_buckets"}
Truth table66%2/3
A or B
A= exists $self->{"buckets"}{$key}B= keys %{$$self{"buckets"};} < $self->{"max_buckets"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 10 | return $bucket; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 134 | ▶ | sub _update_bucket_limits { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update_bucket_limits: called CC=11 SCAR=24.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 134 | my ( $self, $bucket, $response, $now ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # An overlapping response must not increase quota already consumed by another request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 134 | my $remaining = _number( $response->header('X-RateLimit-Remaining') ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 134 | ▶ | if ( defined $remaining ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | $bucket->{remaining} = $remaining | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | if !$bucket->{active} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | || !defined( $bucket->{remaining} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 13 | ▶ | || $remaining < $bucket->{remaining}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not $bucket->{"active"} && defined $bucket->{"remaining"} or $remaining < $bucket->{"remaining"}
$bucket->{'active'} && defined $bucket->{'remaining'}
MC/DC33%1/3
not $bucket->{"active"} && defined $bucket->{"remaining"} or $remaining < $bucket->{"remaining"}
$bucket->{'active'}
defined $bucket->{'remaining'}
$remaining < $bucket->{"remaining"}
Truth table50%2/4
not(A && B) or C
A= $bucket->{'active'}B= defined $bucket->{'remaining'}C= $remaining < $bucket->{"remaining"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # Prefer relative reset times; translate epoch resets using the injected wall clock. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 134 | my $after = _number( $response->header('X-RateLimit-Reset-After') ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 134 | ▶ | if ( !defined $after ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 129 | my $epoch = _number( $response->header('X-RateLimit-Reset') ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 129 | ▶ | $after = $epoch - $self->{wall_clock}->() if defined $epoch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 134 | ▶ | if ( defined $after ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 6 | ▶ | $after = 0 if $after < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 6 | my $reset = $now + $after; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 6 | ▶ | $bucket->{reset} = $reset if !defined( $bucket->{reset} ) || $reset > $bucket->{reset}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $bucket->{"reset"} or $reset > $bucket->{"reset"}
MC/DC0%0/2
not defined $bucket->{"reset"} or $reset > $bucket->{"reset"}
not defined $bucket->{"reset"}
$reset > $bucket->{"reset"}
Truth table33%1/3
not(A) or B
A= not defined $bucket->{"reset"}B= $reset > $bucket->{"reset"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 3 | ▶ | sub _invalidate_authentication { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_invalidate_authentication: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 3 | my ( $self, $job, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # Fail queued Bot-authorized requests while leaving token-only requests independent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 3 | $self->{auth_failed} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 3 | $self->_finish( $job, undef, undef, $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 3 | my @queued = @{ $self->{jobs} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 3 | for my $queued (@queued) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 3 | ▶ | next if $queued->{active} || !$queued->{auth}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$queued->{"active"} or not $queued->{"auth"}
MC/DC50%1/2
$queued->{"active"} or not $queued->{"auth"}
$queued->{"active"}
not $queued->{"auth"}
Truth table66%2/3
A or not(B)
A= $queued->{"active"}B= not $queued->{"auth"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 2 | $self->_finish( $queued, 'Bot authentication is invalid', 'authentication' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 13 | ▶ | sub _handle_rate_limit { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_rate_limit: called CC=21 SCAR=31.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 13 | my ( $self, $job, $bucket, $response, $now ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 13 | ▶ | if ( $self->{auth_failed} && $job->{auth} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"auth_failed"} and $job->{"auth"}
MC/DC50%1/2
$self->{"auth_failed"} and $job->{"auth"}
$self->{"auth_failed"}
$job->{"auth"}
Truth table66%2/3
A and B
A= $self->{"auth_failed"}B= $job->{"auth"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 1 | $self->_finish( $job, 'Bot authentication is invalid', 'authentication', $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | # Honor the larger valid body/header delay rather than retrying too early. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 12 | my $data = eval { JSON::PP->new->utf8->decode( $response->content ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 12 | ▶ | $data = {} unless ref($data) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 12 | my $retry = _number( $data->{retry_after} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 12 | my $header = _number( $response->header('Retry-After') ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 12 | ▶ | $retry = $header if defined($header) && ( !defined($retry) || $header > $retry ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $header and !defined($retry) || $header > $retry
not defined $retry or $header > $retry
MC/DC33%1/3
defined $header and !defined($retry) || $header > $retry
defined $header
not defined $retry
$header > $retry
Truth table75%3/4
A and not(B) or C
A= defined $headerB= not defined $retryC= $header > $retry
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 12 | ▶ | if ( !defined($retry) || $retry <= 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $retry or $retry <= 0
MC/DC50%1/2
not defined $retry or $retry <= 0
not defined $retry
$retry <= 0
Truth table66%2/3
not(A) or B
A= not defined $retryB= $retry <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 1 | $self->_finish( $job, 'Rate limit response has no positive retry delay', 'rate_limit', $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # Global limits apply to the authentication scope; other limits block only this bucket. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 11 | my $until = $now + $retry; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | my $global = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | $data->{global} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 11 | ▶ | || ( $response->header('X-RateLimit-Global') // '' ) eq 'true' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition90%9/10
$data->{'global'} || ($response->header('X-RateLimit-Global') // '') eq 'true' || ($response->header('X-RateLimit-Scope') // '') eq 'global'
$data->{"global"} or ($response->header("X-RateLimit-Global") // "") eq "true"
$response->header("X-RateLimit-Global") // ""
$response->header("X-RateLimit-Scope") // ""
MC/DC100%3/3
$data->{'global'} || ($response->header('X-RateLimit-Global') // '') eq 'true' || ($response->header('X-RateLimit-Scope') // '') eq 'global'
$data->{"global"}
$response->header("X-RateLimit-Global")
$response->header("X-RateLimit-Scope")
Truth table100%4/4
A or B || C
A= $data->{"global"}B= $response->header("X-RateLimit-Global")C= $response->header("X-RateLimit-Scope")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | || ( $response->header('X-RateLimit-Scope') // '' ) eq 'global'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 11 | ▶ | $self->_emit( 'rate_limit', global => $global ? 1 : 0, retry_after => $retry ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 11 | ▶ | return if $job->{removed} || $self->{stopped}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$job->{"removed"} or $self->{"stopped"}
MC/DC0%0/2
$job->{"removed"} or $self->{"stopped"}
$job->{"removed"}
$self->{"stopped"}
Truth table33%1/3
A or B
A= $job->{"removed"}B= $self->{"stopped"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 11 | ▶ | if ($global) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 5 | my $scope = $job->{global_scope}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 5 | ▶ | $self->{global_until}{$scope} = $until if $until > ( $self->{global_until}{$scope} // 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"global_until"}{$scope} // 0
MC/DC0%0/1
$self->{"global_until"}{$scope} // 0
$self->{"global_until"}{$scope}
Truth table50%1/2
A
A= $self->{"global_until"}{$scope}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 6 | ▶ | $bucket->{blocked} = $until if $until > $bucket->{blocked}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | # Record the limit even when this request has exhausted its retry budget. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 11 | ▶ | if ( $job->{retries}++ >= $self->{max_retries} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 2 | $self->_finish( $job, 'Rate limit retry budget exhausted', 'rate_limit', $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 9 | $self->_emit('request_retry'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | # Otherwise the same job remains at its original queue position. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 287 | ▶ | sub _release { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_release: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 287 | my ( $self, $job ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # Release a reservation once, including when its bucket has since been merged. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 287 | ▶ | return unless delete $job->{active}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 150 | --_root( $job->{bucket} )->{active}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 150 | --$self->{active}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 150 | delete $job->{bucket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 146 | ▶ | sub _remove { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_remove: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 146 | my ( $self, $job ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | # Release capacity once; transport cancellation can re-enter removal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 146 | ▶ | return if $job->{removed}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 146 | $job->{removed} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 146 | $self->_release($job); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 146 | @{ $self->{jobs} } = grep { $_ != $job } @{ $self->{jobs} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 136 | ▶ | sub _finish { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_finish: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 136 | my ( $self, $job, $message, $category, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # Detach the job before resolving its Future and running application callbacks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 136 | $self->_remove($job); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 136 | ▶ | return if $job->{future}->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 136 | ▶ | $self->_emit( 'request_finished', category => $category // 'response' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$category // "response"
MC/DC100%1/1
$category // "response"
$category
Truth table100%2/2
A
A= $category
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 136 | ▶ | if ( defined $category ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 15 | $job->{future}->fail( $message, $category, $response ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 121 | $job->{future}->done($response); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 596 | ▶ | sub _emit { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_emit: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 596 | my ( $self, $type, %fields ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | # Diagnostics contain scheduling metadata only and cannot interrupt request handling. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 596 | ++$self->{counters}{$type}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 596 | ▶ | return unless $self->{on_diagnostic}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 23 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | $self->{on_diagnostic}->( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | type => $type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | %fields, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 23 | pending => scalar( @{ $self->{jobs} } ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | active => $self->{active} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 23 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 23 | ▶ | warn "REST diagnostic handler failed\n" unless $ok; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 11 | ▶ | sub stats { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stats: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 11 | return { %{ $self->{counters} }, pending => scalar( @{ $self->{jobs} } ), active => $self->{active} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 148 | ▶ | sub close { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
close: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 148 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | # Stop admission and timers before failing queued requests and cancelling active I/O. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 148 | $self->{stopped} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 148 | ▶ | if ( my $timer = delete $self->{timer} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 1 | ▶ | $timer->cancel unless $timer->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 148 | my @jobs = @{ $self->{jobs} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 148 | for my $job (@jobs) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 2 | my $transport_future = delete $job->{transport_future}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 2 | $self->_finish( $job, 'REST client is closed', 'shutdown' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 2 | ▶ | $transport_future->cancel if $transport_future && !$transport_future->is_ready; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$transport_future and not $transport_future->is_ready
MC/DC50%1/2
$transport_future and not $transport_future->is_ready
$transport_future
not $transport_future->is_ready
Truth table66%2/3
A and not(B)
A= $transport_futureB= not $transport_future->is_ready
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 148 | $self->{routes} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 148 | $self->{buckets} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 148 | return $self->{loop}->new_future->done; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 22 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||