| 1 | package Sub::Throttler::Limit; | ||||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use utf8; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 7 | our @CARP_NOT = qw( Sub::Throttler Sub::Throttler::Periodic::EV ); | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 7 | our $VERSION = 'v0.2.10'; | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use parent qw( Sub::Throttler::algo ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Sub::Throttler qw( throttle_flush ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 104 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use warnings FATAL => qw( misc ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 17 | 104 | my ($class, %opt) = @_; | |||||||||||||||||||||||||||||
| 18 | my $self = bless { | ||||||||||||||||||||||||||||||
| 19 | 103 | ▶ | limit => delete $opt{limit} // 1, | ||||||||||||||||||||||||||||
|
Condition60%3/5
delete $opt{"limit"} // 1
ref $class || $class
MC/DC100%1/1
delete $opt{"limit"} // 1
delete $opt{"limit"}
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table100%2/2
A
A= delete $opt{"limit"}
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||
| 20 | acquired=> {}, # { $id => { $key => $quantity, … }, … } | ||||||||||||||||||||||||||||||
| 21 | used => {}, # { $key => $quantity, … } | ||||||||||||||||||||||||||||||
| 22 | }, ref $class || $class; | ||||||||||||||||||||||||||||||
| 23 | 103 | ▶ | croak 'limit must be an unsigned integer' if $self->{limit} !~ /\A\d+\z/ms; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 24 | 101 | ▶ | croak 'bad param: '.(keys %opt)[0] if keys %opt; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 25 | 99 | return $self; | |||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 8 | ▶ | sub acquire { | ||||||||||||||||||||||||||||
|
Subroutine
acquire: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||
| 29 | 8 | my ($self, $id, $key, $quantity) = @_; | |||||||||||||||||||||||||||||
| 30 | 8 | ▶ | if (!$self->try_acquire($id, $key, $quantity)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 31 | 8 | croak "$self: unable to acquire $quantity of resource '$key'"; | |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||
| 33 | 0 | return $self; | |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||
| 36 | 34 | ▶ | sub limit { | ||||||||||||||||||||||||||||
|
Subroutine
limit: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 37 | 34 | my ($self, $limit) = @_; | |||||||||||||||||||||||||||||
| 38 | 34 | ▶ | if (1 == @_) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 39 | 4 | return $self->{limit}; | |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||
| 41 | 30 | ▶ | croak 'limit must be an unsigned integer' if $limit !~ /\A\d+\z/ms; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 42 | # OPTIMIZATION call throttle_flush() only if amount of available | ||||||||||||||||||||||||||||||
| 43 | # resources increased (i.e. limit was increased) | ||||||||||||||||||||||||||||||
| 44 | 30 | my $resources_increases = $self->{limit} < $limit; | |||||||||||||||||||||||||||||
| 45 | 30 | $self->{limit} = $limit; | |||||||||||||||||||||||||||||
| 46 | 30 | ▶ | if ($resources_increases) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 47 | 14 | throttle_flush(); | |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||
| 49 | 30 | return $self; | |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | 3 | ▶ | sub load { | ||||||||||||||||||||||||||||
|
Subroutine
load: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 53 | 3 | my ($class, $state) = @_; | |||||||||||||||||||||||||||||
| 54 | 3 | ▶ | croak 'bad state: wrong algorithm' if $state->{algo} ne __PACKAGE__; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 55 | 2 | my $v = version->parse($state->{version}); | |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | if ($v > $VERSION) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 57 | 1 | carp 'restoring state saved by future version'; | |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | 2 | my $self = $class->new(limit=>$state->{limit}); | |||||||||||||||||||||||||||||
| 60 | 2 | return $self; | |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | 75 | ▶ | sub release { | ||||||||||||||||||||||||||||
|
Subroutine
release: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 64 | 75 | return _release(@_); | |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | 49 | ▶ | sub release_unused { | ||||||||||||||||||||||||||||
|
Subroutine
release_unused: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 68 | 49 | return _release(@_); | |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | sub save { | ||||||||||||||||||||||||||||
|
Subroutine
save: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 72 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 73 | my $state = { | ||||||||||||||||||||||||||||||
| 74 | algo => __PACKAGE__, | ||||||||||||||||||||||||||||||
| 75 | version => version->declare($VERSION)->numify, | ||||||||||||||||||||||||||||||
| 76 | limit => $self->{limit}, | ||||||||||||||||||||||||||||||
| 77 | used => $self->{used}, | ||||||||||||||||||||||||||||||
| 78 | 3 | at => time, | |||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||||
| 80 | 3 | return $state; | |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||
| 83 | 401 | ▶ | sub try_acquire { | ||||||||||||||||||||||||||||
|
Subroutine
try_acquire: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 84 | 401 | my ($self, $id, $key, $quantity) = @_; | |||||||||||||||||||||||||||||
| 85 | croak sprintf '%s already acquired %s', $id, $key | ||||||||||||||||||||||||||||||
| 86 | 401 | ▶ | if $self->{acquired}{$id} && exists $self->{acquired}{$id}{$key}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"acquired"}{$id} and exists $self->{"acquired"}{$id}{$key}
MC/DC100%2/2
$self->{"acquired"}{$id} and exists $self->{"acquired"}{$id}{$key}
$self->{"acquired"}{$id}
exists $self->{"acquired"}{$id}{$key}
Truth table100%3/3
A and B
A= $self->{"acquired"}{$id}B= exists $self->{"acquired"}{$id}{$key}
| |||||||||||||||||||||||||||||||
| 87 | 396 | ▶ | croak 'quantity must be positive' if $quantity <= 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | 380 | ▶ | my $used = $self->{used}{$key} || 0; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'used'}{$key} || 0
MC/DC100%1/1
$self->{'used'}{$key} || 0
$self->{'used'}{$key}
Truth table100%2/2
A
A= $self->{'used'}{$key}
| |||||||||||||||||||||||||||||||
| 90 | 380 | ▶ | if ($used + $quantity > $self->{limit}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 91 | 165 | return; | |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||
| 93 | 215 | $self->{used}{$key} = $used + $quantity; | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | 215 | $self->{acquired}{$id}{$key} = $quantity; | |||||||||||||||||||||||||||||
| 96 | 215 | return 1; | |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | 124 | ▶ | sub _release { | ||||||||||||||||||||||||||||
|
Subroutine
_release: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||
| 100 | 124 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||
| 101 | 124 | ▶ | croak sprintf '%s not acquired anything', $id if !$self->{acquired}{$id}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | 119 | for my $key (keys %{ $self->{acquired}{$id} }) { | |||||||||||||||||||||||||||||
| 104 | 133 | my $quantity = $self->{acquired}{$id}{$key}; | |||||||||||||||||||||||||||||
| 105 | 133 | $self->{used}{$key} -= $quantity; | |||||||||||||||||||||||||||||
| 106 | # clean up (avoid memory leak in long run with unique keys) | ||||||||||||||||||||||||||||||
| 107 | 133 | ▶ | if (!$self->{used}{$key}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | 111 | delete $self->{used}{$key}; | |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||
| 111 | 119 | delete $self->{acquired}{$id}; | |||||||||||||||||||||||||||||
| 112 | 119 | throttle_flush(); | |||||||||||||||||||||||||||||
| 113 | 119 | return $self; | |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||
| 117 | 7 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||||