Role/Object/RateLimiter.pm

filter: Statementstmt 100% 24 / 24 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 95.2% 40 / 42 CC 5 SCAR 6.5
CC
5
Cov
94%
delayed
3.0
clear_delayed
2.0
get_rate_limiter
2.0
CRAP 1.9
« Object/RateLimiter.pm ↑ Summary
1 package Role::Object::RateLimiter;
2 1 $Role::Object::RateLimiter::VERSION = '1.002001';
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Object::RateLimiter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub delayed {
11 7 ▶ my ($self, %args) = @_;
Subroutine delayed: called CC=3 SCAR=11.0
Pod documented
12 7 ▶ return $self->{__rl} = Object::RateLimiter->new(%args) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 1 6 7
13 confess
14 'Attempted to call ->delayed without a configured ratelimiter',
15 ' -- perhaps you wanted ->delayed(events => $x, seconds => $y)'
16 6 ▶ unless defined $self->{__rl};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"__rl"} 1 5 6
17 $self->{__rl}->delay
18 5 }
19  
20 sub clear_delayed {
21 1 ▶ my ($self) = @_;
Subroutine clear_delayed: called CC=2 SCAR=7.1
Pod documented
22 1 ▶ return unless defined $self->{__rl};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"__rl"} 0 1 1
23 $self->{__rl}->clear
24 1 }
25  
26 sub get_rate_limiter {
27 2 ▶ my ($self) = @_;
Subroutine get_rate_limiter: called CC=2 SCAR=7.1
Pod documented
28 2 ▶ return unless defined $self->{__rl};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"__rl"} 0 2 2
29 $self->{__rl}
30 2 }
31  
32 1 1;
33  
34 =pod
35  
36 =head1 NAME
37  
38 Role::Object::RateLimiter - Add a rate limiter to your class
39  
40 =head1 SYNOPSIS
41  
42 package My::Responder;
43 use Moo;
44 with 'Role::Object::RateLimiter';
45  
46 # Set up our rate limiter (any time before attempting to use it):
47 has limit_events => (
48 is => 'ro',
49 required => 1,
50 );
51  
52 has limit_seconds => (
53 is => 'ro',
54 required => 1,
55 );
56  
57 sub BUILD {
58 my ($self) = @_;
59 $self->delayed(
60 events => $self->limit_events,
61 seconds => $self->limit_seconds
62 );
63 }
64  
65 # do_stuff but only if we're not rate-limited:
66 sub respond {
67 my ($self) = @_;
68 if (my $delay = $self->delayed) {
69 sleep $delay
70 }
71 $self->do_stuff
72 }
73  
74 sub respond_nonblocking {
75 my ($self) = @_;
76 return if $self->delayed;
77 $self->do_stuff
78 }
79  
80 =head1 DESCRIPTION
81  
82 This is a small role wrapping L<Object::RateLimiter> to make it slightly more
83 convenient to add rate limiting to objects.
84  
85 Currently only C<HASH>-type objects are supported.
86  
87 Although the L</SYNOPSIS> uses L<Moo>, this role uses L<Role::Tiny> and can
88 also be composed via L<Role::Tiny::With>.
89  
90 See L<Object::RateLimiter> for more details.
91  
92 =head2 delayed
93  
94 # Set up the rate limiter:
95 $self->delayed(events => 4, seconds => 5);
96  
97 # Check if this event should be delayed;
98 # returns number of seconds to wait:
99 my $delay = $self->delayed;
100  
101 If called with arguments, passes them to L<Object::RateLimiter>'s constructor
102 and returns the newly initialized rate limiter; any existing rate limiter is
103 replaced.
104  
105 If called without arguments, records an entry in the L<Object::RateLimiter>'s
106 event history and returns the number of seconds until the event should be
107 allowed (or zero if not delayed).
108  
109 See L<Object::RateLimiter/delay>.
110  
111 =head2 clear_delayed
112  
113 Clear the current event history.
114  
115 =head2 get_rate_limiter
116  
117 Returns the current L<Object::RateLimiter> instance.
118  
119 =head1 AUTHOR
120  
121 Jon Portnoy <avenj@cobaltirc.org>
122  
123 =cut
« Object/RateLimiter.pm ↑ Summary