File Coverage

examples/07-extension-fullflush/app.pl
Criterion Covered Total %
statement 24 24 100.0
branch 6 8 75.0
condition 2 3 66.6
subroutine 4 4 100.0
pod n/a
total 36 39 92.3


line stmt bran cond sub pod time code
1 1     1   229856 use strict;
  1         3  
  1         40  
2 1     1   4 use warnings;
  1         1  
  1         40  
3 1     1   6 use Future::AsyncAwait;
  1         2  
  1         10  
4              
5             # Demonstrates fullflush extension during streaming response.
6             # The fullflush event forces immediate TCP buffer flush, useful for
7             # Server-Sent Events or real-time streaming where latency matters.
8              
9 2     2   3 async sub app {
10 2         5 my ($scope, $receive, $send) = @_;
11              
12 2 100       27 die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'http';
13              
14             # Drain request body if present
15 1         2 while (1) {
16 1         2 my $event = await $receive->();
17 1 50       17 last if $event->{type} ne 'http.request';
18 1 50       4 last unless $event->{more};
19             }
20              
21             # Check if server supports fullflush extension
22 1         3 my $supports_fullflush = exists $scope->{extensions}{fullflush};
23              
24 1         7 await $send->({
25             type => 'http.response.start',
26             status => 200,
27             headers => [ [ 'content-type', 'text/plain' ] ],
28             });
29              
30             # Stream chunks with flush after each (if supported)
31 1         34 my @chunks = ("Line 1\n", "Line 2\n", "Line 3\n");
32              
33 1         4 for my $i (0 .. $#chunks) {
34 3         52 my $is_last = ($i == $#chunks);
35 3         13 await $send->({
36             type => 'http.response.body',
37             body => $chunks[$i],
38             more => $is_last ? 0 : 1,
39             });
40              
41             # Flush immediately after each chunk so client sees it right away
42             # Only send if server advertises support and not the final chunk
43 3 100 66     88 if ($supports_fullflush && !$is_last) {
44 2         5 await $send->({ type => 'http.fullflush' });
45             }
46             }
47             }
48              
49             \&app; # Return coderef when loaded via do