| 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
| |||||||||||||||||||||
| 29 | 45 | ▶ | die "Invalid value for $k: $args->{$k}" unless int($args->{$k}) > 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 41 | ▶ | $self->{$k} = delete $args->{$k} if exists $args->{$k}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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}
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]
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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;
}
)
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;
}
)
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 3 | return $self->{timer}; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 6 | 1; | |||||||||||||||||||