blib/lib/MojoX/UserAgent/Throttler.pm

filter: Statementstmt 76.3% 55 / 72 Branchbran 45.8% 11 / 24 Conditioncond 44.4% 8 / 18 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 90.0% 9 / 10 Podpod n/a totaltotal 64.1% 86 / 134 CC 23 SCAR 22.7
CC
23
Cov
65%
__ANON__
16.0
start
16.0
_client_close
8.1
CRAP 9.7
↑ Summary
1 package MojoX::UserAgent::Throttler;
2  
3 3 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = 'v1.0.3';
6  
7 3 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Mojo::Util qw( monkey_patch );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Sub::Util 1.40 qw( set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Sub::Throttler 0.002000 qw( throttle_me throttle_me_sync done_cb );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # https://github.com/kraih/mojo/issues/663
14 # Inconsistent behavior of Mojo::UserAgent::DESTROY:
15 # - sync requests always executed, even when started while DESTROY
16 # - for all active async requests which was started before DESTROY user's
17 # callback will be called with error in $tx
18 # - for all async requests which was started while DESTROY user's callback
19 # won't be called
20 # To emulate this behaviour with throttling:
21 # - sync request: always executed, even when started while DESTROY
22 # - new async request while DESTROY: ignored
23 # - delayed async request (it was delayed before DESTROY):
24 # * if it start before DESTROY: let Mojo::UserAgent handle it using
25 # done_cb($done,$cb)
26 # * if it start while DESTROY: do $done->(0) and call user's callback
27 # with error in $tx
28 # * if it still delayed after DESTROY: call user's callback with error
29 # in $tx
30  
31 3 ▶use constant START_ARGS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 my %Delayed; # $ua => { $tx => [$tx, $cb], … }
34 my %IsDestroying; # $ua => 1
35  
36 3 my $ORIG_start = \&Mojo::UserAgent::start;
37 3 my $ORIG_DESTROY= \&Mojo::UserAgent::DESTROY;
38  
39 monkey_patch 'Mojo::UserAgent',
40 0 ▶start => set_subname('Mojo::UserAgent::start', sub {
Subroutine __ANON__: not called CC=8 SCAR=27.7
Subroutine start: called CC=8 SCAR=27.7
41 # WARNING Async call return undef instead of (undocumented) connection $id.
42 ## no critic (ProhibitExplicitReturnUndef)
43 0 my ($self, $tx, $cb) = @_;
44 0 ▶ if (START_ARGS == @_ && $cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (3 == @_ and $cb) 0 0 0
if (3 == @_ and $cb) 6 6 12
Condition50%3/6
3 == @_ and $cb
!ll&&!rl&&r
000
3 == @_ and $cb
!ll&&!rl&&r
816
MC/DC0%0/2
3 == @_ and $cb
3 == @_ $cb
MC/DC100%2/2
3 == @_ and $cb
3 == @_ $cb
Truth table0%0/3
A and B
A= 3 == @_B= $cb
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= 3 == @_B= $cb
ABresult
0X0
100
111
45 0 ▶ if ($IsDestroying{ $self }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($IsDestroying{$self}) { } 0 0 0
if ($IsDestroying{$self}) { } 1 5 6
46 # $cb->($self, $tx->client_close(1)); # to fix issue 663 or not to fix?
47 0 return undef;
48 }
49 else {
50 0 $Delayed{ $self }{ $tx } = [ $tx, $cb ];
51 }
52 }
53 0 ▶ my $done = ref $_[-1] eq 'CODE' ? &throttle_me || return undef : &throttle_me_sync;
Branch: 2 branches
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
ref $_[-1] eq 'CODE' ? : 10 1 11
Condition50%2/4
&throttle_me or return undef
l!l
00
&throttle_me or return undef
l!l
55
MC/DC0%0/1
&throttle_me or return undef
&throttle_me
MC/DC100%1/1
&throttle_me or return undef
&throttle_me
Truth table0%0/2
A
A= &throttle_me
Aresult
11
00
Truth table100%2/2
A
A= &throttle_me
Aresult
11
00
54 0 ($self, $tx, $cb) = @_;
55 0 ▶ if ($cb) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cb) { } 0 0 0
if ($cb) { } 5 1 6
56 0 ▶ if ($IsDestroying{ $self }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($IsDestroying{$self}) { } 0 0 0
if ($IsDestroying{$self}) { } 1 4 5
57 0 $done->(0);
58 }
59 else {
60 0 delete $Delayed{ $self }{ $tx };
61 0 $self->$ORIG_start($tx, done_cb($done, $cb));
62 }
63 0 return undef;
64 }
65 else {
66 0 $tx = $self->$ORIG_start($tx);
67 0 $done->();
68 0 return $tx;
69 }
70 }),
71 1 ▶DESTROY => sub {
Subroutine DESTROY: called CC=3 SCAR=11.0
72 1 my ($self) = @_;
73 1 $IsDestroying{ $self } = 1;
74 1 ▶ for (values %{ delete $Delayed{ $self } || {} }) {
Condition50%1/2
delete $Delayed{$self} || {}
l!l
10
MC/DC0%0/1
delete $Delayed{$self} || {}
delete $Delayed{$self}
Truth table50%1/2
A
A= delete $Delayed{$self}
Aresult
11
00
75 1 my ($tx, $cb) = @{ $_ };
76 1 $cb->($self, _client_close($tx, 1));
77 }
78 1 $self->$ORIG_DESTROY;
79 1 delete $IsDestroying{ $self };
80 1 return;
81 3 };
82  
83 # This is a replacement of $tx->client_close() removed in Mojolicious 6.43.
84 1 ▶sub _client_close {
Subroutine _client_close: called CC=5 SCAR=20.9
85 ## no critic(ProhibitAmbiguousNames,ProhibitMagicNumbers)
86 1 my ($self, $close) = @_;
87  
88 1 my $res = $self->completed->emit('finish')->res->finish;
89 1 ▶ if ($close && !$res->code && !$res->error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($close and not $res->code and not $res->error) { } 1 0 1
elsif ($res->is_error) { } 0 0 0
Condition33%2/6
$close and not $res->code and not $res->error
!ll&&!rl&&r
001
$close and not $res->code
!ll&&!rl&&r
001
MC/DC0%0/3
$close and not $res->code and not $res->error
$close not $res->code not $res->error
Truth table25%1/4
A and not(B) and not(C)
A= $closeB= not $res->codeC= not $res->error
ABCresult
0XX0
10X0
1100
1111
90 1 $res->error({message => 'Premature connection close'});
91 }
92 elsif ($res->is_error) {
93 0 $res->error({message => $res->message, code => $res->code});
94 }
95  
96 1 return $self;
97 }
98  
99  
100 3 1; # Magic true value required at end of module
101 __END__
↑ Summary