|
NAMEAlgorithm::FloodControl - Limit event processing to count/time ratio. SYNOPSISFunctional interface use Algorithm::FloodControl;
my $wait = flood_check( 5, 60, 'FLOOD EVENT NAME' );
if( $wait ) {
print "Please wait $wait sec. before requesting this resource again.";
} else {
print "Ok, here you are.";
}
Object-oriented interface use Algorithm::FloodControl ();
my $flood_control = Algorithm::FloodControl->new(
storage => $memd,
limits => {
limit_name => [
{
period => 60,
attempts => 5,
}, {
period => 3600,
attempts => 30,
}
]
}
);
$flood_control->register_attempt( limit_name => 'vasja_pupkin' );
my $attempt_count = $flood_control->get_attempt_count( limit_name => 'vasja_pupkin' ); # 1
if ( $flood_control->is_user_overrated( limit_name => 'vasja_pupkin' ) ) {
die "Ненене, Девид Блейн (:";
}
DESCRIPTION"Flood control" method is used to restrict the number of events to happen or to be processed in specific perion of time. Few examples are: web server can limit requsets number to a page or you may want to receive no more than 10 SMS messages on your GSM Phone per hour. Applications of this method are unlimited. FUNCTIONSThis module exports several functions:
METHODS
EXAMPLECGI script is very usefull as example because it has all elements of Algorithm::FloodControl use: #!/usr/bin/perl
use strict;
use Storable qw( store retrieve ); # used to save/restore the internal data
use LockFile::Simple qw( lock unlock ); # used to lock the data file
use Algorithm::FloodControl;
my $flood_file = "/tmp/flood-cgi.dat";
lock( $flood_file ); # lock the storage
my $FLOOD = retrieve( $flood_file ) if -r $flood_file; # read storage data
flood_storage( $FLOOD ) if $FLOOD; # load storage
my $wait = flood_check( 5, 60, 'FLOOD TEST CGI' ); # check for flood
store( flood_storage(), $flood_file ); # save storage data
unlock( $flood_file ); # unlock the file
print "Content-type: text/plain\n\n";
if( $wait ) {
print "Please wait $wait seconds before requesting this page again.\n";
exit;
}
print "Hello, this is main script here\n";
This example is just one of very large number of cases where flood control can be useful. I used it in IRC bots, email notifications, web site updates, etc. AUTHOR Vladi Belperchinov-Shabanski "Cade" - up to 1.00
<cade@biscom.net> <cade@datamax.bg> <cade@cpan.org>
http://cade.datamax.bg
Andrey Kostenko "GuGu" <andrey@kostenko.name> - 1.00 - 2.00
http://kostenko.name
BUGSNo bugs. NOTESCOPYRIGHT & LICENSEThis program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. VERSION$Id: FloodControl.pm 7 2008-11-06 12:51:33Z gugu $ POD ERRORSHey! The above document had some coding errors, which are explained below:
|