« 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
MC/DC0%0/1
$c->high_water_mark // 65536
$c->high_water_mark
Truth table50%1/2
A
A= $c->high_water_mark
| |||||||||||||
| 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
MC/DC0%0/1
$c->buffered_amount // 0
$c->buffered_amount
Truth table50%1/2
A
A= $c->buffered_amount
| |||||||||||||
| 25 | 10 | ▶ | if ($buffered < $hwm) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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; | |||||||||||