| 1 | package Example::Middleware::RequestTimer; | ||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use v5.40; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | # Event-layer middleware (subclass PAGI::Middleware, implement wrap). It times | ||||||||||||||||||||||||||||||||
| 9 | # the request and injects an X-Response-Time-Ms header by wrapping $send: when | ||||||||||||||||||||||||||||||||
| 10 | # the response start event passes through, it appends the header. Wrapping the | ||||||||||||||||||||||||||||||||
| 11 | # channel like this is how a middleware shapes the response without the handler | ||||||||||||||||||||||||||||||||
| 12 | # knowing or caring. | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | sub wrap ($self, $app) { | ||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | return async sub ($scope, $receive, $send) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 3 | my $t0 = Time::HiRes::time(); | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | my $timing_send = async sub ($event) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | if (ref $event eq 'HASH' && ($event->{type} // '') eq 'http.response.start') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
ref $event eq "HASH" and ($event->{"type"} // "") eq "http.response.start"
$event->{"type"} // ""
MC/DC0%0/2
ref $event eq "HASH" and ($event->{"type"} // "") eq "http.response.start"
ref $event eq "HASH"
$event->{"type"}
Truth table33%1/3
A and B
A= ref $event eq "HASH"B= $event->{"type"}
| |||||||||||||||||||||||||||||||||
| 20 | 3 | my $ms = sprintf('%.1f', (Time::HiRes::time() - $t0) * 1000); | |||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | my @headers = (@{ $event->{headers} // [] }, ['x-response-time-ms', $ms]); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"headers"} // []
MC/DC0%0/1
$event->{"headers"} // []
$event->{"headers"}
Truth table50%1/2
A
A= $event->{"headers"}
| |||||||||||||||||||||||||||||||||
| 22 | 3 | $event = { %$event, headers => \@headers }; | |||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | 6 | await $send->($event); | |||||||||||||||||||||||||||||||
| 25 | 3 | }; | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 3 | await $app->($scope, $receive, $timing_send); | |||||||||||||||||||||||||||||||
| 28 | 1 | }; | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 1 | 1; | |||||||||||||||||||||||||||||||