PAGI
view release on metacpan or search on metacpan
lib/PAGI/Server.pm view on Meta::CPAN
=head1 METHODS
=head2 listen
my $future = $server->listen;
Starts listening for connections. Returns a Future that completes when
the server is ready to accept connections.
=head2 shutdown
my $future = $server->shutdown;
Initiates graceful shutdown. Returns a Future that completes when
shutdown is complete.
=head2 port
my $port = $server->port;
Returns the bound port number. Useful when port => 0 is used.
=head2 socket_path
my $path = $server->socket_path;
Returns the Unix socket path of the first Unix socket listener,
or C<undef> if no Unix socket listeners are configured.
=head2 listeners
my $listeners = $server->listeners;
Returns an arrayref of all normalized listener specifications.
Each entry is a hashref with C<type> (C<'tcp'> or C<'unix'>)
and type-specific keys (C<host>/C<port> for TCP, C<path> for Unix).
=head2 is_running
my $bool = $server->is_running;
Returns true if the server is accepting connections.
=head2 connection_count
my $count = $server->connection_count;
Returns the current number of active connections.
=head2 effective_max_connections
my $max = $server->effective_max_connections;
Returns the effective maximum connections limit. If C<max_connections>
was set explicitly, returns that value. Otherwise returns the default
of 1000.
=head1 FILE RESPONSE STREAMING
PAGI::Server supports efficient file streaming via the C<file> and C<fh>
keys in C<http.response.body> events:
# Stream entire file
await $send->({
type => 'http.response.body',
file => '/path/to/file.mp4',
more => 0,
});
# Stream partial file (for Range requests)
await $send->({
type => 'http.response.body',
file => '/path/to/file.mp4',
offset => 1000,
length => 5000,
more => 0,
});
# Stream from filehandle
open my $fh, '<:raw', $file;
await $send->({
type => 'http.response.body',
fh => $fh,
length => $size,
more => 0,
});
close $fh;
The server streams files in 64KB chunks to avoid memory bloat. Small files
(under 64KB) are read synchronously for speed; larger files use async I/O
via a worker pool to avoid blocking the event loop.
=head2 Production Recommendations for Static Files
B<For production deployments, we strongly recommend delegating static file
serving to a reverse proxy:>
=over 4
=item 1. B<Use nginx, Apache, or a CDN>
Place a reverse proxy in front of PAGI::Server and let it handle static
files directly. This provides:
=over 4
=item * Optimized file serving with kernel sendfile
=item * Efficient caching and compression
=item * Protection from slow client attacks
=item * HTTP/2 and HTTP/3 support
=back
=item 2. B<Use L<PAGI::Middleware::XSendfile>>
For files that require authentication or authorization, use the XSendfile
middleware to delegate file serving to the reverse proxy:
( run in 0.729 second using v1.01-cache-2.11-cpan-140bd7fdf52 )