NAME

MojoX::UserAgent::Throttler - add throttling support to Mojo::UserAgent

SYNOPSIS

use MojoX::UserAgent::Throttler;
use Sub::Throttler::SOME_ALGORITHM;

my $throttle = Sub::Throttler::SOME_ALGORITHM->new(...);
$throttle->apply_to_methods('Mojo::UserAgent');

DESCRIPTION

This module helps throttle Mojo::UserAgent using Sub::Throttler.

While in most cases this module isn't needed and existing functionality of Sub::Throttler is enough to throttle Mojo::UserAgent, there are two special cases which needs extra handling - when Mojo::UserAgent object is destroyed while there are delayed requests, and when new async requests start while destroying Mojo::UserAgent object.

To handle these cases it won't be enough to just do usual:

throttle_it('Mojo::UserAgent::start');

Instead you'll have to write "custom wrapper" in Sub::Throttler plus add wrapper for Mojo::UserAgent::DESTROY. Both are provided by this module and activated when you load it.

So, when using this module you shouldn't manually call throttle_it() like shown above - just use this module and then setup throttling algorithms as you need and apply them to "start" in Mojo::UserAgent - this will let you throttle all (sync/async, GET/POST/etc.) requests. Use "apply_to" in Sub::Throttler::algo to customize throttling based on request method, hostname, etc.

EXAMPLE

use MojoX::UserAgent::Throttler;
use Sub::Throttler::Limit;
my $throttle = Sub::Throttler::Limit->new(limit=>5);
# Example policy:
# - don't throttle sync calls
# - throttle async GET requests by host
# - throttle other async requests by method, per $ua object
# I.e. allow up to 5 parallel GET requests to each host globally for
# all Mojo::UserAgent objects plus up to 5 parallel non-GET requests
# per each Mojo::UserAgent object.
$throttle->apply_to(sub {
    my ($this, $name, @params) = @_;
    if (ref $this eq 'Mojo::UserAgent') {
        my ($tx, $cb) = @params;
        if (!$cb) {
            return;
        } elsif ('GET' eq uc $tx->req->method) {
            return { host => $tx->req->url->host };
        } else {
            return { ua_method => "$this " . uc $tx->req->method };
        }
    }
    return;
});

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=MojoX-UserAgent-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.