examples/flow-control/app.pl

filter: Statementstmt 100% 26 / 26 Branchbran 50.0% 1 / 2 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 87.5% 35 / 40 CC 5 SCAR 11.2
CC
5
Cov
91%
__ANON__
5.1
__ANON__
1.0
BEGIN
1.0
CRAP 3.1
« examples/custom-send-events/app.pl ↑ Summary examples/custom-middleware/lib/Example/Middleware/RequestTimer.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::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use PAGI::Nano;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Ports PAGI's 13-flow-control to PAGI::Nano.
7 # A high-frequency SSE feed that *conflates* under backpressure: when the client
8 # falls behind (the transport's buffered bytes exceed the high-water mark) the
9 # server skips stale readings and sends only the freshest, so a slow client never
10 # accumulates a backlog. $c->buffered_amount / $c->high_water_mark expose the
11 # transport's write buffer.
12 #
13 # pagi-server app.pl
14 # curl -N http://127.0.0.1:5000/feed
15  
16 1 ▶my $app = app {
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 ▶ sse '/feed' => async sub ($c) {
Subroutine __ANON__: called CC=5 SCAR=16.3
18 1 my $s = $c->sse;
19 1 ▶ my $hwm = $c->high_water_mark // 65536;
Condition50%1/2
$c->high_water_mark // 65536
l!l
01
MC/DC0%0/1
$c->high_water_mark // 65536
$c->high_water_mark
Truth table50%1/2
A
A= $c->high_water_mark
Aresult
11
00
20  
21 1 my $latest;
22 1 for my $i (1 .. 10) {
23 10 $latest = "reading $i";
24 10 ▶ my $buffered = $c->buffered_amount // 0;
Condition50%1/2
$c->buffered_amount // 0
l!l
100
MC/DC0%0/1
$c->buffered_amount // 0
$c->buffered_amount
Truth table50%1/2
A
A= $c->buffered_amount
Aresult
11
00
25 10 ▶ if ($buffered < $hwm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffered < $hwm) 10 0 10
26 10 await $s->send($latest); # client keeping up: send it
27 }
28 # else: client behind -> drop this reading (conflate to freshest)
29 }
30 1 await $s->send("final: $latest"); # always deliver the freshest
31 1 await $s->close;
32 1 };
33 1 };
34  
35 1 $app;
« examples/custom-send-events/app.pl ↑ Summary examples/custom-middleware/lib/Example/Middleware/RequestTimer.pm »