| 1 | use v5.40; | ||
| 2 | use feature 'class'; | ||
| 3 | no warnings qw[experimental::class recursion]; | ||
| 4 | use Time::HiRes qw[time]; | ||
| 5 | |||
| 6 | # A chainable FRP pipeline over bounded Channels: async stream stages. Every stage is a | ||
| 7 | # factory: it allocates its own bounded output Channel, spawns a background fiber that loops the input applying a | ||
| 8 | # callback, and returns a new Stream wrapping the output. Backpressure is free - a full bounded channel parks the | ||
| 9 | # stage's producer all the way upstream, which is exactly what t/057's parked_puts() counts. | ||
| 10 | # | ||
| 11 | # Teardown is the same everywhere: each stage's fiber ends its `while (my $x = $src->get)` loop when the upstream | ||
| 12 | # Channel shuts down, then shuts its own output down in turn, so a consume()'d chain unwinds fiber-by-fiber back to | ||
| 13 | # the source. `shutdown` releases the parked get()ers (Channel.pm's shutdown adjusts the get semaphore by 1e9), so no | ||
| 14 | # stage can park forever on a source that quit - the "zero leaked fibers" guarantee holds from the very | ||
| 15 | # first from_channel. | ||
| 16 | class Acme::Parataxis::Stream v0.1.6 { | ||
| 17 | use Carp qw[croak]; | ||
| 18 | use Acme::Parataxis qw[fiber await_sleep]; | ||
| 19 | use Acme::Parataxis::Channel; | ||
| 20 | field $src : param; # the Channel this stage reads from | ||
| 21 | field $chain : reader; # the upstream Stream this stage was built from (undef at the head) | ||
| 22 | field $cap : param : reader //= 128; # bounded output Channel size shared by every stage of the pipeline | ||
| 23 | |||
| 24 | method map ($cb) { | ||
| 25 | croak 'map() requires a CODE ref' unless ref $cb eq 'CODE'; | ||
| 26 | return $self->chain_factory( map => $cb ); | ||
| 27 | } | ||
| 28 | |||
| 29 | method filter ($cb) { | ||
| 30 | croak 'filter() requires a CODE ref' unless ref $cb eq 'CODE'; | ||
| 31 | return $self->chain_factory( filter => $cb ); | ||
| 32 | } | ||
| 33 | |||
| 34 | method batch ($n) { | ||
| 35 | $n >= 1 or croak "batch size must be >= 1 (got $n)\n"; | ||
| 36 | return $self->chain_factory( batch_count => $n ); | ||
| 37 | } | ||
| 38 | |||
| 39 | method batch_time ($ms) { | ||
| 40 | $ms >= 0 or croak "batch_time must be >= 0 (got $ms)\n"; | ||
| 41 | return $self->chain_factory( batch_deadline => $ms ); | ||
| 42 | } | ||
| 43 | |||
| 44 | method throttle ($per_s) { | ||
| 45 | $per_s >= 1 or croak "throttle rate must be >= 1/s (got $per_s)\n"; | ||
| 46 | return $self->chain_factory( throttle => $per_s ); | ||
| 47 | } | ||
| 48 | |||
| 49 | method chain_factory (%op) { | ||
| 50 | my $out = Acme::Parataxis::Channel->new( capacity => $self->cap ); # bounded: a slow consume parks upstream for free | ||
| 51 | my $op = ( keys %op )[0]; | ||
| 52 | my $val = $op{$op}; | ||
| 53 | my $fid = fiber { | ||
| 54 | if ( $op eq 'throttle' ) { | ||
| 55 | |||
| 56 | # At most $val emissions a second: schedule the next emission one gap ahead and sleep only the | ||
| 57 | # remainder. A stage held up by backpressure emits immediately (it never "catches up") and pushes the | ||
| 58 | # next slot forward from the actual emission, so the rate is an upper bound, reached from idle below. | ||
| 59 | my $gap_ms = 1000 / $val; | ||
| 60 | my $next = 0; | ||
| 61 | while ( defined( my $x = $src->get ) ) { | ||
| 62 | if ($next) { | ||
| 63 | my $remaining = $next - Acme::Parataxis::_now_ms(); | ||
| 64 | await_sleep($remaining) if $remaining > 0; | ||
| 65 | } | ||
| 66 | $out->put($x); | ||
| 67 | $next = Acme::Parataxis::_now_ms() + $gap_ms; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | elsif ( $op eq 'batch_count' ) { | ||
| 71 | my @g; | ||
| 72 | while ( defined( my $x = $src->get ) ) { | ||
| 73 | push @g, $x; | ||
| 74 | if ( @g >= $val ) { $out->put( [@g] ); @g = () } | ||
| 75 | } | ||
| 76 | $out->put( [@g] ) if @g; | ||
| 77 | } | ||
| 78 | elsif ( $op eq 'batch_deadline' ) { | ||
| 79 | |||
| 80 | # The first item of a batch arms an absolute deadline; the stage then waits with get-with-deadline so | ||
| 81 | # the deadline fires even with no further items. Enough items (or the deadline) flushes the batch and | ||
| 82 | # the next item re-arms. A source that quits mid-batch flushes what it has as the final partial batch. | ||
| 83 | my @g; | ||
| 84 | my $deadline = 0; # 0 = no batch open yet | ||
| 85 | while (1) { | ||
| 86 | my ( $ch, $item ); | ||
| 87 | if ($deadline) { | ||
| 88 | my $remaining = $deadline - Acme::Parataxis::_now_ms(); | ||
| 89 | if ( $remaining <= 0 ) { | ||
| 90 | $out->put( [@g] ); | ||
| 91 | @g = (); | ||
| 92 | $deadline = 0; | ||
| 93 | next; | ||
| 94 | } | ||
| 95 | ( $ch, $item ) = $src->select( [ $src, 'get' ], timeout => $remaining ); | ||
| 96 | if ( !defined $ch ) { # our own deadline fired | ||
| 97 | $out->put( [@g] ); | ||
| 98 | @g = (); | ||
| 99 | $deadline = 0; | ||
| 100 | next; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | ( $ch, $item ) = ( $src, $src->get ); | ||
| 105 | } | ||
| 106 | last unless defined $item; # source shutdown delivers undef; flush the partial below | ||
| 107 | push @g, $item; | ||
| 108 | $deadline = Acme::Parataxis::_now_ms() + $val unless $deadline; | ||
| 109 | } | ||
| 110 | $out->put( [@g] ) if @g; | ||
| 111 | } | ||
| 112 | else { # map | filter | ||
| 113 | while ( defined( my $x = $src->get ) ) { | ||
| 114 | if ( $op eq 'filter' ) { $out->put($x) if $val->($x) } | ||
| 115 | else { $out->put( $val->($x) ) } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | $out->shutdown; # upstream ended: release any downstream parked get()ers; the chain unwinds one fiber at a time | ||
| 119 | 1; | ||
| 120 | }; | ||
| 121 | my $chain = __CLASS__->new( src => $out, cap => $self->cap ); | ||
| 122 | $chain->_link($self); # chain heads back toward the raw source (introspection) | ||
| 123 | return $chain; | ||
| 124 | } | ||
| 125 | method _link ($up) { $chain = $up; 1 } | ||
| 126 | |||
| 127 | method consume ($cb) { | ||
| 128 | croak 'consume() requires a CODE ref' unless ref $cb eq 'CODE'; | ||
| 129 | my $ffid = fiber { | ||
| 130 | while ( defined( my $x = $src->get ) ) { | ||
| 131 | if ( ref $x && ref $x eq 'ARRAY' ) { $cb->(@$x) } | ||
| 132 | else { $cb->($x) } | ||
| 133 | } | ||
| 134 | 1; | ||
| 135 | }; | ||
| 136 | return $ffid; # the fiber handle so the caller can ->await it; the fiber ends when the source shuts down | ||
| 137 | } | ||
| 138 | method shutdown () { $src->shutdown; $self->chain->shutdown if $self->chain; 1 } | ||
| 139 | |||
| 140 | # from_channel() is the chain head constructor, called as Acme::Parataxis::Stream->from_channel($ch, ...), | ||
| 141 | # so like Channel->select it is a plain package sub: it builds a new stream rather than dispatching on one, | ||
| 142 | # and takes the class as $class. | ||
| 143 | sub from_channel ( $class, $ch, %opts ) { | ||
| 144 | my $cap = $opts{stage_capacity} // 128; | ||
| 145 | return $class->new( src => $ch, cap => $cap ); | ||
| 146 | } | ||
| 147 | }; | ||
| 148 | # | ||
| 149 | 1; |