blib/lib/Discord/Client/Gateway/IdentifyLimiter.pm

filter: Statementstmt 94.9% 169 / 178 Branchbran 76.1% 67 / 88 Conditioncond 56.0% 37 / 66 MC/DCmcdc 30.5% 11 / 36 Subroutinesub 88.4% 23 / 26 Podpod 0.0% 0 / 5 totaltotal 76.9% 307 / 399 CC 73 SCAR 26.5
CC
73
Cov
82%
_pump
29.4
_valid_info
22.1
new
10.5
CRAP 14.1
« blib/lib/Discord/Client/Gateway/Session.pm ↑ Summary blib/lib/Discord/Client/Interactions.pm »
1 package Discord::Client::Gateway::IdentifyLimiter;
2  
3 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Discord::Client::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '0.002';
12  
13 62 ▶sub new {
Subroutine new: called CC=6 SCAR=23.5
Pod undocumented
14 62 my ( $class, %args ) = @_;
15  
16 # One scheduler shares metadata, pending permits and concurrency buckets.
17 62 ▶ croak 'loop and REST client are required' unless $args{loop} && $args{rest};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"loop"} and $args{"rest"} 0 62 62
Condition33%1/3
$args{"loop"} and $args{"rest"}
!ll&&!rl&&r
0062
MC/DC0%0/2
$args{"loop"} and $args{"rest"}
$args{"loop"} $args{"rest"}
Truth table33%1/3
A and B
A= $args{"loop"}B= $args{"rest"}
ABresult
0X0
100
111
18 return bless {
19 loop => $args{loop},
20 rest => $args{rest},
21 0 ▶ clock => $args{clock} // sub { clock_gettime(CLOCK_MONOTONIC) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 0 ▶ delay => $args{delay} // sub { $args{loop}->delay_future( after => $_[0] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 62 ▶ max_pending => $args{max_pending} // 1024,
Condition50%4/8
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
l!l&&r!l&&!r
6200
$args{"delay"} // sub { $args{"loop"}->delay_future("after", $_[0]); }
l!l&&r!l&&!r
6200
$args{"max_pending"} // 1024
l!l
755
MC/DC0%0/2
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
$args{"clock"} sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
MC/DC0%0/2
$args{"delay"} // sub { $args{"loop"}->delay_future("after", $_[0]); }
$args{"delay"} sub { $args{"loop"}->delay_future("after", $_[0]); }
MC/DC100%1/1
$args{"max_pending"} // 1024
$args{"max_pending"}
Truth table33%1/3
A // B
A= $args{"clock"}B= sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args{"delay"}B= sub { $args{"loop"}->delay_future("after", $_[0]); }
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args{"max_pending"}
Aresult
11
00
24 waiters => [],
25 bucket_until => {},
26 }, $class;
27 }
28  
29 5 ▶sub _fail {
Subroutine _fail: called CC=1 SCAR=0.0
30 5 my ( $self, $future, $message, $category ) = @_;
31 5 my $error = Discord::Client::Error->new( message => $message, category => $category );
32 5 return $future->fail( $message, $category, $error );
33 }
34  
35 68 ▶sub info {
Subroutine info: called CC=3 SCAR=11.2
Pod undocumented
36 68 my ($self) = @_;
37  
38 # Serve cached metadata or join the current discovery request.
39 68 my $future = $self->{loop}->new_future;
40 68 ▶ return $self->_fail( $future, 'Identify limiter is closed', 'shutdown' ) if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 68 68
41 68 ▶ return $future->done( $self->{info} ) if $self->{info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"} 14 54 68
42 54 my $load = $self->_load;
43 $load->on_ready(
44 54 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
45 54 ▶ return if $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready 0 54 54
46 54 ▶ if ( $load->is_done ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($load->is_done) { } 52 2 54
elsif ($load->is_failed) { } 2 0 2
47 52 $future->done( $self->{info} );
48 }
49 elsif ( $load->is_failed ) {
50 2 $future->fail( $load->failure );
51 }
52 else {
53 0 $self->_fail( $future, 'Gateway discovery cancelled', 'transport' );
54 }
55 }
56 54 );
57 54 return $future;
58 }
59  
60 63 ▶sub _load {
Subroutine _load: called CC=4 SCAR=14.1
61 63 my ($self) = @_;
62  
63 # Coalesce discovery so concurrent callers cannot reset the budget separately.
64 63 ▶ return $self->{load} if $self->{load};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"load"} 1 62 63
65 62 my $result = $self->{loop}->new_future;
66 62 $self->{load} = $result;
67 62 my $http = eval { $self->{rest}->request( 'GET', '/gateway/bot' ) };
68 62 ▶ unless ( blessed($http) && $http->isa('Future') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($http) and $http->isa("Future")) 0 62 62
Condition33%1/3
builtin::blessed($http) and $http->isa("Future")
!ll&&!rl&&r
0062
MC/DC0%0/2
builtin::blessed($http) and $http->isa("Future")
builtin::blessed($http) $http->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($http)B= $http->isa("Future")
ABresult
0X0
100
111
69 0 delete $self->{load};
70 0 return $self->_fail( $result, 'Gateway discovery transport failed', 'transport' );
71 }
72 62 $self->{http} = $http;
73 $http->on_ready(
74 62 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
75 # Clear discovery ownership before notifying callbacks that may re-enter.
76 62 delete @{$self}{qw(http load)};
77 62 ▶ return if $result->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_ready 0 62 62
78 62 $self->_complete_discovery( $result, $http );
79  
80 62 ▶ if ( $result->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_failed) 4 58 62
81  
82 # Fail queued requests together when no usable budget can be discovered.
83 4 my @waiters = @{ $self->{waiters} };
84 4 $self->{waiters} = [];
85 $_->{future}->fail( $result->failure )
86 4 for grep { !$_->{future}->is_ready } @waiters;
87 }
88 62 $self->_pump;
89 }
90 62 );
91 62 return $result;
92 }
93  
94 62 ▶sub _complete_discovery {
Subroutine _complete_discovery: called CC=6 SCAR=18.0
95 62 my ( $self, $result, $http ) = @_;
96  
97 # Settle terminal failures before inspecting or installing any metadata.
98 62 ▶ return $self->_fail( $result, 'Identify limiter is closed', 'shutdown' ) if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 2 60 62
99 60 ▶ return $result->fail( $http->failure ) if $http->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $http->is_failed 0 60 60
100 60 ▶ return $self->_fail( $result, 'Gateway discovery cancelled', 'transport' ) if $http->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $http->is_cancelled 0 60 60
101  
102 60 my ($info) = $http->result;
103 60 ▶ return $self->_fail( $result, 'Invalid Gateway Bot metadata', 'protocol' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid_info($info) 2 58 60
104 unless $self->_valid_info($info);
105  
106 # Install the validated budget before notifying callers that may request permits.
107 # Zero reset_after supplies no future reset time.
108 58 my $limit = $info->{session_start_limit};
109 58 $self->{info} = $info;
110 58 $self->{remaining} = $limit->{remaining};
111 $self->{reset_at} =
112 $limit->{reset_after} > 0
113 58 ▶ ? $self->{clock}->() + $limit->{reset_after} / 1000
Branch: 1 branch
BranchTrueFalseTotal
$limit->{'reset_after'} > 0 ? : 54 4 58
114 : undef;
115 58 $self->{max_concurrency} = $limit->{max_concurrency};
116 58 return $result->done($info);
117 }
118  
119 60 ▶sub _valid_info {
Subroutine _valid_info: called CC=12 SCAR=30.9
120 60 my ( $self, $info ) = @_;
121  
122 # Reject malformed fields before they become scheduling state.
123 return 0
124 unless ref($info) eq 'HASH'
125 && defined( $info->{url} )
126 && !ref( $info->{url} )
127 && $info->{url} =~ m{\Awss://[^\s]+\z}
128 60 ▶ && ref( $info->{session_start_limit} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info eq "HASH" and defined $info->{"url"} and not ref $info->{"url"} and $info->{"url"} =~ m[\Awss://[^\s]+\z]u and ref $info->{"session_start_limit"} eq "HASH" 0 60 60
Condition33%4/12
ref $info eq "HASH" and defined $info->{"url"} and not ref $info->{"url"} and $info->{"url"} =~ m[\Awss://[^\s]+\z]u and ref $info->{"session_start_limit"} eq "HASH"
!ll&&!rl&&r
0060
ref $info eq "HASH" and defined $info->{"url"} and not ref $info->{"url"} and $info->{"url"} =~ m[\Awss://[^\s]+\z]u
!ll&&!rl&&r
0060
ref $info eq "HASH" and defined $info->{"url"} and not ref $info->{"url"}
!ll&&!rl&&r
0060
ref $info eq "HASH" and defined $info->{"url"}
!ll&&!rl&&r
0060
MC/DC0%0/5
ref $info eq "HASH" and defined $info->{"url"} and not ref $info->{"url"} and $info->{"url"} =~ m[\Awss://[^\s]+\z]u and ref $info->{"session_start_limit"} eq "HASH"
ref $info eq "HASH" defined $info->{"url"} not ref $info->{"url"} $info->{"url"} =~ m[\Awss://[^\s]+\z]u ref $info->{"session_start_limit"} eq "HASH"
Truth table16%1/6
A and B and not(C) and D and E
A= ref $info eq "HASH"B= defined $info->{"url"}C= not ref $info->{"url"}D= $info->{"url"} =~ m[\Awss://[^\s]+\z]uE= ref $info->{"session_start_limit"} eq "HASH"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
129 60 my $limits = $info->{session_start_limit};
130 60 for my $field (qw(total remaining reset_after max_concurrency)) {
131 return 0
132 unless defined( $limits->{$field} )
133 && !ref( $limits->{$field} )
134 236 ▶ && $limits->{$field} =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $limits->{$field} and not ref $limits->{$field} and $limits->{$field} =~ /\A\d+\z/u 2 234 236
Condition83%5/6
defined $limits->{$field} and not ref $limits->{$field} and $limits->{$field} =~ /\A\d+\z/u
!ll&&!rl&&r
11234
defined $limits->{$field} and not ref $limits->{$field}
!ll&&!rl&&r
10235
MC/DC66%2/3
defined $limits->{$field} and not ref $limits->{$field} and $limits->{$field} =~ /\A\d+\z/u
defined $limits->{$field} not ref $limits->{$field} $limits->{$field} =~ /\A\d+\z/u
Truth table75%3/4
A and not(B) and C
A= defined $limits->{$field}B= not ref $limits->{$field}C= $limits->{$field} =~ /\A\d+\z/u
ABCresult
0XX0
10X0
1100
1111
135 }
136 return
137 $limits->{total} > 0
138 && $limits->{max_concurrency} > 0
139 58 ▶ && $limits->{remaining} <= $limits->{total};
Condition33%2/6
$limits->{'total'} > 0 && $limits->{'max_concurrency'} > 0 && $limits->{'remaining'} <= $limits->{'total'}
!ll&&!rl&&r
0058
$limits->{"total"} > 0 and $limits->{"max_concurrency"} > 0
!ll&&!rl&&r
0058
MC/DC0%0/3
$limits->{'total'} > 0 && $limits->{'max_concurrency'} > 0 && $limits->{'remaining'} <= $limits->{'total'}
$limits->{"total"} > 0 $limits->{"max_concurrency"} > 0 $limits->{'remaining'} <= $limits->{'total'}
Truth table25%1/4
A and B && C
A= $limits->{"total"} > 0B= $limits->{"max_concurrency"} > 0C= $limits->{'remaining'} <= $limits->{'total'}
ABCresult
0XX0
10X0
1100
1111
140 }
141  
142 67 ▶sub acquire {
Subroutine acquire: called CC=6 SCAR=18.4
Pod undocumented
143 67 my ( $self, $shard_id ) = @_;
144  
145 # Validate and bound requests before adding a waiter to the shared queue.
146 67 my $future = $self->{loop}->new_future;
147 67 ▶ return $self->_fail( $future, 'Identify limiter is closed', 'shutdown' ) if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 67 67
148 67 ▶ return $self->_fail( $future, 'Invalid shard ID', 'request' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shard_id and not ref $shard_id and $shard_id =~ /\A\d+\z/u 0 67 67
Condition33%2/6
defined $shard_id and not ref $shard_id and $shard_id =~ /\A\d+\z/u
!ll&&!rl&&r
0067
defined $shard_id and not ref $shard_id
!ll&&!rl&&r
0067
MC/DC0%0/3
defined $shard_id and not ref $shard_id and $shard_id =~ /\A\d+\z/u
defined $shard_id not ref $shard_id $shard_id =~ /\A\d+\z/u
Truth table25%1/4
A and not(B) and C
A= defined $shard_idB= not ref $shard_idC= $shard_id =~ /\A\d+\z/u
ABCresult
0XX0
10X0
1100
1111
149 unless defined($shard_id) && !ref($shard_id) && $shard_id =~ /\A\d+\z/;
150 return $self->_fail( $future, 'Identify queue capacity exceeded', 'queue_full' )
151 67 ▶ if @{ $self->{waiters} } >= $self->{max_pending};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"waiters"};} >= $self->{"max_pending"} 1 66 67
152 66 my $job = { shard => $shard_id, future => $future };
153 66 push @{ $self->{waiters} }, $job;
154  
155 # Cancellation removes an ungranted request and recomputes queue timing.
156 $future->on_cancel(
157 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
158 1 @{ $self->{waiters} } = grep { $_ != $job } @{ $self->{waiters} };
159 1 $self->_pump;
160 }
161 66 );
162 66 $self->_pump;
163 66 return $future;
164 }
165  
166 201 ▶sub _pump {
Subroutine _pump: called CC=29 SCAR=33.8
167 201 my ($self) = @_;
168 201 ▶ return if $self->{closed} || $self->{pumping};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"pumping"} 12 189 201
Condition100%3/3
$self->{"closed"} or $self->{"pumping"}
l!l&&r!l&&!r
210189
MC/DC100%2/2
$self->{"closed"} or $self->{"pumping"}
$self->{"closed"} $self->{"pumping"}
Truth table100%3/3
A or B
A= $self->{"closed"}B= $self->{"pumping"}
ABresult
1X1
011
000
169 189 local $self->{pumping} = 1;
170  
171 # Recompute wake-up timing whenever the queue or server budget changes.
172 189 ▶ if ( my $timer = delete $self->{timer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timer = delete $self->{"timer"}) 3 186 189
173 3 ▶ $timer->cancel unless $timer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $timer->is_ready 3 0 3
174 }
175 189 ▶ return unless @{ $self->{waiters} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"waiters"};} 111 78 189
176  
177 # Expired metadata must be refreshed before another Identify can be granted.
178 delete $self->{info}
179 if $self->{info}
180 && defined( $self->{reset_at} )
181 78 ▶ && $self->{clock}->() >= $self->{reset_at};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"} and defined $self->{"reset_at"} and $self->{"clock"}->() >= $self->{"reset_at"} 3 75 78
Condition100%6/6
$self->{"info"} and defined $self->{"reset_at"} and $self->{"clock"}->() >= $self->{"reset_at"}
!ll&&!rl&&r
11643
$self->{"info"} and defined $self->{"reset_at"}
!ll&&!rl&&r
6567
MC/DC100%3/3
$self->{"info"} and defined $self->{"reset_at"} and $self->{"clock"}->() >= $self->{"reset_at"}
$self->{"info"} defined $self->{"reset_at"} $self->{"clock"}->() >= $self->{"reset_at"}
Truth table100%4/4
A and B and C
A= $self->{"info"}B= defined $self->{"reset_at"}C= $self->{"clock"}->() >= $self->{"reset_at"}
ABCresult
0XX0
10X0
1100
1111
182 78 ▶ if ( !$self->{info} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"info"}) 9 69 78
183 9 my $load = $self->_load;
184 9 ▶ if ( $load->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($load->is_failed) 1 8 9
185 1 my @jobs = @{ $self->{waiters} };
186 1 $self->{waiters} = [];
187 0 $_->{future}->fail( $load->failure ) for grep { !$_->{future}->is_ready } @jobs;
188 }
189 9 ▶ return unless $self->{info};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"info"} 3 6 9
190 }
191 75 my $now = $self->{clock}->();
192 75 my $next_attempt_at;
193  
194 # Reserve each free concurrency bucket and consume one start per permit.
195 75 ▶ while ( $self->{remaining} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remaining"} > 0) 129 9 138
196 129 my $job;
197 129 $next_attempt_at = undef;
198 129 for my $candidate ( @{ $self->{waiters} } ) {
199 73 my $bucket = $candidate->{shard} % $self->{max_concurrency};
200 73 ▶ next if $self->{reserved}{$bucket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reserved"}{$bucket} 1 72 73
201 72 ▶ my $available_at = $self->{bucket_until}{$bucket} // 0;
Condition100%2/2
$self->{"bucket_until"}{$bucket} // 0
l!l
1656
MC/DC100%1/1
$self->{"bucket_until"}{$bucket} // 0
$self->{"bucket_until"}{$bucket}
Truth table100%2/2
A
A= $self->{"bucket_until"}{$bucket}
Aresult
11
00
202 72 ▶ if ( $available_at > $now ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($available_at > $now) 9 63 72
203 9 ▶ $next_attempt_at = $available_at
Branch: 1 branch
BranchTrueFalseTotal
if not defined $next_attempt_at or $available_at < $next_attempt_at 9 0 9
Condition33%1/3
not defined $next_attempt_at or $available_at < $next_attempt_at
l!l&&r!l&&!r
900
MC/DC0%0/2
not defined $next_attempt_at or $available_at < $next_attempt_at
not defined $next_attempt_at $available_at < $next_attempt_at
Truth table33%1/3
not(A) or B
A= not defined $next_attempt_atB= $available_at < $next_attempt_at
ABresult
1X1
011
000
204 if !defined($next_attempt_at) || $available_at < $next_attempt_at;
205 9 next;
206 }
207 63 $job = $candidate;
208 63 $self->{reserved}{$bucket} = 1;
209 63 $job->{key} = $bucket;
210 63 last;
211 }
212 129 ▶ last unless $job;
Branch: 1 branch
BranchTrueFalseTotal
unless $job 66 63 129
213 63 @{ $self->{waiters} } = grep { $_ != $job } @{ $self->{waiters} };
214 63 --$self->{remaining};
215 $job->{future}->done( bless { limiter => $self, key => $job->{key} },
216 63 'Discord::Client::Gateway::IdentifyLimiter::Permit' );
217 63 ▶ last if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 63 63
218 }
219 75 ▶ return if $self->{closed} || !@{ $self->{waiters} };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or not @{$self->{"waiters"};} 62 13 75
Condition66%2/3
$self->{"closed"} or not @{$self->{"waiters"};}
l!l&&r!l&&!r
06213
MC/DC50%1/2
$self->{"closed"} or not @{$self->{"waiters"};}
$self->{"closed"} not @{$self->{"waiters"};}
Truth table66%2/3
A or not(B)
A= $self->{"closed"}B= not @{$self->{"waiters"};}
ABresult
1X1
011
000
220  
221 # A zero reset delay supplies no future reset time. Poll an exhausted
222 # budget conservatively rather than scheduling a busy loop.
223 13 ▶ if ( $self->{remaining} <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remaining"} <= 0) 4 9 13
224 4 ▶ $self->{reset_at} //= $now + 1;
Condition100%2/2
$self->{"reset_at"} //= $now + 1
l!l
22
MC/DC100%1/1
$self->{"reset_at"} //= $now + 1
$self->{"reset_at"}
Truth table100%2/2
A
A= $self->{"reset_at"}
Aresult
11
00
225 4 $next_attempt_at = $self->{reset_at};
226 }
227 13 ▶ return unless defined $next_attempt_at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next_attempt_at 1 12 13
228  
229 # Refresh metadata before the next bucket wake-up if its reset is earlier.
230 $next_attempt_at = $self->{reset_at}
231 12 ▶ if defined( $self->{reset_at} ) && $next_attempt_at > $self->{reset_at};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"reset_at"} and $next_attempt_at > $self->{"reset_at"} 0 12 12
Condition66%2/3
defined $self->{"reset_at"} and $next_attempt_at > $self->{"reset_at"}
!ll&&!rl&&r
1110
MC/DC0%0/2
defined $self->{"reset_at"} and $next_attempt_at > $self->{"reset_at"}
defined $self->{"reset_at"} $next_attempt_at > $self->{"reset_at"}
Truth table66%2/3
A and B
A= defined $self->{"reset_at"}B= $next_attempt_at > $self->{"reset_at"}
ABresult
0X0
100
111
232 12 my $timer = $self->{delay}->( $next_attempt_at - $self->{clock}->() );
233 12 $self->{timer} = $timer;
234 $timer->on_done(
235 9 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
236 9 delete $self->{timer};
237 9 $self->_pump;
238 }
239 12 );
240 }
241  
242 2 ▶sub stats {
Subroutine stats: called CC=1 SCAR=0.0
Pod undocumented
243 2 my ($self) = @_;
244 return {
245 remaining => $self->{remaining},
246 max_concurrency => $self->{max_concurrency},
247 2 pending => scalar( @{ $self->{waiters} } )
248 };
249 }
250  
251 62 ▶sub close {
Subroutine close: called CC=6 SCAR=19.4
Pod undocumented
252 62 my ($self) = @_;
253  
254 # Release timers and pending work without closing the caller-owned REST client.
255 62 $self->{closed} = 1;
256 62 ▶ if ( my $timer = delete $self->{timer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timer = delete $self->{"timer"}) 0 62 62
257 0 ▶ $timer->cancel unless $timer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $timer->is_ready 0 0 0
258 }
259 62 ▶ if ( my $http = delete $self->{http} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $http = delete $self->{"http"}) 2 60 62
260 2 ▶ $http->cancel unless $http->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $http->is_ready 2 0 2
261 }
262 62 my @jobs = @{ $self->{waiters} };
263 62 $self->{waiters} = [];
264 $self->_fail( $_->{future}, 'Identify limiter is closed', 'shutdown' )
265 0 for grep { !$_->{future}->is_ready } @jobs;
266 62 return $self->{loop}->new_future->done;
267 }
268  
269 package Discord::Client::Gateway::IdentifyLimiter::Permit;
270  
271 63 ▶sub commit {
Subroutine commit: called CC=2 SCAR=7.0
272 63 my ($self) = @_;
273  
274 # Sends commit just before writing. Unused permits also commit on release
275 # so abandoning a reservation cannot bypass the shared bucket cooldown.
276 63 ▶ return if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 63 63
277 63 $self->{committed} = 1;
278 63 my $limiter = $self->{limiter};
279 63 delete $limiter->{reserved}{ $self->{key} };
280 63 $limiter->{bucket_until}{ $self->{key} } = $limiter->{clock}->() + 5;
281 63 $limiter->_pump;
282 }
283  
284 0 ▶sub cancel { $_[0]->commit }
Subroutine cancel: not called CC=1 SCAR=6.9
285 63 ▶sub DESTROY { $_[0]->cancel unless $_[0]->{committed} }
Subroutine DESTROY: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"committed"} 0 63 63
286  
287 11 1;
« blib/lib/Discord/Client/Gateway/Session.pm ↑ Summary blib/lib/Discord/Client/Interactions.pm »