App-Cerberus

 view release on metacpan or  search on metacpan

lib/App/Cerberus/Plugin/Throttle.pm  view on Meta::CPAN

package App::Cerberus::Plugin::Throttle;
$App::Cerberus::Plugin::Throttle::VERSION = '0.11';
use strict;
use warnings;
use Carp;
use parent 'App::Cerberus::Plugin';
use Net::IP::Match::Regexp qw(create_iprange_regexp_depthfirst match_ip);
use DateTime();

my %Periods = (
    second => 5,
    minute => 4,
    hour   => 3,
    day    => 2,
    month  => 1
);

my %Expire = (
    second => 1,
    minute => 60,
    hour   => 3600,
    day    => 24 * 3600,
    month  => 31 * 24 * 3600
);

my @Days = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );

my %Sleep;
%Sleep = (
    second => sub { shift->{second_penalty} },
    minute => sub { 60 - $_[6] },
    hour   => sub { ( 60 - $_[5] ) * 60 - $Sleep{minute}->(@_) },
    day    => sub { ( 24 - $_[4] ) * 3600 - $Sleep{hour}->(@_) },
    month  => sub {
        my ( undef, $y, $M, $d ) = @_;
        my $days = $Days[ $M - 1 ];
        $days++ if $M == 2 and !( $y % 4 ) && ( $y % 100 ) || !( $y % 400 );
        ( $days - $d ) * 24 * 3600 - $Sleep{day}->(@_);
    },
);

#===================================
sub init {
#===================================
    my ( $self, $conf ) = @_;

    $self->{store} = $self->_init_store( $conf->{store} );
    $self->{second_penalty} = $conf->{second_penalty} || 1;

    my $events = $conf->{events} || { default => $conf->{ranges} };
    croak "No (ranges) have been specified"
        unless %$events;

    for ( keys %$events ) {
        $self->{events}{$_} = $self->_parse_ranges( $_, $events->{$_} );
    }
}

#===================================
sub _parse_ranges {
#===================================
    my ( $self, $event, $conf ) = @_;

    my ( %range_limits, %group, @ips );

    for my $name ( keys %$conf ) {

        $group{$name} = 1



( run in 0.677 second using v1.01-cache-2.11-cpan-39bf76dae61 )