blib/lib/Discord/Client/REST/RateLimiter.pm

filter: Statementstmt 98.8% 260 / 263 Branchbran 85.2% 121 / 142 Conditioncond 67.9% 87 / 128 MC/DCmcdc 43.8% 32 / 73 Subroutinesub 100% 28 / 28 Podpod 0.0% 0 / 4 totaltotal 82.7% 528 / 638 CC 125 SCAR 23.4
CC
125
Cov
88%
_handle_rate_limit
22.6
_response_bucket
19.7
_update_bucket_limits
11.6
CRAP 10.4
« blib/lib/Discord/Client/Interactions.pm ↑ Summary blib/lib/Discord/Client/HTTPInteractions.pm »
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.004';
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
BranchTrueFalseTotal
if $self->{"stopped"} 1 150 151
34 return $future->fail( 'Bot authentication is invalid', 'authentication' )
35 150 ▶ if $self->{auth_failed} && $args{auth};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auth_failed"} and $args{"auth"} 1 149 150
Condition100%3/3
$self->{"auth_failed"} and $args{"auth"}
!ll&&!rl&&r
14811
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"}
ABresult
0X0
100
111
36 return $future->fail( 'REST request capacity exceeded', 'queue_full' )
37 149 ▶ if @{ $self->{jobs} } >= $self->{max_pending};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"jobs"};} >= $self->{"max_pending"} 1 148 149
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
BranchTrueFalseTotal
unless (exists $self->{"routes"}{$args{"route"}}) 128 20 148
42 return $future->fail( 'REST bucket capacity exceeded', 'queue_full' )
43 128 ▶ if keys( %{ $self->{routes} } ) >= $self->{max_buckets};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"routes"};} >= $self->{"max_buckets"} 2 126 128
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
BranchTrueFalseTotal
if $transport_future and not $transport_future->is_ready 8 2 10
Condition66%2/3
$transport_future and not $transport_future->is_ready
!ll&&!rl&&r
208
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($bucket->{"parent"}) 2 641 643
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
BranchTrueFalseTotal
if keys %{$$self{"routes"};} < $self->{"max_buckets"} 145 3 148
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
BranchTrueFalseTotal
if $used{$route} or $bucket->{"active"} or $bucket->{"blocked"} > $now or ($bucket->{"reset"} // 0) > $now 2 1 3
Condition72%8/11
$used{$route} or $bucket->{"active"} or $bucket->{"blocked"} > $now or ($bucket->{"reset"} // 0) > $now
l!l&&r!l&&!r
111
$used{$route} or $bucket->{"active"} or $bucket->{"blocked"} > $now
l!l&&r!l&&!r
102
$used{$route} or $bucket->{"active"}
l!l&&r!l&&!r
102
$bucket->{"reset"} // 0
l!l
20
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"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
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
BranchTrueFalseTotal
unless $live{_root($self->{"buckets"}{$key})} 1 1 2
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
BranchTrueFalseTotal
if $self->{"pumping"} or $self->{"stopped"} 1 308 309
Condition66%2/3
$self->{"pumping"} or $self->{"stopped"}
l!l&&r!l&&!r
10308
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (my $timer = delete $self->{"timer"}) 12 296 308
110 12 ▶ $timer->cancel unless $timer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $timer->is_ready 12 0 12
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
BranchTrueFalseTotal
if ($self->{"active"} < $self->{"max_in_flight"}) 449 9 458
117 449 my $candidate;
118 449 ( $candidate, $earliest ) = $self->_next_ready_job( $self->{clock}->() );
119 449 ▶ last unless $candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless $candidate 299 150 449
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
BranchTrueFalseTotal
if (defined $earliest and not $self->{"stopped"}) 25 283 308
Condition66%2/3
defined $earliest and not $self->{"stopped"}
!ll&&!rl&&r
283025
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"}
ABresult
0X0
100
111
125 25 my $delay = $earliest - $self->{clock}->();
126 25 ▶ $delay = 0 if $delay < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delay < 0 0 25 25
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
BranchTrueFalseTotal
if $job->{"active"} 174 189 363
146 189 my $bucket = _root( $self->{routes}{ $job->{route} } );
147 189 ▶ next if $bucket->{active};
Branch: 1 branch
BranchTrueFalseTotal
if $bucket->{"active"} 8 181 189
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
BranchTrueFalseTotal
if $available_at <= $now 150 31 181
150 31 ▶ $earliest = $available_at if !defined($earliest) || $available_at < $earliest;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $earliest or $available_at < $earliest 30 1 31
Condition66%2/3
not defined $earliest or $available_at < $earliest
l!l&&r!l&&!r
3001
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (defined $bucket->{"reset"} and $bucket->{"reset"} <= $now) 4 177 181
Condition100%3/3
defined $bucket->{"reset"} and $bucket->{"reset"} <= $now
!ll&&!rl&&r
16894
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
ABresult
0X0
100
111
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
l!l
20161
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}
Aresult
11
00
166 181 ▶ $available_at = $global_reset_at if $global_reset_at > $available_at;
Branch: 1 branch
BranchTrueFalseTotal
if $global_reset_at > $available_at 20 161 181
167 181 ▶ if ( defined( $bucket->{remaining} ) && $bucket->{remaining} <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bucket->{"remaining"} and $bucket->{"remaining"} <= 0) 9 172 181
Condition100%3/3
defined $bucket->{"remaining"} and $bucket->{"remaining"} <= 0
!ll&&!rl&&r
16759
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if defined $bucket->{"reset"} and $bucket->{"reset"} > $available_at 8 1 9
Condition66%2/3
defined $bucket->{"reset"} and $bucket->{"reset"} > $available_at
!ll&&!rl&&r
108
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $job->{"removed"} or $self->{"stopped"} 0 150 150
Condition33%1/3
$job->{"removed"} or $self->{"stopped"}
l!l&&r!l&&!r
00150
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"}
ABresult
1X1
011
000
187 150 ▶ --$bucket->{remaining} if defined( $bucket->{remaining} ) && $bucket->{remaining} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bucket->{"remaining"} and $bucket->{"remaining"} > 0 4 146 150
Condition100%3/3
defined $bucket->{"remaining"} and $bucket->{"remaining"} > 0
!ll&&!rl&&r
14514
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ($transport_future and eval { do { $transport_future->isa("Future") } }) 0 150 150
Condition33%1/3
$transport_future and eval { do { $transport_future->isa("Future") } }
!ll&&!rl&&r
00150
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") } }
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $job->{"removed"} 9 141 150
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
BranchTrueFalseTotal
if $transport_future->is_cancelled 0 141 141
215 if $transport_future->is_cancelled;
216 141 ▶ if ( $transport_future->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transport_future->is_failed) 8 133 141
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
BranchTrueFalseTotal
if ref $response and eval { do { $response->isa("HTTP::Response") } } 1 7 8
Condition66%2/3
ref $response and eval { do { $response->isa("HTTP::Response") } }
!ll&&!rl&&r
701
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") } }
ABresult
0X0
100
111
223 7 ▶ my $failure_category = ( $category // '' ) =~ /\A(?:timeout|stall_timeout)\z/ ? 'timeout' : 'transport';
Branch: 1 branch
BranchTrueFalseTotal
($category // '') =~ /\A(?:timeout|stall_timeout)\z/u ? : 4 3 7
Condition100%2/2
$category // ""
l!l
52
MC/DC100%1/1
$category // ""
$category
Truth table100%2/2
A
A= $category
Aresult
11
00
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
BranchTrueFalseTotal
unless ref $response and eval { do { $response->isa("HTTP::Response") } } 0 133 133
Condition33%1/3
ref $response and eval { do { $response->isa("HTTP::Response") } }
!ll&&!rl&&r
00133
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") } }
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /\A\d+(?:\.\d+)?\z/u 388 33 421
Condition66%4/6
defined $value and not ref $value and $value =~ /\A\d+(?:\.\d+)?\z/u
!ll&&!rl&&r
388033
defined $value and not ref $value
!ll&&!rl&&r
388033
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
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if $job->{"removed"} or $self->{"stopped"} 0 134 134
Condition33%1/3
$job->{"removed"} or $self->{"stopped"}
l!l&&r!l&&!r
00134
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($response->code == 401 and $job->{"auth"}) 3 131 134
Condition100%3/3
$response->code == 401 and $job->{"auth"}
!ll&&!rl&&r
13013
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"}
ABresult
0X0
100
111
257 3 $self->_invalidate_authentication( $job, $response );
258 3 return;
259 }
260 131 ▶ if ( $response->code == 429 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 429) 13 118 131
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
BranchTrueFalseTotal
unless defined $hash and length $hash 124 10 134
Condition66%2/3
defined $hash and length $hash
!ll&&!rl&&r
124010
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($known and _root($known) != $bucket) 2 8 10
Condition100%3/3
$known and _root($known) != $bucket
!ll&&!rl&&r
712
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if defined $bucket->{$field} and !defined($known->{$field}) || $bucket->{$field} > $known->{$field} 0 4 4
Condition50%3/6
defined $bucket->{$field} and !defined($known->{$field}) || $bucket->{$field} > $known->{$field}
!ll&&!rl&&r
220
not defined $known->{$field} or $bucket->{$field} > $known->{$field}
l!l&&r!l&&!r
002
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}
ABCresult
0XX0
1000
11X1
1011
286 }
287 2 ▶ if ( defined $bucket->{remaining} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bucket->{"remaining"}) 0 2 2
288 $known->{remaining} = $bucket->{remaining}
289 0 ▶ if !defined( $known->{remaining} ) || $bucket->{remaining} < $known->{remaining};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $known->{"remaining"} or $bucket->{"remaining"} < $known->{"remaining"} 0 0 0
Condition0%0/3
not defined $known->{"remaining"} or $bucket->{"remaining"} < $known->{"remaining"}
l!l&&r!l&&!r
000
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if exists $self->{"buckets"}{$key} or keys %{$$self{"buckets"};} < $self->{"max_buckets"} 10 0 10
Condition66%2/3
exists $self->{"buckets"}{$key} or keys %{$$self{"buckets"};} < $self->{"max_buckets"}
l!l&&r!l&&!r
370
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (defined $remaining) 13 121 134
307 $bucket->{remaining} = $remaining
308 if !$bucket->{active}
309 || !defined( $bucket->{remaining} )
310 13 ▶ || $remaining < $bucket->{remaining};
Branch: 1 branch
BranchTrueFalseTotal
if not $bucket->{"active"} && defined $bucket->{"remaining"} or $remaining < $bucket->{"remaining"} 12 1 13
Condition66%4/6
not $bucket->{"active"} && defined $bucket->{"remaining"} or $remaining < $bucket->{"remaining"}
l!l&&r!l&&!r
1201
$bucket->{'active'} && defined $bucket->{'remaining'}
!ll&&!rl&&r
1201
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"}
ABCresult
0XX1
10X1
1111
1100
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
BranchTrueFalseTotal
unless (defined $after) 129 5 134
316 129 my $epoch = _number( $response->header('X-RateLimit-Reset') );
317 129 ▶ $after = $epoch - $self->{wall_clock}->() if defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
if defined $epoch 1 128 129
318 }
319 134 ▶ if ( defined $after ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $after) 6 128 134
320 6 ▶ $after = 0 if $after < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $after < 0 0 6 6
321 6 my $reset = $now + $after;
322 6 ▶ $bucket->{reset} = $reset if !defined( $bucket->{reset} ) || $reset > $bucket->{reset};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bucket->{"reset"} or $reset > $bucket->{"reset"} 6 0 6
Condition33%1/3
not defined $bucket->{"reset"} or $reset > $bucket->{"reset"}
l!l&&r!l&&!r
600
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $queued->{"active"} or not $queued->{"auth"} 1 2 3
Condition66%2/3
$queued->{"active"} or not $queued->{"auth"}
l!l&&r!l&&!r
102
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($self->{"auth_failed"} and $job->{"auth"}) 1 12 13
Condition66%2/3
$self->{"auth_failed"} and $job->{"auth"}
!ll&&!rl&&r
1201
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ref $data eq "HASH" 1 11 12
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
BranchTrueFalseTotal
if defined $header and !defined($retry) || $header > $retry 3 9 12
Condition66%4/6
defined $header and !defined($retry) || $header > $retry
!ll&&!rl&&r
903
not defined $retry or $header > $retry
l!l&&r!l&&!r
120
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
ABCresult
0XX0
1000
11X1
1011
352 12 ▶ if ( !defined($retry) || $retry <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $retry or $retry <= 0) 1 11 12
Condition66%2/3
not defined $retry or $retry <= 0
l!l&&r!l&&!r
0111
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
ABresult
1X1
011
000
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'
l!l&&r!l&&!r
506
$data->{"global"} or ($response->header("X-RateLimit-Global") // "") eq "true"
l!l&&r!l&&!r
416
$response->header("X-RateLimit-Global") // ""
l!l
16
$response->header("X-RateLimit-Scope") // ""
l!l
15
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")
ABCresult
1XX1
01X1
0011
0000
362 || ( $response->header('X-RateLimit-Scope') // '' ) eq 'global';
363 11 ▶ $self->_emit( 'rate_limit', global => $global ? 1 : 0, retry_after => $retry );
Branch: 1 branch
BranchTrueFalseTotal
$global ? : 5 6 11
364 11 ▶ return if $job->{removed} || $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"removed"} or $self->{"stopped"} 0 11 11
Condition33%1/3
$job->{"removed"} or $self->{"stopped"}
l!l&&r!l&&!r
0011
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"}
ABresult
1X1
011
000
365 11 ▶ if ($global) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global) { } 5 6 11
366 5 my $scope = $job->{global_scope};
367 5 ▶ $self->{global_until}{$scope} = $until if $until > ( $self->{global_until}{$scope} // 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $until > ($self->{"global_until"}{$scope} // 0) 5 0 5
Condition50%1/2
$self->{"global_until"}{$scope} // 0
l!l
05
MC/DC0%0/1
$self->{"global_until"}{$scope} // 0
$self->{"global_until"}{$scope}
Truth table50%1/2
A
A= $self->{"global_until"}{$scope}
Aresult
11
00
368 }
369 else {
370 6 ▶ $bucket->{blocked} = $until if $until > $bucket->{blocked};
Branch: 1 branch
BranchTrueFalseTotal
if $until > $bucket->{"blocked"} 6 0 6
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
BranchTrueFalseTotal
if ($job->{'retries'}++ >= $self->{'max_retries'}) { } 2 9 11
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
BranchTrueFalseTotal
unless delete $job->{"active"} 137 150 287
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
BranchTrueFalseTotal
if $job->{"removed"} 0 146 146
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
BranchTrueFalseTotal
if $job->{"future"}->is_ready 0 136 136
410 136 ▶ $self->_emit( 'request_finished', category => $category // 'response' );
Condition100%2/2
$category // "response"
l!l
15121
MC/DC100%1/1
$category // "response"
$category
Truth table100%2/2
A
A= $category
Aresult
11
00
411 136 ▶ if ( defined $category ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $category) { } 15 121 136
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
BranchTrueFalseTotal
unless $self->{"on_diagnostic"} 573 23 596
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
BranchTrueFalseTotal
unless $ok 0 23 23
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
BranchTrueFalseTotal
if (my $timer = delete $self->{"timer"}) 1 147 148
450 1 ▶ $timer->cancel unless $timer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $timer->is_ready 1 0 1
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
BranchTrueFalseTotal
if $transport_future and not $transport_future->is_ready 1 1 2
Condition66%2/3
$transport_future and not $transport_future->is_ready
!ll&&!rl&&r
101
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
ABresult
0X0
100
111
457 }
458 148 $self->{routes} = {};
459 148 $self->{buckets} = {};
460 148 return $self->{loop}->new_future->done;
461 }
462  
463 22 1;
« blib/lib/Discord/Client/Interactions.pm ↑ Summary blib/lib/Discord/Client/HTTPInteractions.pm »