« blib/lib/Bot/Cobalt/IRC/Role/AdminCmds.pm
↑ Summary
blib/lib/Bot/Cobalt/Plugin/Games/RockPaperScissors.pm »
| 1 | package Bot::Cobalt::IRC::FloodChk; | ||||||||||||||||||||||||||||
| 2 | $Bot::Cobalt::IRC::FloodChk::VERSION = '0.021003'; | ||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Bot::Cobalt::Common ':types'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use List::Objects::WithUtils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | ## _fqueue->{$context}->{$key} = array() | ||||||||||||||||||||||||||||
| 14 | ## FIXME Should probably be an obj ... | ||||||||||||||||||||||||||||
| 15 | has _fqueue => ( | ||||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||
| 17 | lazy => 1, | ||||||||||||||||||||||||||||
| 18 | 0 | ▶ | default => sub { +{} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | has count => ( is => 'rw', isa => Num, required => 1 ); | ||||||||||||||||||||||||||||
| 22 | has in => ( is => 'rw', isa => Num, required => 1 ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 0 | my ($self, $context, $key) = @_; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | return unless defined $context and defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $context and defined $key
MC/DC0%0/2
defined $context and defined $key
defined $context
defined $key
Truth table0%0/3
A and B
A= defined $contextB= defined $key
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my $thisq = $self->_fqueue->{$context}->{$key} //= array; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->_fqueue->{$context}{$key} //= array()
MC/DC0%0/2
$self->_fqueue->{$context}{$key} //= array()
$self->_fqueue->{$context}{$key}
array()
Truth table0%0/3
A //= B
A= $self->_fqueue->{$context}{$key}B= array()
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if ((my $pending = $thisq->count) >= $self->count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | my $oldest_ts = $thisq->head; | |||||||||||||||||||||||||||
| 33 | 0 | my $ev_c = $self->count; | |||||||||||||||||||||||||||
| 34 | 0 | my $ev_sec = $self->in; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | my $delayed = | |||||||||||||||||||||||||||
| 37 | ($oldest_ts + ($pending * $ev_sec / $ev_c) ) | ||||||||||||||||||||||||||||
| 38 | - Time::HiRes::time(); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | ## Too many events in this time window: | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | return $delayed if $delayed > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | ## ...otherwise shift and continue: | ||||||||||||||||||||||||||||
| 44 | 0 | $thisq->shift; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | ## Safe to push this ev, no delay: | ||||||||||||||||||||||||||||
| 48 | 0 | $thisq->push( Time::HiRes::time() ); | |||||||||||||||||||||||||||
| 49 | 0 | return 0 | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my ($self, $context, $key) = @_; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | confess "clear() needs a context specified" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | unless defined $context; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return unless exists $self->_fqueue->{$context}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | return delete $self->_fqueue->{$context}->{$key} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | if defined $key; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | delete $self->_fqueue->{$context} | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub expire { | ||||||||||||||||||||||||||
|
Subroutine
expire: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | ## Clear keys when recent_event_time - time > $self->in | ||||||||||||||||||||||||||||
| 67 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | CONTEXT: for my $context (keys %{ $self->_fqueue } ) { | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | KEY: for my $key (keys %{ $self->_fqueue->{$context} } ) { | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $events = $self->_fqueue->{$context}->{$key}; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $latest_time = $events->get(-1) // next KEY; | ||||||||||||||||||||||||||
|
Condition0%0/2
$events->get(-1) // (next KEY)
MC/DC0%0/1
$events->get(-1) // (next KEY)
$events->get(-1)
Truth table0%0/2
A
A= $events->get(-1)
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (Time::HiRes::time() - $latest_time > $self->in) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | ## It's been more than ->in seconds since latest event was | ||||||||||||||||||||||||||||
| 78 | ## noted. We can clear() this entry. | ||||||||||||||||||||||||||||
| 79 | 0 | $self->clear($context, $key); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } # KEY | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | unless (keys %{ $self->_fqueue->{$context} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | ## Nothing left for this context. | ||||||||||||||||||||||||||||
| 85 | 0 | $self->clear($context); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1; | ||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||