« examples/custom-middleware/lib/Example/Middleware/RequestTimer.pm
↑ Summary
examples/sse-custom-events/app.pl »
| 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 | # Full-duplex over a single HTTP request: read the streaming request body while | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # concurrently streaming the response. This is the HTTP-streaming analog of | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # examples/bidirectional-websocket — two branches raced with wait_any, one | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # echoing client input, one pushing server ticks unsolicited. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # Caveats (this is why WebSocket exists): no message framing (it's a byte stream | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # both ways), browsers can't do full-duplex on a single fetch, and HTTP/1.1 | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # proxies may buffer the request before forwarding. It is a fine fit for | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # non-browser / service-to-service / HTTP-2 clients. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # pagi-server app.pl | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # # Plain POST sends NO request body, so there is nothing to echo; the | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # # connection drives the stream, so ticks keep coming until you stop reading: | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # curl -N -XPOST --max-time 4 http://127.0.0.1:5000/duplex | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # # To actually send a body and see echoes, stream stdin as the body with | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # # `-T -` (so you get echo: lines interleaved with ticks): | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # ( printf 'hello\n'; sleep 1; printf 'world\n'; sleep 2 ) \ | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # | curl -N -T - -XPOST -H 'Transfer-Encoding: chunked' http://127.0.0.1:5000/duplex | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # # Note: typing into `curl -T -` interactively lags by one line — curl holds | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # # the line you just typed in its upload buffer until its next read. That is | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # # a curl artifact, not the server: the server echoes each chunk within ~40ms | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # # of receiving it. For a real-time wire proof use the raw-socket probe, which | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # # flushes each chunk immediately: | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # perl probe.pl 5000 | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | my $app = app { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | post '/duplex' => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | my $in = $c->req->body_stream; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | $c->response->stream(async sub ($w) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | # Echo request-body chunks as they arrive, concurrently. This branch | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # ends when the client finishes its body — but that must NOT end the | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # stream: the client can stop sending and keep receiving. So it runs | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # alongside the ticker, it does not drive termination. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | my $echoer = (async sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | while (defined(my $chunk = await $in->next_chunk)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | next unless length $chunk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | await $w->write("echo: $chunk\n"); | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | })->(); | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | # The stream lives as long as the client stays connected, pushing a | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # tick a second. A ?ticks=N cap bounds the run for demos and for | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # in-process tests (PAGI::Test::Client models a client that stays | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # connected and cannot disconnect mid-response, so an unbounded | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # connection-driven stream would never complete there). Without the | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # cap, the connection drives termination — see probe.pl against | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # pagi-server for the real full-duplex run. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | my $max = $c->req->query_param('ticks'); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | my $n = 0; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | while (!$c->is_disconnected && (!defined $max || $n < $max)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not $c->is_disconnected and !defined($max) || $n < $max
not defined $max or $n < $max
MC/DC33%1/3
not $c->is_disconnected and !defined($max) || $n < $max
not $c->is_disconnected
not defined $max
$n < $max
Truth table50%2/4
not(A) and not(B) or C
A= not $c->is_disconnectedB= not defined $maxC= $n < $max
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | await $w->write('tick ' . (++$n) . "\n"); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | await Future::IO->sleep(1); | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | $echoer->cancel unless $echoer->is_ready; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | await $w->close; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | $app; | |||||||||||||||||||||||||||||||||||||||||