CustomerIO/RateLimiter.pm

filter: Statementstmt 100% 52 / 52 Branchbran 80.0% 8 / 10 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 91.8% 79 / 86 CC 9 SCAR 8.5
CC
9
Cov
94%
_init
5.0
_current_queue
2.0
_start_timer
2.0
CRAP 2.3
« CustomerIO.pm ↑ Summary CustomerIO/Customer.pm »
1 package WebService::Async::CustomerIO::RateLimiter;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 our $VERSION = '0.002'; ## VERSION
13  
14 =head1 NAME
15 WebService::Async::CustomerIO::RateLimitter - This class provide possibility to limit amount
16 of request in time interval
17  
18 =head1 SYNOPSIS
19  
20  
21 =head1 DESCRIPTION
22  
23 =cut
24  
25 25 ▶sub _init {
Subroutine _init: called CC=5 SCAR=16.1
26 25 my ($self, $args) = @_;
27 25 for my $k (qw(limit interval)) {
28 47 ▶ die "Missing required argument: $k" unless exists $args->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$k} 2 45 47
29 45 ▶ die "Invalid value for $k: $args->{$k}" unless int($args->{$k}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless int $args->{$k} > 0 4 41 45
30 41 ▶ $self->{$k} = delete $args->{$k} if exists $args->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$k} 41 0 41
31 }
32  
33 19 $self->{queue} = [];
34 19 $self->{counter} = 0;
35  
36 19 return $self->next::method($args);
37 }
38  
39 =head2 interval
40 =cut
41  
42 2 ▶sub interval { return shift->{interval} }
Subroutine interval: called CC=1 SCAR=0.0
Pod documented
43  
44 =head2 limit
45 =cut
46  
47 32 ▶sub limit { return shift->{limit} }
Subroutine limit: called CC=1 SCAR=0.0
Pod documented
48  
49 =head2 acquire
50  
51 Method checks availability for free slot.
52 It returns future, when slot will be available, then future will be resolved.
53  
54 =cut
55  
56 4 ▶sub acquire {
Subroutine acquire: called CC=2 SCAR=6.9
Pod documented
57 4 my ($self) = @_;
58  
59 4 $self->_start_timer;
60 4 ▶ return Future->done if ++$self->{counter} <= $self->limit;
Branch: 1 branch
BranchTrueFalseTotal
if ++$self->{"counter"} <= $self->limit 2 2 4
61  
62 2 my $current = $self->_current_queue;
63 2 $current->{counter}++;
64 2 return $current->{future};
65 }
66  
67 14 ▶sub _current_queue {
Subroutine _current_queue: called CC=2 SCAR=7.0
68 14 my ($self) = @_;
69  
70 # +1 for getting correct position for edge cases like: limit 2, counter 4, should be 0
71 14 my $pos = int(($self->{counter} - ($self->limit + 1)) / $self->limit);
72  
73 14 ▶ $self->{queue}[$pos] //= {
Condition50%1/2
$self->{"queue"}[$pos] //= {"future", $self->loop->new_future, "counter", 0}
l!l
014
MC/DC0%0/1
$self->{"queue"}[$pos] //= {"future", $self->loop->new_future, "counter", 0}
$self->{"queue"}[$pos]
Truth table50%1/2
A
A= $self->{"queue"}[$pos]
Aresult
11
00
74 future => $self->loop->new_future,
75 counter => 0
76 };
77  
78 14 return $self->{queue}[$pos];
79 }
80  
81 3 ▶sub _start_timer {
Subroutine _start_timer: called CC=2 SCAR=7.0
82 3 my ($self) = @_;
83  
84 $self->{timer} //= $self->loop->delay_future(
85 after => $self->interval,
86 )->on_ready(
87 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
88 1 $self->{counter} = 0;
89 1 delete $self->{timer};
90  
91 1 ▶ return unless @{$self->{queue}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"queue"};} 0 1 1
92  
93 1 $self->_start_timer;
94  
95 1 my $current = shift @{$self->{queue}};
96 1 $self->{counter} = $current->{counter};
97 1 $current->{future}->done;
98 3 ▶ });
Condition66%2/3
$self->{"timer"} //= $self->loop->delay_future("after", $self->interval)->on_ready(sub { $self->{"counter"} = 0; delete $self->{"timer"}; return unless @{$self->{"queue"};}; $self->_start_timer; my $current = shift @{$$self{"queue"};}; $self->{"counter"} = $current->{"counter"}; $current->{"future"}->done; } )
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"timer"} //= $self->loop->delay_future("after", $self->interval)->on_ready(sub { $self->{"counter"} = 0; delete $self->{"timer"}; return unless @{$self->{"queue"};}; $self->_start_timer; my $current = shift @{$$self{"queue"};}; $self->{"counter"} = $current->{"counter"}; $current->{"future"}->done; } )
$self->{"timer"} $self->loop->delay_future("after", $self->interval)->on_ready(sub { $self->{"counter"} = 0; delete $self->{"timer"}; return unless @{$self->{"queue"};}; $self->_start_timer; my $current = shift @{$$self{"queue"};}; $self->{"counter"} = $current->{"counter"}; $current->{"future"}->done; } )
Truth table66%2/3
A //= B
A= $self->{"timer"}B= $self->loop->delay_future("after", $self->interval)->on_ready(sub { $self->{"counter"} = 0; delete $self->{"timer"}; return unless @{$self->{"queue"};}; $self->_start_timer; my $current = shift @{$$self{"queue"};}; $self->{"counter"} = $current->{"counter"}; $current->{"future"}->done; } )
ABresult
1X1
011
000
99  
100 3 return $self->{timer};
101 }
102  
103 6 1;
« CustomerIO.pm ↑ Summary CustomerIO/Customer.pm »