« examples/duplex-http-stream/app.pl
↑ Summary
examples/custom-middleware/lib/Example/Middleware/ApiKey.pm »
| 1 | 1 | ▶ | use v5.40; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Future; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Future::IO; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use PAGI::Nano; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | # Custom SSE events, delivered through $receive the composable way (the SSE | ||||||||||||||||||||||||||||||||
| 9 | # evolution of PAGI's 17-event-middleware). One event stream carries TWO custom | ||||||||||||||||||||||||||||||||
| 10 | # event types from two sources, folded onto the same channel by a middleware: | ||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||
| 12 | # tick -> periodic, from a timer source | ||||||||||||||||||||||||||||||||
| 13 | # message -> user-driven: any client POSTs to /say, broadcast to ALL /events | ||||||||||||||||||||||||||||||||
| 14 | # subscribers | ||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||
| 16 | # A middleware owns the fold: for the SSE stream it races the next protocol event | ||||||||||||||||||||||||||||||||
| 17 | # against the next bus event WITHOUT cancelling the long-lived $receive (cancelling | ||||||||||||||||||||||||||||||||
| 18 | # it would end the connection). The handler is pure — it awaits the next event and | ||||||||||||||||||||||||||||||||
| 19 | # switches on type — and SSE's native `event:` field surfaces the custom type. | ||||||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||||||
| 21 | # pagi-server app.pl | ||||||||||||||||||||||||||||||||
| 22 | # # one terminal — watch the stream (the Accept header is what promotes the | ||||||||||||||||||||||||||||||||
| 23 | # # request to SSE; a browser's EventSource sends it automatically): | ||||||||||||||||||||||||||||||||
| 24 | # curl -N -H 'Accept: text/event-stream' http://127.0.0.1:5000/events | ||||||||||||||||||||||||||||||||
| 25 | # # another terminal — anyone can broadcast a message to every subscriber: | ||||||||||||||||||||||||||||||||
| 26 | # curl -XPOST http://127.0.0.1:5000/say -d 'hello everyone' | ||||||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||||||
| 28 | # See probe.pl for a self-contained proof that a POSTed message reaches an open | ||||||||||||||||||||||||||||||||
| 29 | # SSE stream. | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | # A tiny pub/sub bus carrying typed events. A subscriber gets a Future that | ||||||||||||||||||||||||||||||||
| 32 | # resolves on the next published event; publish drains every current waiter. | ||||||||||||||||||||||||||||||||
| 33 | 1 | package EventBus { | |||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub new ($class) { bless { waiters => [] }, $class } | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub next_event ($self) { push @{ $self->{waiters} }, my $f = Future->new; $f } | ||||||||||||||||||||||||||||||
|
Subroutine
next_event: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | sub publish ($self, $event) { | ||||||||||||||||||||||||||||||
|
Subroutine
publish: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 38 | # done() on a waiter cancelled by a lost race is a harmless no-op. Note | ||||||||||||||||||||||||||||||||
| 39 | # this is a simple in-memory bus: a subscriber momentarily between events | ||||||||||||||||||||||||||||||||
| 40 | # (not currently parked on next_event) can miss a publish. A production | ||||||||||||||||||||||||||||||||
| 41 | # bus would queue per subscriber. | ||||||||||||||||||||||||||||||||
| 42 | 1 | $_->done($event) for splice @{ $self->{waiters} }; | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | # Resolve as soon as ANY of the given futures is ready, cancelling none of them. | ||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | async sub await_either (@futures) { | ||||||||||||||||||||||||||||||
|
Subroutine
await_either: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 48 | 1 | my $first = Future->new; | |||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | $_->on_ready(sub { $first->done unless $first->is_ready }) for @futures; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | 1 | await $first; | |||||||||||||||||||||||||||||||
| 51 | 1 | return; | |||||||||||||||||||||||||||||||
| 52 | 1 | } | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | # The middleware: for the SSE stream, fold the bus's events into $receive so they | ||||||||||||||||||||||||||||||||
| 55 | # arrive alongside the protocol events. Gated to the SSE scope — HTTP requests | ||||||||||||||||||||||||||||||||
| 56 | # (including POST /say) get a clean, unwrapped $receive so body reading is intact. | ||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | my $with_events = async sub ($scope, $receive, $send, $next) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||
| 58 | 3 | my $bus = $scope->{state}{bus}; | |||||||||||||||||||||||||||||||
| 59 | return await $next->() | ||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | unless $bus && ($scope->{type} // '') eq 'sse'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$bus and ($scope->{"type"} // "") eq "sse"
$scope->{"type"} // ""
MC/DC50%1/2
$bus and ($scope->{"type"} // "") eq "sse"
$bus
$scope->{"type"}
Truth table66%2/3
A and B
A= $busB= $scope->{"type"}
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 1 | my $protocol_f; | |||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | my $wrapped_receive = async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | $protocol_f //= $receive->(); # one outstanding receive, kept alive | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$protocol_f //= &$receive()
MC/DC0%0/2
$protocol_f //= &$receive()
$protocol_f
&$receive()
Truth table33%1/3
A //= B
A= $protocol_fB= &$receive()
| |||||||||||||||||||||||||||||||||
| 65 | 1 | my $event_f = $bus->next_event; | |||||||||||||||||||||||||||||||
| 66 | 1 | await await_either($protocol_f, $event_f); | |||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if ($protocol_f->is_ready) { # a protocol event arrived | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | 1 | my $event = $protocol_f->get; | |||||||||||||||||||||||||||||||
| 69 | 1 | undef $protocol_f; # consumed -> fetch a fresh one next time | |||||||||||||||||||||||||||||||
| 70 | 1 | return $event; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | 0 | return $event_f->get; # a bus event (already typed) | |||||||||||||||||||||||||||||||
| 73 | 1 | }; | |||||||||||||||||||||||||||||||
| 74 | 1 | await $next->($scope, $wrapped_receive, $send); | |||||||||||||||||||||||||||||||
| 75 | 1 | }; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | my $app = app { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||
| 78 | # The bus and its timer source live for the app's lifetime, shared by all | ||||||||||||||||||||||||||||||||
| 79 | # requests via state (one per worker). | ||||||||||||||||||||||||||||||||
| 80 | 1 | ▶ | startup async sub ($state) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 81 | 1 | my $bus = EventBus->new; | |||||||||||||||||||||||||||||||
| 82 | 1 | $state->{bus} = $bus; | |||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | $state->{ticker} = (async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||
| 84 | 1 | my $n = 0; | |||||||||||||||||||||||||||||||
| 85 | 1 | while (1) { | |||||||||||||||||||||||||||||||
| 86 | 1 | await Future::IO->sleep(1); | |||||||||||||||||||||||||||||||
| 87 | 0 | $bus->publish({ type => 'tick', count => ++$n }); | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | 1 | })->(); | |||||||||||||||||||||||||||||||
| 90 | 1 | }; | |||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | shutdown async sub ($state) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | $state->{ticker}->cancel if $state->{ticker}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 1 | }; | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 1 | enable $with_events; | |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | # The SSE stream is pure: it knows nothing about the bus. It awaits the next | ||||||||||||||||||||||||||||||||
| 98 | # event and switches on type — tick, message, and the disconnect all arrive | ||||||||||||||||||||||||||||||||
| 99 | # through $receive — emitting a named SSE event for each. | ||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | sse '/events' => async sub ($c) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.4
| |||||||||||||||||||||||||||||||||
| 101 | 1 | my $s = $c->sse; | |||||||||||||||||||||||||||||||
| 102 | 1 | await $s->start; # establish the stream immediately | |||||||||||||||||||||||||||||||
| 103 | 1 | while (1) { | |||||||||||||||||||||||||||||||
| 104 | 1 | my $event = await $c->receive->(); | |||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | my $type = $event->{type} // ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"type"} // ""
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
| |||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | if ($type eq 'tick') { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 107 | 0 | await $s->send_event(event => 'tick', data => $event->{count}); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | elsif ($type eq 'message') { | ||||||||||||||||||||||||||||||||
| 110 | 0 | await $s->send_event(event => 'message', data => $event->{text}); | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | elsif ($type eq 'sse.disconnect') { | ||||||||||||||||||||||||||||||||
| 113 | 1 | last; # client went away | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | # other protocol events are ignored | ||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | 1 | await $s->close; | |||||||||||||||||||||||||||||||
| 118 | 1 | }; | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | # The user-driven source: a POST publishes a message event to the bus, which | ||||||||||||||||||||||||||||||||
| 121 | # the middleware folds into every open SSE stream. | ||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | post '/say' => async sub ($c) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 123 | 1 | my $text = await $c->req->body; | |||||||||||||||||||||||||||||||
| 124 | 1 | $text =~ s/\s+\z//; # trim trailing newline | |||||||||||||||||||||||||||||||
| 125 | 1 | $c->state->{bus}->publish({ type => 'message', text => $text }); | |||||||||||||||||||||||||||||||
| 126 | 1 | $c->json({ ok => 1, broadcast => $text }, status => 202); | |||||||||||||||||||||||||||||||
| 127 | 1 | }; | |||||||||||||||||||||||||||||||
| 128 | 1 | }; | |||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 1 | $app; | |||||||||||||||||||||||||||||||