| 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
Condition50%3/6
3 == @_ and $cb
3 == @_ and $cb
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
Truth table100%3/3
A and B
A= 3 == @_B= $cb
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ($IsDestroying{ $self }) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%2/4
&throttle_me or return undef
&throttle_me or return undef
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
Truth table100%2/2
A
A= &throttle_me
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ($self, $tx, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($cb) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($IsDestroying{ $self }) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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} || {}
MC/DC0%0/1
delete $Delayed{$self} || {}
delete $Delayed{$self}
Truth table50%1/2
A
A= delete $Delayed{$self}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
$close and not $res->code and not $res->error
$close and not $res->code
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||