NAME

Sub::Throttler::Limit - throttle by quantity

SYNOPSIS

use Sub::Throttler::Limit;

my $throttle = Sub::Throttler::Limit->new(limit => 5);

$throttle->apply_to_methods(Mojo::UserAgent => qw( get post ));

DESCRIPTION

This is a plugin for Sub::Throttler providing simple algorithm for throttling by quantity of used resources.

In a nutshell it's just a hash, with resource names as keys and currently used resource quantities as values; plus one limit on maximum quantity applied to any key. Of course, each instance will have own hash/limit.

When you configure it you define which functions/methods it should throttle, and which resource name(s) and quantity(ies) of that resource(s) each function/method should acquire to run.

In basic use case you'll use one instance and configure it using "apply_to_functions" in Sub::Throttler::algo and/or "apply_to_methods" in Sub::Throttler::algo helpers - which result in any throttled function/method will need 1 resource named "default" to run. This way you'll effectively use just one counter, which will increase when any throttled function/method run and decrease when it finish, so you will have up to limit simultaneously running functions/methods (limit is usually set when you call "new").

my $throttle_tasks = Sub::Throttler::Limit->new(limit => 5);
$throttle_tasks->apply_to_functions('run_background_task');
# This code will start 5 background tasks but last two will be
# put into queue instead of being started. When any of started
# background tasks will finish first one of queued tasks will be
# started, etc. Usually you'll need event loop or something else
# to make this really works, but this has nothing with throttling.
for (1..7) {
    run_background_task();
}
# this function must support throttling
sub run_background_task { ... }

In advanced use case you may use many counters in one instance (by using "apply_to" in Sub::Throttler::algo to define different resource names/quantities for different throttled functions/methods) and have many instances (with different limit) throttling same or different functions/methods.

my $throttle_tasks = Sub::Throttler::Limit->new(limit => 5);
my $throttle_cpu   = Sub::Throttler::Limit->new(limit => 100);
# allow to simultaneously run up to 5 side_task() plus up to:
# - 5 small_task() or
# - 2 normal_task() plus 1 small_task() or
# - 1 large_task() plus 1 normal_task() or
# - 1 large_task() plus 2 small_task()
$throttle_tasks->apply_to(sub {
    my ($this, $name, @param) = @_;
    if ($name eq 'small_task') {
        return { task => 1 };
    } elsif ($name eq 'normal_task') {
        return { task => 2 };
    } elsif ($name eq 'large_task') {
        return { task => 3 };
    } elsif ($name eq 'side_task') {
        return { side => 1 };
    }
    return;
});
# and apply extra limitation on amount of simultaneously running
# side_task() depending on it first parameter (number between 1 and
# 100 showing how much CPU this side_task() will use)
$throttle_cpu->apply_to(sub {
    my ($this, $name, @param) = @_;
    if ($name eq 'side_task') {
        return { default => $param[0] };
    }
    return;
});
# here is how it will works:
large_task();   # started ($throttle_tasks 'task' == 3)
side_task(60);  # started ($throttle_tasks 'side' == 1,
                #          $throttle_cpu 'default' == 60)
small_task();   # started ($throttle_tasks 'task' == 4)
normal_task();  # delayed ($throttle_tasks 'task' + 2 > limit)
side_task(30);  # started ($throttle_tasks 'side' == 2,
                #          $throttle_cpu 'default' == 90)
side_task(30);  # delayed ($throttle_cpu 'default' + 30 > limit)

EXPORTS

Nothing.

INTERFACE

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

new
my $throttle = Sub::Throttler::Limit->new;
my $throttle = Sub::Throttler::Limit->new(limit => 42);

Create and return new instance of this algorithm.

Default limit is 1.

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

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

Get or modify current limit.

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

Create and return new instance of this algorithm.

Only limit is restored. Information about acquired resources won't be restored because there is no way to release these resources later.

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 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.