examples/periodic-events/app.pl

filter: Statementstmt 65.6% 42 / 64 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 73.6% 14 / 19 Podpod n/a totaltotal 67.4% 56 / 83 CC 3 SCAR 7.4
CC
3
Cov
66%
tick
6.0
__ANON__
6.0
next_tick
2.0
CRAP 2.1
« examples/custom-middleware/lib/Example/Middleware/ApiKey.pm ↑ Summary examples/rest-resource/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 # Ports PAGI's 14-periodic-events to PAGI::Nano.
9 # A background ticker is rooted in the lifespan (it lives for the app's lifetime)
10 # and publishes to an in-memory hub shared via $c->state. Request handlers read
11 # the current count, long-poll for the next tick, or stream ticks as NDJSON.
12 #
13 # pagi-server app.pl
14 # curl http://127.0.0.1:5000/ # immediate count
15 # curl -N http://127.0.0.1:5000/stream # NDJSON tick stream
16  
17 # A tiny pub/sub hub: a counter plus Futures waiting for the next tick.
18 1 package Hub {
19 1 ▶ use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ sub new ($class) { bless { count => 0, waiters => [] }, $class }
Subroutine new: called CC=1 SCAR=0.0
21 1 ▶ sub count ($self) { $self->{count} }
Subroutine count: called CC=1 SCAR=0.0
22 0 ▶ sub next_tick ($self) {
Subroutine next_tick: not called CC=1 SCAR=6.9
23 0 my $f = Future->new;
24 0 push @{ $self->{waiters} }, $f;
25 0 return $f;
26 }
27 0 ▶ sub tick ($self) {
Subroutine tick: not called CC=2 SCAR=17.9
28 0 $self->{count}++;
29 0 my @waiters = @{ $self->{waiters} };
30 0 $self->{waiters} = [];
31 0 $_->done($self->{count}) for @waiters;
32 }
33 }
34  
35 1 ▶my $app = app {
Subroutine __ANON__: called CC=1 SCAR=0.8
36 1 ▶ startup async sub ($state) {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 my $hub = Hub->new;
38 1 $state->{hub} = $hub;
39 # Recurring background ticker, retained for the app lifetime.
40 1 ▶ $state->{ticker} = (async sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
41 1 while (1) {
42 1 await Future::IO->sleep(1);
43 0 $hub->tick;
44 }
45 1 })->();
46 1 };
47  
48 # Cancel the ticker on shutdown: discarding a suspended async sub without
49 # cancelling it warns ("lost its returning future") and leaves its pending
50 # timer armed. Cancellation is the clean end for a background task.
51 1 ▶ shutdown async sub ($state) {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1 $state->{ticker}->cancel;
53 1 };
54  
55 1 ▶ get '/' => sub ($c) { { ticks => $c->state->{hub}->count } };
Subroutine __ANON__: called CC=1 SCAR=0.0
56  
57 0 ▶ get '/next' => async sub ($c) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 0 my $n = await $c->state->{hub}->next_tick; # long-poll
59 0 { tick => $n };
60 1 };
61  
62 0 ▶ get '/stream' => sub ($c) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 my $hub = $c->state->{hub};
64 0 ▶ $c->response->stream(async sub ($w) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
65 0 for my $i (1 .. 3) {
66 0 my $n = await $hub->next_tick;
67 0 await $w->write(qq({"tick":$n}\n)); # NDJSON
68 }
69 0 await $w->close;
70 0 });
71 1 };
72 1 };
73  
74 1 $app;
« examples/custom-middleware/lib/Example/Middleware/ApiKey.pm ↑ Summary examples/rest-resource/app.pl »