Mojo::Promise::Limiter - limit outstanding calls to Mojo::Promise
use Mojo::Promise::Limiter; use Mojo::Promise; use Mojo::IOLoop; my $limiter = Mojo::Promise::Limiter->new(2); my @job = 'a' .. 'e'; Mojo::Promise->all( map { my $name = $_; $limiter->limit(sub { job($name) }) } @job, )->then(sub { my @result = @_; warn "\n"; warn "results: ", (join ", ", map { $_->[0] } @result), "\n"; })->wait; sub job { my $name = shift; my $text = "job $name"; warn "started $text\n"; return Mojo::Promise->new(sub { my $resolve = shift; Mojo::IOLoop->timer(0.1 => sub { warn " $text finished\n"; $resolve->($text); }); }); }
will outputs:
started job a started job b job a finished job b finished started job c started job d job c finished job d finished started job e job e finished results: job a, job b, job c, job d, job e
Mojo::Promise::Limiter allows you to limit outstanding calls to Mojo::Promises. This is a Perl port of https://github.com/featurist/promise-limit.
Mojo::Promise
I sometimes want to limit outstanding calls to reduce load on external services, or to reduce some resource (cpu, memory, etc) usage. For example, without some mechanism to limit outstanding calls, the following code open 5 connections to metacpan.
my $http = Mojo::UserAgent->new; Mojo::Promise->all_settled( $http->get_p("https://metacpan.org/release/App-cpm"), $http->get_p("https://metacpan.org/release/Minilla"), $http->get_p("https://metacpan.org/release/Mouse"), $http->get_p("https://metacpan.org/release/Perl6-Build"), $http->get_p("https://metacpan.org/release/Test-CI"), )->wait;
With Mojo::Promise::Limiter, you can easily limit concurrent connections. See eg/http.pl for real world example.
Mojo::Promise::Limiter inherits all events from Mojo::EventEmitter and can emit the following new ones.
$limiter->on(run => sub { my ($limiter, $name) = @_; ...; });
$limiter->on(remove => sub { my ($limiter, $name) = @_; ...; });
$limiter->on(queue => sub { my ($limiter, $name) = @_; ...; });
$limiter->on(dequeue => sub { my ($limiter, $name) = @_; ...; });
Mojo::Promise::Limiter inherits all methods from Mojo::EventEmitter and implements the following new ones.
my $limiter = Mojo::Promise::Limiter->new($concurrency);
Constructs Mojo::Promise::Limiter object.
my $promise = $limiter->limit($sub); my $promise = $limiter->limit($sub, $name);
Limits calls to $sub based on concurrency, where $sub is a subroutine reference that must return a promise, and $name is an optional argument which will be used in events. $limiter->limit($sub) returns a promise that resolves or rejects the same value or error as $sub. All subroutine references are executed in the same order in which they were passed to $limiter->limit method.
$sub
concurrency
$name
$limiter->limit($sub)
$limiter->limit
https://github.com/featurist/promise-limit
Shoichi Kaji <skaji@cpan.org>
Copyright 2020 Shoichi Kaji <skaji@cpan.org>
The ISC License
To install Mojo::Promise::Limiter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::Promise::Limiter
CPAN shell
perl -MCPAN -e shell install Mojo::Promise::Limiter
For more information on module installation, please visit the detailed CPAN module installation guide.