Async.pm

filter: Statementstmt 100% 63 / 63 Branchbran 80.0% 16 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 16.6% 1 / 6 totaltotal 91.2% 94 / 103 CC 13 SCAR 12.9
CC
13
Cov
95%
BUILD
7.0
do
5.0
process_queue
3.0
CRAP 3.6
↑ Summary Async/Worker.pm »
1 package Broker::Async;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Broker::Async::Worker;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Broker::Async - broker tasks for multiple workers
11  
12 =for html <a href="https://travis-ci.org/mark-5/p5-broker-async"><img src="https://travis-ci.org/mark-5/p5-broker-async.svg?branch=master"></a>
13  
14 =head1 SYNOPSIS
15  
16 my @workers;
17 for my $uri (@uris) {
18 my $client = SomeClient->new($uri);
19 push @workers, sub { $client->request(@_) };
20 }
21  
22 my $broker = Broker::Async->new(workers => \@workers);
23 for my $future (map $broker->do($_), @requests) {
24 my $result = $future->get;
25 ...
26 }
27  
28 =head1 DESCRIPTION
29  
30 This module brokers tasks for multiple asynchronous workers. A worker can be any code reference that returns a L<Future>, representing work awaiting completion.
31  
32 Some common use cases include throttling asynchronous requests to a server, or delegating tasks to a limited number of processes.
33  
34 =cut
35  
36 6 our $VERSION = "0.0.5"; # __VERSION__
37  
38 =head1 ATTRIBUTES
39  
40 =head2 workers
41  
42 An array ref of workers used for handling tasks.
43 Can be a code reference, a hash ref of L<Broker::Async::Worker> arguments, or a L<Broker::Async::Worker> object.
44 Every invocation of a worker must return a L<Future> object.
45  
46 Under the hood, code and hash references are simply used to instantiate a L<Broker::Async::Worker> object.
47 See L<Broker::Async::Worker> for more documentation about how these parameters are used.
48  
49 =cut
50  
51 use Class::Tiny qw( workers ), {
52 5 ▶ queue => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 6 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 =head1 METHODS
56  
57 =head2 new
58  
59 my $broker = Broker::Async->new(
60 workers => [ sub { ... }, ... ],
61 );
62  
63 =cut
64  
65 sub active {
66 118 ▶ my ($self) = @_;
Subroutine active: called CC=1 SCAR=0.0
Pod undocumented
67 118 return grep { $_->active } @{ $self->workers };
68 }
69  
70 sub available {
71 392 ▶ my ($self) = @_;
Subroutine available: called CC=1 SCAR=0.0
Pod undocumented
72 392 return grep { $_->available } @{ $self->workers };
73 }
74  
75 sub BUILD {
76 10 ▶ my ($self) = @_;
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
77 10 for my $name (qw( workers )) {
78 10 ▶ croak "$name attribute required" unless defined $self->$name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$name 0 10 10
79 }
80  
81 10 my $workers = $self->workers;
82 10 ▶ croak "workers attribute must be an array ref: received $workers"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $workers eq "ARRAY" 0 10 10
83 unless ref($workers) eq 'ARRAY';
84  
85 10 ▶ for (my $i = 0; $i < @$workers; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$workers) 21 10 31
86 21 my $worker = $workers->[$i];
87  
88 21 my $type = ref($worker);
89 21 ▶ if ($type eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'CODE') { } 17 4 21
elsif ($type eq 'HASH') { } 3 1 4
90 17 $workers->[$i] = Broker::Async::Worker->new({code => $worker});
91 } elsif ($type eq 'HASH') {
92 3 $workers->[$i] = Broker::Async::Worker->new($worker);
93 }
94 }
95 }
96  
97 =head2 do
98  
99 my $future = $broker->do(@args);
100  
101 Queue the invocation of a worker with @args.
102 @args can be any data structure, and is passed as is to a worker code ref.
103 Returns a L<Future> object that resolves when the work is done.
104  
105 There is no guarantee when a worker will be called, that depends on when a worker becomes available.
106 However, calls are guaranteed to be invoked in the order they are seen by $broker->do.
107  
108 =cut
109  
110  
111 sub do {
112 118 ▶ my ($self, @args) = @_;
Subroutine do: called CC=5 SCAR=16.2
Pod documented
113  
114 # enforces consistent order of task execution
115 # makes sure current task is only started if nothing else is queued
116 118 $self->process_queue;
117  
118 118 my $future;
119 118 ▶ if (my @active_futures = map $_->active, $self->active) {
Branch: 2 branches
BranchTrueFalseTotal
if (my(@active_futures) = map($_->active, $self->active)) { } 111 7 118
elsif (my($available_worker) = $self->available) { } 7 0 7
120 # generate future from an existing future
121 # see Future::_new_convergent
122 111 my $_future = $active_futures[0];
123 111 ▶ ref($_) eq "Future" or $_future = $_, last for @active_futures;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "Future" or $_future = $_, last 0 963 963
124  
125 111 $future = $_future->new;
126 111 push @{ $self->queue }, {args => \@args, future => $future};
127 } elsif (my ($available_worker) = $self->available) {
128 # should only be here if there's nothing active and nothing queued
129 # so start the task and return it's future
130 7 $future = $self->do_worker($available_worker, @args);
131 }
132  
133 # start any recently queued tasks, if there are available workers
134 118 $self->process_queue;
135 118 return $future;
136 }
137  
138 sub do_worker {
139 118 ▶ weaken(my $self = shift);
Subroutine do_worker: called CC=1 SCAR=0.0
Pod undocumented
140 118 my ($worker, @args) = @_;
141  
142 return $worker->do(@args)->on_ready(sub{
143 # queue next task
144 114 ▶ $self->process_queue;
Subroutine __ANON__: called CC=1 SCAR=0.0
145 118 });
146 }
147  
148 sub process_queue {
149 350 ▶ weaken(my $self = shift);
Subroutine process_queue: called CC=3 SCAR=11.0
Pod undocumented
150 350 my $queue = $self->queue;
151  
152 350 ▶ while (@$queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$queue) 385 76 461
153 385 ▶ my ($worker) = $self->available or last;
Branch: 1 branch
BranchTrueFalseTotal
my($worker) = $self->available or last 274 111 385
154 111 my $task = shift @$queue;
155  
156 111 $self->do_worker($worker, @{$task->{args}})
157 111 ->on_ready($task->{future});
158 }
159 }
160  
161 =head1 AUTHOR
162  
163 Mark Flickinger E<lt>maf@cpan.orgE<gt>
164  
165 =head1 LICENSE
166  
167 This software is licensed under the same terms as Perl itself.
168  
169 =cut
170  
171 6 1;
↑ Summary Async/Worker.pm »