NAME

Sub::Throttler::Rate::AnyEvent - throttle by rate (quantity per time)

VERSION

This document describes Sub::Throttler::Rate::AnyEvent version v0.2.10

SYNOPSIS

use Sub::Throttler::Rate::AnyEvent;

# default limit=1, period=1
my $throttle = Sub::Throttler::Rate::AnyEvent->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: resources acquired earlier than given period value will be made available for acquiring again.

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

For throttling sync subs this algorithm can be used even without event loop, but if you'll use huge amount of unique resource names in long-running application then some memory will leak.

EXPORTS

Nothing.

INTERFACE

Sub::Throttler::Rate::AnyEvent inherits all methods from Sub::Throttler::algo and implements the following ones.

new

my $throttle = Sub::Throttler::Rate::AnyEvent->new;
my $throttle = Sub::Throttler::Rate::AnyEvent->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.

NOTE: After decreasing limit in some case maximum of limits used while current period may be used instead of current limit for next period.

load

my $throttle = Sub::Throttler::Rate::AnyEvent->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.

SUPPORT

Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker at https://github.com/powerman/perl-Sub-Throttler/issues. You will be notified automatically of any progress on your issue.

Source Code

This is open source software. The code repository is available for public review and contribution under the terms of the license. Feel free to fork the repository and submit pull requests.

https://github.com/powerman/perl-Sub-Throttler

git clone https://github.com/powerman/perl-Sub-Throttler.git

Resources

AUTHOR

Alex Efros <powerman@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2014- by Alex Efros <powerman@cpan.org>.

This is free software, licensed under:

The MIT (X11) License