Data::Throttler

Limit data throughput
Download

Data::Throttler Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Michael Schilli
  • Publisher web site:
  • http://search.cpan.org/~mschilli/

Data::Throttler Tags


Data::Throttler Description

Limit data throughput Data::Throttler is a Perl module that helps solving throttling tasks like "allow a single IP only to send 100 emails per hour". It provides an optionally persistent data store to keep track of what happened before and offers a simple yes/no interface to an application, which can then focus on performing the actual task (like sending email) or suppressing/postponing it.When defining a throttler, you can tell it to keep its internal data structures in memory: # in-memory throttler my $throttler = Data::Throttler->new( max_items => 100, interval => 3600, );However, if the data structures need to be maintained across different invocations of a script or several instances of scripts using the throttler, using a persistent database is required: # persistent throttler my $throttler = Data::Throttler->new( max_items => 100, interval => 3600, backend => "YAML", backend_options => { db_file => "/tmp/mythrottle.yml", }, );The call above will reuse an existing backend store, given that the max_items and interval settings are compatible and leave the stored counter bucket chain contained therein intact. To specify that the backend store should be rebuilt and all counters be reset, use the reset => 1 option of the Data::Throttler object constructor.In the simplest case, Data::Throttler just keeps track of single events. It allows a certain number of events per time frame to succeed and it recommends to block the rest: if($throttler->try_push()) { print "Item can be pushed "; } else { print "Item needs to wait "; }When throttling different categories of items, like attempts to send emails by IP address of the sender, a key can be used: if($throttler->try_push( key => "192.168.0.1" )) { print "Item can be pushed "; } else { print "Item needs to wait "; }In this case, each key will be tracked separately, even if the quota for one key is maxed out, other keys will still succeed until their quota is reached.SYNOPSIS use Data::Throttler; ### Simple: Limit throughput to 100 per hour my $throttler = Data::Throttler->new( max_items => 100, interval => 3600, ); if($throttler->try_push()) { print "Item can be pushed "; } else { print "Item needs to wait "; } ### Advanced: Use a persistent data store and throttle by key: my $throttler = Data::Throttler->new( max_items => 100, interval => 3600, backend => "YAML", backend_options => { db_file => "/tmp/mythrottle.yml", }, ); if($throttler->try_push(key => "somekey")) { print "Item can be pushed "; } Requirements: · Perl


Data::Throttler Related Software