blib/lib/Ekorn/Pipeline/Futures.pm

filter: Statementstmt 100% 126 / 126 Branchbran 100% 42 / 42 Conditioncond 100% 23 / 23 MC/DCmcdc 100% 15 / 15 Subroutinesub 100% 28 / 28 Podpod 100% 2 / 2 totaltotal 100% 236 / 236 CC 32 SCAR 12.5
CC
32
Cov
100%
__ANON__
8.0
_in_futures
5.0
__ANON__
5.0
CRAP 3.5
↑ Summary
1 5 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline::Futures;
4  
5 5 ▶use experimental qw(try);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Stages running a block for each value that returns
8 # something to await, keeping several in flight in one process, while
9 # the stage waits on them. For I/O, such as HTTP requests, through a
10 # library giving Futures or promises: Net::Async::HTTP, anything on
11 # IO::Async, or Mojo::UserAgent.
12 #
13 # - in_futures($n, $block): results in the order the values came;
14 # - in_futures_as_ready($n, $block): results as they're ready.
15 #
16 # The stage knows no event loop, and no one class. It takes whatever
17 # Future::AsyncAwait can await: a Future, such as IO::Async::Future, or
18 # a Mojo::Promise, through the methods await uses (AWAIT_IS_READY,
19 # AWAIT_ON_READY, AWAIT_GET, AWAIT_WAIT and AWAIT_CLONE), and waits by
20 # AWAIT_WAIT, which runs the loop it's tied to. The run itself stays
21 # synchronous.
22  
23 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Future ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Ekorn::Pipeline::Context qw(kept_origin);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use Ekorn::Pipeline::Errors ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use Ekorn::Pipeline::Gather qw(gather);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use Ekorn::Pipeline::Stop ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 5 our $VERSION = 'v0.1.0';
33  
34 # Its whole purpose is its two stages, so both are exported by default,
35 # as Stop and Gather are (Ekorn-Pipeline's 2026-0064, D12).
36 5 our @EXPORT = qw(in_futures in_futures_as_ready); ## no critic (Modules::ProhibitAutomaticExportation)
37  
38 # Croak at the line that called Ekorn::Pipeline.
39 5 our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL;
40  
41 5 my @AWAIT = map { "AWAIT_$_" } qw(IS_READY ON_READY GET WAIT CLONE DONE);
42  
43 # Where a failure that's a string, without a line ending, says it was
44 # raised, when Future or Mojo::Promise croaks it from AWAIT_GET, as
45 # Mojo's "Connection refused" is: this file, which tells whoever reads
46 # it nothing, so it's taken off, leaving the failure as it was given.
47 # The file is matched by its name's end, as its path may be relative,
48 # absolute, under blib, or with Windows' backslashes.
49 5 my $FILE = qr( Ekorn [/\\] Pipeline [/\\] Futures\.pm )x;
50 5 my $HERE = qr( \ at\ [^\n]*? $FILE \ line\ \d+ \.? \n \z )x;
51  
52  
53 # Whether it's something to await, as Future::AsyncAwait would.
54 540 ▶sub _awaitable ($thing) {
Subroutine _awaitable: called CC=2 SCAR=6.9
55 540 ▶ return blessed $thing && !grep { !$thing->can($_) } @AWAIT;
Condition100%3/3
builtin::blessed($thing) && !grep({not $thing->can($_);} @AWAIT)
!ll&&!rl&&r
101529
MC/DC100%2/2
builtin::blessed($thing) && !grep({not $thing->can($_);} @AWAIT)
builtin::blessed($thing) !grep({not $thing->can($_);} @AWAIT)
Truth table100%3/3
A && not(B)
A= builtin::blessed($thing)B= !grep({not $thing->can($_);} @AWAIT)
ABresult
0X0
100
111
56 }
57  
58  
59 # As in_futures, passing each value's results on as soon as they're
60 # ready, so one slow value holds up none of the rest.
61 11 ▶sub in_futures_as_ready ($n, $block, %options) {
Subroutine in_futures_as_ready: called CC=1 SCAR=0.0
Pod documented
62 11 return _in_futures($n, $block, 0, %options);
63 }
64  
65  
66 62 ▶sub in_futures ($n, $block, %options) {
Subroutine in_futures: called CC=1 SCAR=0.0
Pod documented
67 62 return _in_futures($n, $block, 1, %options);
68 }
69  
70  
71 # Both: ordered, or as ready, which is the stage's to say, not an
72 # option; and they take none, so one given croaks, rather than being
73 # ignored (2026-0064's D17).
74 73 ▶sub _in_futures ($n, $block, $ordered, %options) {
Subroutine _in_futures: called CC=5 SCAR=16.1
75 73 ▶ croak 'Expected a number in flight of 1 or more'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ / \A [1-9] \d* \z /ux 3 70 73
Condition100%3/3
defined $n and $n =~ / \A [1-9] \d* \z /ux
!ll&&!rl&&r
1270
MC/DC100%2/2
defined $n and $n =~ / \A [1-9] \d* \z /ux
defined $n $n =~ / \A [1-9] \d* \z /ux
Truth table100%3/3
A and B
A= defined $nB= $n =~ / \A [1-9] \d* \z /ux
ABresult
0X0
100
111
76 unless defined $n && $n =~ m( \A [1-9] \d* \z )x;
77 70 ▶ croak 'Expected a code reference' unless ref $block eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block eq "CODE" 1 69 70
78 69 my($option) = sort keys %options;
79 69 ▶ croak "Expected no option $option" if defined $option;
Branch: 1 branch
BranchTrueFalseTotal
if defined $option 4 65 69
80  
81 71 ▶ return gather {
Subroutine __ANON__: called CC=2 SCAR=6.9
82 71 my @pending; # jobs not yet passed on, in the order values came
83 my @ready; # ready jobs, in the order they became ready
84  
85 # Call the block in the stage, as a stage would, so it sees the
86 # value's context, until its first await. What it returns is
87 # awaited, or else is its results; what it throws, a failed
88 # Future, so an error or a stop takes its turn among the results.
89 550 ▶ my $start = sub ($value) {
Subroutine __ANON__: called CC=4 SCAR=13.9
90 550 my $job = {origin => kept_origin};
91 550 my $awaited;
92 550 try {
93 550 local $_ = $value;
94 550 my @returned = $block->($value);
95 546 ▶ $awaited
Branch: 1 branch
BranchTrueFalseTotal
@returned == 1 && _awaitable($returned[0]) ? : 529 17 546
Condition100%3/3
@returned == 1 and _awaitable($returned[0])
!ll&&!rl&&r
611529
MC/DC100%2/2
@returned == 1 and _awaitable($returned[0])
@returned == 1 _awaitable($returned[0])
Truth table100%3/3
A and B
A= @returned == 1B= _awaitable($returned[0])
ABresult
0X0
100
111
96 = @returned == 1 && _awaitable($returned[0])
97 ? $returned[0]
98 : Future->done(@returned);
99 }
100 catch ($e) {
101 4 $awaited = Future->fail($e);
102 }
103 550 $job->{awaited} = $awaited;
104 550 push @pending, $job;
105 # AWAIT_ON_READY is for what's still pending, as await only
106 # asks it of that: Future's never calls back for one already
107 # ready, so one that is goes straight to @ready.
108 550 ▶ if ($awaited->AWAIT_IS_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($awaited->AWAIT_IS_READY) { } 116 434 550
109 116 push @ready, $job;
110 }
111 else {
112 434 ▶ $awaited->AWAIT_ON_READY(sub (@) { push @ready, $job });
Subroutine __ANON__: called CC=1 SCAR=0.0
113 }
114 550 return;
115 71 };
116  
117 71 ▶ my $is_ready = sub ($job) { $job->{awaited}->AWAIT_IS_READY };
Subroutine __ANON__: called CC=1 SCAR=0.0
118  
119 # The next ready job whose results may go on: ordered, the
120 # oldest, once it's ready; otherwise, the first to be ready.
121 my $next_ready
122 = $ordered
123 1257 ▶ ? sub () { @pending && $is_ready->($pending[0]) ? $pending[0] : undef }
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@pending && &$is_ready($pending[0]) ? : 521 736 1257
Condition100%3/3
@pending and &$is_ready($pending[0])
!ll&&!rl&&r
121615521
MC/DC100%2/2
@pending and &$is_ready($pending[0])
@pending &$is_ready($pending[0])
Truth table100%3/3
A and B
A= @pendingB= &$is_ready($pending[0])
ABresult
0X0
100
111
124 71 ▶ : sub () { $ready[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$ordered ? : 61 10 71
125  
126 # Wait on what's given, by its own loop. Mojo's loop, already
127 # running, as in a callback of it, can't be run again from
128 # inside: a Future tied to it croaks, and a Mojo::Promise returns
129 # at once, warning, still waiting, so the run would spin. So a
130 # Mojo::Promise's loop is asked first, and what's still waiting
131 # after any other croaks, naming why. IO::Async's runs again.
132 # Two messages (D2, as amended): one for a loop known to be
133 # running, and one for what's still waiting after its wait, for
134 # which the stage can't tell why.
135 71 my $running = q(Can't wait for a result: its event loop is already running, )
136 . 'and a pipeline runs synchronously inside it';
137 71 my $waiting = q(Can't wait for a result: it was still waiting after its wait, )
138 . 'as no event loop ran for it, or its loop was already running';
139 184 ▶ my $wait_on = sub ($awaited) {
Subroutine __ANON__: called CC=5 SCAR=16.1
140 184 ▶ croak $running if $awaited->can('ioloop') && $awaited->ioloop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
if $awaited->can("ioloop") and $awaited->ioloop->is_running 1 183 184
Condition100%3/3
$awaited->can("ioloop") and $awaited->ioloop->is_running
!ll&&!rl&&r
134491
MC/DC100%2/2
$awaited->can("ioloop") and $awaited->ioloop->is_running
$awaited->can("ioloop") $awaited->ioloop->is_running
Truth table100%3/3
A and B
A= $awaited->can("ioloop")B= $awaited->ioloop->is_running
ABresult
0X0
100
111
141 183 try { $awaited->AWAIT_WAIT }
142 catch ($e) {
143 # Its own failure, taken in its turn, unless it's still waiting.
144 5 ▶ croak $waiting unless $awaited->AWAIT_IS_READY;
Branch: 1 branch
BranchTrueFalseTotal
unless $awaited->AWAIT_IS_READY 3 2 5
145 }
146 180 ▶ croak $waiting unless $awaited->AWAIT_IS_READY;
Branch: 1 branch
BranchTrueFalseTotal
unless $awaited->AWAIT_IS_READY 1 179 180
147 179 return;
148 71 };
149  
150 # Wait for a job that may go on: the oldest, ordered; otherwise,
151 # any, by a new one of the oldest's kind, made ready by the first
152 # of them to be. It's called only with jobs pending.
153 186 ▶ my $wait = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
154 186 ▶ return if $next_ready->();
Branch: 1 branch
BranchTrueFalseTotal
if &$next_ready() 2 184 186
155 184 ▶ return $wait_on->($pending[0]{awaited}) if $ordered;
Branch: 1 branch
BranchTrueFalseTotal
if $ordered 153 31 184
156 31 my $any = $pending[0]{awaited}->AWAIT_CLONE;
157 31 for my $job (grep { !$is_ready->($_) } @pending) {
158 $job->{awaited}
159 144 ▶ ->AWAIT_ON_READY(sub (@) { $any->AWAIT_DONE unless $any->AWAIT_IS_READY });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $any->AWAIT_IS_READY 31 113 144
160 }
161 31 return $wait_on->($any);
162 71 };
163  
164 # Futures are cancelled; a Mojo::Promise can't be, and is left
165 # to settle, unheeded, as its loop next runs.
166 85 ▶ my $cancel = sub () {
Subroutine __ANON__: called CC=3 SCAR=11.0
167 85 for my $job (grep { !$is_ready->($_) } @pending) {
168 35 ▶ $job->{awaited}->cancel if $job->{awaited}->can('cancel');
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"awaited"}->can("cancel") 23 12 35
169 }
170 85 @pending = @ready = ();
171 85 return;
172 71 };
173  
174 # Take the ready jobs that may go on, adding their results to
175 # @$results, which a flush's takes share; a stop carries all of
176 # them (2026-0064's D15), and one that isn't an array carries
177 # none, as in Ekorn (D16). A failure stops the taking; it's
178 # thrown once nothing this take took is left to pass on. In a
179 # flush, what earlier takes took is lost with it, as a flush
180 # can't pass values on and then die (Ekorn-Pipeline's 2026-0061).
181 640 ▶ my $take = sub ($results) {
Subroutine __ANON__: called CC=8 SCAR=20.8
182 640 my $before = @$results;
183 640 ▶ while (my $job = $next_ready->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $job = &$next_ready()) 510 622 1132
184 510 my($failed, $e, @got);
185 510 try { @got = $job->{awaited}->AWAIT_GET }
186 18 catch ($caught) { ($failed, $e) = (1, $caught) }
187 510 ▶ return if $failed && @$results > $before;
Branch: 1 branch
BranchTrueFalseTotal
if $failed and @$results > $before 4 506 510
Condition100%3/3
$failed and @$results > $before
!ll&&!rl&&r
492144
MC/DC100%2/2
$failed and @$results > $before
$failed @$results > $before
Truth table100%3/3
A and B
A= $failedB= @$results > $before
ABresult
0X0
100
111
188 506 @pending = grep { $_ != $job } @pending;
189 506 @ready = grep { $_ != $job } @ready;
190 506 ▶ if ($failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 14 492 506
191 14 $cancel->();
192 14 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 4 10 14
193 4 ▶ my $carried = eval { [@$e[1 .. $#$e]] } // [];
Condition100%2/2
eval { do { [@$e[1 .. $e->$#*]] } } // []
l!l
31
MC/DC100%1/1
eval { do { [@$e[1 .. $e->$#*]] } } // []
eval { do { [@$e[1 .. $e->$#*]] } }
Truth table100%2/2
A
A= eval { do { [@$e[1 .. $e->$#*]] } }
Aresult
11
00
194 Ekorn::Pipeline::Stop->throw(
195 @$results,
196 4 $job->{origin}->gives(@$carried),
197 );
198 }
199 10 ▶ die ref $e ? $e : $e =~ s( $HERE )(\n)xr; ## no critic (ErrorHandling::RequireCarping): the block's error as it was
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 1 9 10
200 }
201 492 push @$results, $job->{origin}->gives(@got);
202 }
203 622 return;
204 71 };
205  
206 550 ▶ my $step = sub ($value) {
Subroutine __ANON__: called CC=3 SCAR=11.0
207 550 $start->($value);
208 # Wait while as many are in flight as may be, and none may go on.
209 550 ▶ $wait->() while @pending >= $n && !$next_ready->();
Branch: 1 branch
BranchTrueFalseTotal
@pending >= $n and not &$next_ready() and do { &$wait() } 91 546 637
Condition100%3/3
@pending >= $n and not &$next_ready()
!ll&&!rl&&r
43511191
MC/DC100%2/2
@pending >= $n and not &$next_ready()
@pending >= $n not &$next_ready()
Truth table100%3/3
A and not(B)
A= @pending >= $nB= not &$next_ready()
ABresult
0X0
100
111
210 546 $take->(\my @results);
211 534 return @results;
212 71 };
213  
214 50 ▶ my $flush = sub () {
Subroutine __ANON__: called CC=2 SCAR=6.9
215 50 my @results;
216 50 ▶ while (@pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending) 95 47 142
217 95 $wait->();
218 94 $take->(\@results);
219 }
220 47 return @results;
221 71 };
222  
223 71 ▶ my $end = sub ($how) { $cancel->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
224  
225 71 return {step => $step, flush => $flush, end => $end, holds => 1};
226 65 };
227 }
228  
229 5 1;
230  
231 __END__
↑ Summary