| 1 | package Sub::Throttler::Periodic::EV; | ||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | our @CARP_NOT = qw( Sub::Throttler ); | |||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = 'v0.2.10'; | |||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use parent qw( Sub::Throttler::Limit ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Sub::Throttler qw( throttle_flush ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Time::HiRes qw( time sleep ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use EV; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||
| 18 | 35 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use warnings FATAL => qw( misc ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 20 | 35 | my ($class, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||
| 21 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||
| 22 | limit => delete $opt{limit} // 1, | ||||||||||||||||||||||||||||||||||||||||
| 23 | 34 | ▶ | period => delete $opt{period} // 1, | ||||||||||||||||||||||||||||||||||||||
|
Condition71%5/7
delete $opt{"limit"} // 1
delete $opt{"period"} // 1
ref $class || $class
MC/DC100%1/1
delete $opt{"limit"} // 1
delete $opt{"limit"}
MC/DC100%1/1
delete $opt{"period"} // 1
delete $opt{"period"}
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table100%2/2
A
A= delete $opt{"limit"}
Truth table100%2/2
A
A= delete $opt{"period"}
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||
| 24 | acquired=> {}, # { $id => { $key => $quantity, … }, … } | ||||||||||||||||||||||||||||||||||||||||
| 25 | used => {}, # { $key => $quantity, … } | ||||||||||||||||||||||||||||||||||||||||
| 26 | }, ref $class || $class; | ||||||||||||||||||||||||||||||||||||||||
| 27 | 34 | ▶ | croak 'limit must be an unsigned integer' if $self->{limit} !~ /\A\d+\z/ms; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 28 | 32 | ▶ | croak 'period must be a positive number' if $self->{period} <= 0; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 29 | 32 | ▶ | croak 'bad param: '.(keys %opt)[0] if keys %opt; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 30 | 30 | weaken(my $this = $self); | |||||||||||||||||||||||||||||||||||||||
| 31 | 30 | ▶ | $self->{_t} = EV::periodic 0, $self->{period}, 0, sub { $this && $this->_tick() }; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 32 | 30 | $self->{_t}->keepalive(0); | |||||||||||||||||||||||||||||||||||||||
| 33 | 30 | return $self; | |||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub acquire { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
acquire: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | my ($self, $id, $key, $quantity) = @_; | |||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if (!$self->try_acquire($id, $key, $quantity)) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if ($quantity <= $self->{limit} && $self->{used}{$key}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$quantity <= $self->{"limit"} and $self->{"used"}{$key}
MC/DC0%0/2
$quantity <= $self->{"limit"} and $self->{"used"}{$key}
$quantity <= $self->{"limit"}
$self->{"used"}{$key}
Truth table0%0/3
A and B
A= $quantity <= $self->{"limit"}B= $self->{"used"}{$key}
| |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $time = time; | |||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $delay = int($time/$self->{period})*$self->{period} + $self->{period} - $time; | |||||||||||||||||||||||||||||||||||||||
| 42 | 0 | sleep $delay; | |||||||||||||||||||||||||||||||||||||||
| 43 | 0 | $self->_tick(); | |||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (!$self->try_acquire($id, $key, $quantity)) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | croak "$self: unable to acquire $quantity of resource '$key'"; | |||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 53 | 5 | my ($class, $state) = @_; | |||||||||||||||||||||||||||||||||||||||
| 54 | 5 | ▶ | croak 'bad state: wrong algorithm' if $state->{algo} ne __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | my $v = version->parse($state->{version}); | |||||||||||||||||||||||||||||||||||||||
| 56 | 4 | ▶ | if ($v > $VERSION) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | carp 'restoring state saved by future version'; | |||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | my $self = $class->new(limit=>$state->{limit}, period=>$state->{period}); | |||||||||||||||||||||||||||||||||||||||
| 60 | # time jump backward, no matter how much, handled like we still is in | ||||||||||||||||||||||||||||||||||||||||
| 61 | # current period, to be safe | ||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | ▶ | if (int($state->{at}/$self->{period})*$self->{period} + $self->{period} > time) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | $self->{used} = $state->{used}; | |||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | if (keys %{ $self->{used} }) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | $self->{_t}->keepalive(1); | |||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||
| 68 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | ▶ | sub period { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
period: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | my ($self, $period) = @_; | |||||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | if (1 == @_) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | return $self->{period}; | |||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | croak 'period must be a positive number' if $period <= 0; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | $self->{period} = $period; | |||||||||||||||||||||||||||||||||||||||
| 78 | 3 | $self->{_t}->set(0, $self->{period}, 0); | |||||||||||||||||||||||||||||||||||||||
| 79 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||
| 82 | 13 | ▶ | sub release { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
release: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 83 | 13 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||
| 84 | 13 | ▶ | croak sprintf '%s not acquired anything', $id if !$self->{acquired}{$id}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 85 | 10 | delete $self->{acquired}{$id}; | |||||||||||||||||||||||||||||||||||||||
| 86 | 10 | return $self; | |||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||
| 89 | 10 | ▶ | sub release_unused { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
release_unused: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 90 | 10 | my $self = shift->SUPER::release_unused(@_); | |||||||||||||||||||||||||||||||||||||||
| 91 | 7 | ▶ | if (!keys %{ $self->{used} }) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | $self->{_t}->keepalive(0); | |||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||
| 94 | 7 | return $self; | |||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | ▶ | sub save { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||
| 99 | my $state = { | ||||||||||||||||||||||||||||||||||||||||
| 100 | algo => __PACKAGE__, | ||||||||||||||||||||||||||||||||||||||||
| 101 | version => version->declare($VERSION)->numify, | ||||||||||||||||||||||||||||||||||||||||
| 102 | limit => $self->{limit}, | ||||||||||||||||||||||||||||||||||||||||
| 103 | period => $self->{period}, | ||||||||||||||||||||||||||||||||||||||||
| 104 | used => $self->{used}, | ||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | at => time, | |||||||||||||||||||||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | return $state; | |||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||
| 110 | 96 | ▶ | sub try_acquire { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
try_acquire: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 111 | 96 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||
| 112 | 96 | ▶ | if ($self->SUPER::try_acquire(@_)) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 113 | 54 | $self->{_t}->keepalive(1); | |||||||||||||||||||||||||||||||||||||||
| 114 | 54 | return 1; | |||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||
| 116 | 38 | return; | |||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||
| 119 | 24 | ▶ | sub _tick { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tick: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||
| 120 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||
| 121 | 24 | for my $id (keys %{ $self->{acquired} }) { | |||||||||||||||||||||||||||||||||||||||
| 122 | 22 | for my $key (keys %{ $self->{acquired}{$id} }) { | |||||||||||||||||||||||||||||||||||||||
| 123 | 38 | $self->{acquired}{$id}{$key} = 0; | |||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||
| 126 | # OPTIMIZATION call throttle_flush() only if amount of available | ||||||||||||||||||||||||||||||||||||||||
| 127 | # resources increased (i.e. if some sources was released) | ||||||||||||||||||||||||||||||||||||||||
| 128 | 24 | ▶ | if (keys %{ $self->{used} }) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 129 | 13 | $self->{used} = {}; | |||||||||||||||||||||||||||||||||||||||
| 130 | 13 | throttle_flush(); | |||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||
| 132 | 24 | ▶ | if (!keys %{ $self->{used} }) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 133 | 23 | $self->{_t}->keepalive(0); | |||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||
| 135 | 24 | return; | |||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||
| 139 | 2 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||||||||||||||||||||||