NAME

Sub::Throttler::Periodic::EV - throttle by rate (quantity per time)

SYNOPSIS

use Sub::Throttler::Periodic::EV;

# default limit=1, period=1
my $throttle = Sub::Throttler::Periodic::EV->new(period => 0.1, limit => 42);

my $limit = $throttle->limit;
$throttle->limit(42);
my $period = $throttle->period;
$throttle->period(0.1);

# --- Activate throttle for selected subrouties
$throttle->apply_to_functions('Some::func', 'Other::func2', …);
$throttle->apply_to_methods('Class', 'method', 'method2', …);
$throttle->apply_to_methods($object, 'method', 'method2', …);
$throttle->apply_to(sub {
  my ($this, $name, @params) = @_;
  ...
  return;   # OR
  return { key1=>$quantity1, ... };
});

# --- Manual resource management
if ($throttle->try_acquire($id, $key, $quantity)) {
    ...
    $throttle->release($id);
    $throttle->release_unused($id);
}

DESCRIPTION

This is a plugin for Sub::Throttler providing simple algorithm for throttling by rate (quantity per time) of used resources.

This algorithm works like Sub::Throttler::Limit with one difference: when current time is divisible by given period value all used resources will be made available for acquiring again.

It uses EV::periodic, but will avoid keeping your event loop running when it doesn't needed anymore (if there are no acquired resources).

EXPORTS

Nothing.

INTERFACE

Sub::Throttler::Periodic::EV inherits all methods from Sub::Throttler::algo and implements the following ones.

new
my $throttle = Sub::Throttler::Periodic::EV->new;
my $throttle = Sub::Throttler::Periodic::EV->new(period => 0.1, limit => 42);

Create and return new instance of this algorithm.

Default period is 1.0, limit is 1.

See "new" in Sub::Throttler::algo for more details.

period
my $period = $throttle->period;
$throttle  = $throttle->period($period);

Get or modify current period.

limit
my $limit = $throttle->limit;
$throttle = $throttle->limit(42);

Get or modify current limit.

load
my $throttle = Sub::Throttler::Periodic::EV->load($state);

Create and return new instance of this algorithm.

See "load" in Sub::Throttler::algo for more details.

save
my $state = $throttle->save();

Return current state of algorithm needed to restore it using "load" after application restart.

See "save" in Sub::Throttler::algo for more details.

BUGS AND LIMITATIONS

No bugs have been reported.

SUPPORT

Please report any bugs or feature requests through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub-Throttler. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

You can also look for information at:

AUTHOR

Alex Efros <powerman@cpan.org>

LICENSE AND COPYRIGHT

Copyright 2014-2015 Alex Efros <powerman@cpan.org>.

This program is distributed under the MIT (X11) License: http://www.opensource.org/licenses/mit-license.php

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.