| 1 | package PAGI::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 153 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 153 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 153 | our $VERSION = '0.002013'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # Future::XS support - opt-in via PAGI_FUTURE_XS=1 environment variable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Must be loaded before Future to take effect, so we check env var in BEGIN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # Note: We declare these without initialization so BEGIN block values persist | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 153 | our ($FUTURE_XS_AVAILABLE, $FUTURE_XS_ENABLED); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 153 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=5 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | $FUTURE_XS_AVAILABLE = eval { require Future::XS; 1 } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 153 | $FUTURE_XS_ENABLED = 0; # Default to disabled | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 153 | ▶ | if ($ENV{PAGI_FUTURE_XS}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | if ($FUTURE_XS_AVAILABLE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # Future::XS is already loaded from the availability check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | $FUTURE_XS_ENABLED = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | die <<"END_FUTURE_XS_ERROR"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | PAGI_FUTURE_XS=1 set but Future::XS is not installed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | To install Future::XS: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | cpanm Future::XS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | Or unset the PAGI_FUTURE_XS environment variable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | END_FUTURE_XS_ERROR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } elsif ($FUTURE_XS_AVAILABLE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # Available but not requested - unload it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | delete $INC{'Future/XS.pm'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 153 | ▶ | use parent 'IO::Async::Notifier'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 153 | ▶ | use IO::Async::Listener; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 153 | ▶ | use IO::Async::Stream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 153 | ▶ | use IO::Async::Loop; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 153 | ▶ | use IO::Async::Timer::Periodic; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 153 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 153 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 153 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 153 | ▶ | use Scalar::Util qw(weaken refaddr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 153 | ▶ | use Socket qw(sockaddr_family unpack_sockaddr_in unpack_sockaddr_un AF_UNIX AF_INET); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 153 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 153 | ▶ | use PAGI::Server::AppNormalizer (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 153 | ▶ | use PAGI::Server::Connection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 153 | ▶ | use PAGI::Server::EventValidator; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 153 | ▶ | use PAGI::Server::Protocol::HTTP1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # Check TLS module availability (cached at load time for banner display) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 153 | our $TLS_AVAILABLE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 153 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 153 | ▶ | $TLS_AVAILABLE = eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 153 | require IO::Async::SSL; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 153 | require IO::Socket::SSL; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 153 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | ▶ | sub has_tls { return $TLS_AVAILABLE } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_tls: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # Check HTTP/2 module availability (cached at load time) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 153 | our $HTTP2_AVAILABLE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 153 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 153 | ▶ | $HTTP2_AVAILABLE = eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 153 | require PAGI::Server::Protocol::HTTP2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 153 | PAGI::Server::Protocol::HTTP2->available; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 14 | ▶ | sub has_http2 { return $HTTP2_AVAILABLE } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_http2: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # Windows doesn't support Unix signals - signal handling is conditional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 153 | ▶ | use constant WIN32 => $^O eq 'MSWin32'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | PAGI::Server - PAGI Reference Server Implementation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | use IO::Async::Loop; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | use PAGI::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # If using Future::IO libraries (Async::Redis, SSE->every, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # load the IO::Async implementation BEFORE loading them: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | use Future::IO::Impl::IOAsync; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | app => \&my_pagi_app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | host => '127.0.0.1', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | port => 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | $loop->add($server); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | $server->listen->get; # Start accepting connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | See L</LOOP INTEROPERABILITY> for details on Future::IO configuration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | PAGI::Server is a reference implementation of a PAGI-compliant HTTP server. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | It supports HTTP/1.1, WebSocket, and Server-Sent Events (SSE) as defined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | in the PAGI specification. It prioritizes spec compliance and code clarity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | over performance optimization. It serves as the canonical reference for how | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | PAGI servers should behave. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | =head1 PROTOCOL SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | B<Currently supported:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =item * HTTP/1.1 (full support including chunked encoding, trailers, keepalive) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | =item * HTTP/2 (B<experimental> - via nghttp2, h2 over TLS and h2c cleartext) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | =item * WebSocket (RFC 6455, including over HTTP/2 via RFC 8441) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | =item * Server-Sent Events (SSE, including over HTTP/2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | B<Not yet implemented:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | =item * HTTP/3 (QUIC) - Under consideration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | For HTTP/2, see L</ENABLING HTTP/2 SUPPORT (EXPERIMENTAL)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | =head1 UNIX DOMAIN SOCKET SUPPORT (EXPERIMENTAL) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | B<This feature is experimental.> The API is subject to change in future | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | releases. Please report issues at L<https://github.com/jjn1056/pagi-server/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | Unix domain sockets provide efficient local communication between a reverse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | proxy (nginx, HAProxy, etc.) and PAGI::Server running on the same machine. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | They bypass the TCP/IP stack entirely, reducing latency and overhead compared | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | to connecting over C<127.0.0.1>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | =head2 When to Use Unix Sockets | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | =item * B<Behind a reverse proxy> — nginx or HAProxy on the same host handles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | TLS termination, HTTP/2 negotiation, and static files, forwarding dynamic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | requests to PAGI over a Unix socket. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | =item * B<Benchmarks> — frameworks like TechEmpower FrameworkBenchmarks use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | Unix sockets to eliminate network variable from application benchmarks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | =item * B<Microservice IPC> — services on the same host communicate without | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | network overhead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | B<When NOT to use:> If clients connect over the network (remote browsers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | API consumers), use TCP. Unix sockets only accept connections from processes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | on the same machine. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | =head2 Basic Usage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | B<Programmatic:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | socket => '/tmp/pagi.sock', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | B<CLI:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | pagi-server --socket /tmp/pagi.sock ./app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | B<With workers:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | pagi-server --socket /tmp/pagi.sock --workers 4 ./app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | In multi-worker mode, the parent process creates the Unix socket and all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | worker processes inherit the file descriptor via C<fork()>. The kernel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | distributes incoming connections across workers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | =head2 How It Works | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | =item 1. On startup, any existing file at the socket path is removed (stale | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | socket cleanup). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | =item 2. The server creates and binds a C<SOCK_STREAM> Unix domain socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | at the specified path using C<IO::Socket::UNIX> (multi-worker) or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | C<IO::Async::Listener> with C<< family => 'unix' >> (single-worker). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | =item 3. If C<socket_mode> is set, C<chmod()> is called immediately after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | binding to set the file permissions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | =item 4. In multi-worker mode, the parent creates the socket before forking. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | Workers inherit the listening fd and each runs its own C<IO::Async::Listener> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | wrapping the inherited handle. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | =item 5. On graceful shutdown (SIGTERM/SIGINT), the socket file is unlinked | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | by both the single-worker C<shutdown()> path and the multi-worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | C<_initiate_multiworker_shutdown()> path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | =head2 nginx Configuration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | B<Basic upstream:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | upstream pagi_backend { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | server unix:/var/run/myapp/pagi.sock; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | keepalive 32; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | server { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | listen 80; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | server_name myapp.example.com; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | location / { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | proxy_pass http://pagi_backend; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # Required for upstream keepalive: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | proxy_http_version 1.1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | proxy_set_header Connection ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | # Forward client info (since PAGI can't see it over Unix socket): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | proxy_set_header Host $host; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | proxy_set_header X-Real-IP $remote_addr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | proxy_set_header X-Forwarded-Proto $scheme; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | B<Important:> The C<keepalive> directive is critical for performance. Without | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | it, nginx opens a new Unix socket connection for every request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | C<proxy_http_version 1.1> and C<proxy_set_header Connection ""> are required | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | for keepalive to work. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | B<With TLS termination at nginx:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | server { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | listen 443 ssl http2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | ssl_certificate /etc/ssl/myapp.crt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | ssl_certificate_key /etc/ssl/myapp.key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | location / { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | proxy_pass http://pagi_backend; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # ... same proxy headers as above | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | nginx handles TLS and HTTP/2 with clients, then speaks plain HTTP/1.1 to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | PAGI over the Unix socket. This is the recommended production pattern. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | =head2 Socket Permissions and Security | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | By default, the socket file inherits permissions from the process umask. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | Use C<socket_mode> to set explicit permissions: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # CLI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | pagi-server --socket /var/run/myapp/pagi.sock --socket-mode 0660 ./app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # Programmatic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | socket => '/var/run/myapp/pagi.sock', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | socket_mode => 0660, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | B<Production recommendations:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | =item * B<Use a dedicated directory>, not C</tmp/>. Directories like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | C</var/run/myapp/> or C</run/myapp/> prevent symlink attacks and provide | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | an additional permission layer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | =item * B<Use C<0660> with a shared group.> Create a group (e.g., C<myapp>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | that both the application user and the nginx user belong to: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | sudo groupadd myapp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | sudo usermod -aG myapp www-data # nginx user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | sudo usermod -aG myapp myappuser # app user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | sudo mkdir -p /var/run/myapp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | sudo chown myappuser:myapp /var/run/myapp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | sudo chmod 0750 /var/run/myapp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | =item * B<Use systemd C<RuntimeDirectory>> for automatic directory management: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # /etc/systemd/system/myapp.service | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | [Service] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | User=myappuser | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | Group=myapp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | RuntimeDirectory=myapp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | RuntimeDirectoryMode=0750 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | ExecStart=/usr/local/bin/pagi-server \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | --socket /run/myapp/pagi.sock \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | --socket-mode 0660 \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | --workers 4 \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | /opt/myapp/app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | systemd creates C</run/myapp/> on service start and cleans it up on stop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | =head2 TLS Over Unix Sockets | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | TLS can be used over Unix sockets, though this is unusual — normally the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | reverse proxy handles TLS termination. When TLS is configured on a Unix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | socket listener, the server logs an info-level note suggesting reverse proxy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | TLS termination instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | The combination is allowed because it has legitimate uses (encrypted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | inter-container communication, compliance requirements). All major ASGI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | servers (Uvicorn, Hypercorn, Granian) also allow it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | =head2 HTTP/2 Over Unix Sockets | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | h2c (HTTP/2 cleartext) works over Unix sockets. This is useful for gRPC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | backends or reverse proxies that support HTTP/2 to upstreams (e.g., Envoy). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | Note that nginx does B<not> currently support HTTP/2 to upstream backends | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | (except for gRPC via C<grpc_pass>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | =head2 Scope Differences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | For Unix socket connections, the PAGI scope differs from TCP connections: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | =item * B<C<client> is absent> — Unix sockets have no peer IP address or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | port. The C<client> key is omitted entirely from the scope hashref (not set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | to C<undef>). This is spec-compliant: the PAGI specification marks C<client> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | as optional. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | =item * B<C<server> is C<[$socket_path, undef]>>> — instead of C<[$host, $port]>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | B<Middleware implications:> Any middleware that accesses C<< $scope->{client} >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | must check C<< exists $scope->{client} >> first. For client IP identification | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | behind a reverse proxy, use C<X-Forwarded-For> or C<X-Real-IP> headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | instead of C<< $scope->{client} >>. The C<PAGI::Middleware::XForwardedFor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | middleware (if available) handles this automatically. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | B<Access log:> Unix socket connections log C<unix> as the client IP in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | access log instead of an IP address. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | =head2 Stale Socket Cleanup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | If a socket file already exists at the configured path (e.g., from a previous | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | crash), it is automatically removed before binding. This matches the behavior | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | of Starman, Gunicorn, Uvicorn, and other production servers. The socket file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | is also removed during graceful shutdown (SIGTERM/SIGINT). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | If the server is killed with SIGKILL (C<kill -9>), the socket file will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | B<not> be cleaned up. It will be removed on the next startup. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | =head1 MULTI-LISTENER SUPPORT (EXPERIMENTAL) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | B<This feature is experimental.> The API is subject to change in future | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | releases. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | A single PAGI::Server instance can listen on multiple endpoints | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | simultaneously. This is useful for: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | =item * B<TCP for health checks + Unix socket for app traffic> — load | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | balancers probe a TCP port while nginx uses the Unix socket. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | =item * B<Multiple TCP ports> — serve different interfaces on different ports. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | =item * B<Gradual migration> — listen on both old and new ports during | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | a transition. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | =head2 Programmatic API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | listen => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | { host => '0.0.0.0', port => 8080 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | { socket => '/tmp/pagi.sock', socket_mode => 0660 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | Each spec in the C<listen> array is a hashref with either C<< { host, port } >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | for TCP or C<< { socket } >> (with optional C<socket_mode>) for Unix sockets. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | B<Note:> Per-listener TLS configuration is not yet supported. TLS is configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | server-wide via the C<ssl> constructor option and applies to all TCP listeners. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | Unix socket listeners behind a reverse proxy do not need TLS — the proxy handles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | TLS termination. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | =head2 CLI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | The C<--listen> flag is repeatable. The server auto-detects TCP vs Unix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | socket: values containing C<:> are parsed as C<host:port>, everything else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | is treated as a Unix socket path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | # TCP + Unix socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | pagi-server --listen 0.0.0.0:8080 --listen /tmp/pagi.sock ./app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | # Multiple TCP ports | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | pagi-server --listen 0.0.0.0:8080 --listen 0.0.0.0:8443 ./app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | # With workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | pagi-server --listen 0.0.0.0:8080 --listen /tmp/pagi.sock -w 4 ./app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | # IPv6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | pagi-server --listen [::1]:5000 ./app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | C<--listen> is B<mutually exclusive> with C<--host>, C<--port>, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | C<--socket>. C<--socket-mode> applies to all Unix socket listeners when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | using C<--listen>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | =head2 How It Works | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | =item * In B<single-worker mode>, one C<IO::Async::Listener> is created per | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | endpoint. All listeners share the same event loop and connection handler. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | =item * In B<multi-worker mode>, the parent process creates all listening | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | sockets (Unix and TCP) before forking. Workers inherit all file descriptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | and create their own C<IO::Async::Listener> for each inherited socket. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | =item * The C<reuseport> option applies only to TCP listeners. Unix socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | listeners always use the shared-socket model (parent creates, workers inherit). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | =item * On shutdown, all listeners are stopped and all Unix socket files are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | cleaned up. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | =head2 Accessors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | $server->port; # Bound port of first TCP listener, or undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | $server->socket_path; # Path of first Unix socket listener, or undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | $server->listeners; # Arrayref of all listener specs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | =head2 Backward Compatibility | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | The existing C<host>/C<port> constructor options continue to work exactly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | as before. They are internally normalized to a single-element listener | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | array. The C<socket> option is similarly sugar for a single Unix socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | listener. Only C<listen> enables true multi-listener mode. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | =head1 SYSTEMD SOCKET ACTIVATION (EXPERIMENTAL) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | B<This feature is experimental.> The API is subject to change in future | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | releases. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | PAGI::Server supports systemd socket activation, which allows systemd to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | create and hold listening sockets on behalf of the server. This enables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | zero-downtime restarts: when the server is restarted, the kernel continues | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | to queue incoming connections on the socket without refusing or dropping | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | them, even during the gap between the old process exiting and the new one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | starting. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | Benefits of systemd socket activation: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | =item * B<Zero-downtime restarts> — the kernel queues connections during restarts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | =item * B<Atomic permission handling> — systemd creates the socket as root, then drops privileges before exec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | =item * B<On-demand activation> — the server is started automatically when the first connection arrives | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | =head2 Basic Setup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | Create two systemd unit files: a C<.socket> unit that describes the socket, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | and a C<.service> unit for the server itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | B<TCP socket (C</etc/systemd/system/pagi.socket>):> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | [Unit] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | Description=PAGI Application Socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | [Socket] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | ListenStream=0.0.0.0:8080 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | Accept=no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | [Install] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | WantedBy=sockets.target | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | B<Unix socket (C</etc/systemd/system/pagi.socket>):> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | [Unit] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | Description=PAGI Application Socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | [Socket] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | ListenStream=/run/pagi/app.sock | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | SocketMode=0660 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | SocketUser=www-data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | SocketGroup=www-data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | Accept=no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | [Install] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | WantedBy=sockets.target | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | B<Service unit (C</etc/systemd/system/pagi.service>):> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | [Unit] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | Description=PAGI Application Server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | Requires=pagi.socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | [Service] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | User=www-data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | ExecStart=/usr/local/bin/pagi-server -E production ./app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | Restart=on-failure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | [Install] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | WantedBy=multi-user.target | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | C<Accept=no> is B<required>. PAGI::Server accepts connections itself via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | C<IO::Async>; systemd must not accept on its behalf. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | =head2 How Auto-Detection Works | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | When PAGI::Server starts, it checks the C<LISTEN_FDS> and C<LISTEN_PID> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | environment variables set by systemd. If C<LISTEN_PID> matches the current | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | process PID, the server inspects each inherited file descriptor (starting at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | fd 3) with C<getsockname()> to determine its address. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | Each inherited socket is then matched against the configured listeners. For | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | example, if you configure C<< port => 8080 >> and systemd has a socket bound | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | to C<0.0.0.0:8080>, PAGI::Server will use the inherited fd instead of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | creating a new socket. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | The same application code works identically with or without systemd: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | # Without systemd: PAGI::Server binds the socket itself | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | # With systemd: PAGI::Server inherits the socket from systemd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | host => '0.0.0.0', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | port => 8080, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | After reading the inherited fds, PAGI::Server removes C<LISTEN_FDS>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | C<LISTEN_PID>, and C<LISTEN_FDNAMES> from the environment (per the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | C<sd_listen_fds(3)> specification), so child processes do not re-inherit them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | =head2 Unix Socket Cleanup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | Normally, PAGI::Server unlinks its Unix socket file on shutdown. For | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | systemd-activated Unix sockets, the socket file is B<not> unlinked because | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | systemd owns the socket and will recreate it for the next activation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | =head1 FD REUSE INTERNALS (EXPERIMENTAL) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | B<This feature is experimental.> The API is subject to change in future | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | releases. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | PAGI::Server uses a C<PAGI_REUSE> environment variable to pass inherited | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | listening socket file descriptors to re-exec'd processes during hot restart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | (see L</HOT RESTART (EXPERIMENTAL)>). This mechanism also supports systemd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | socket activation (see L</SYSTEMD SOCKET ACTIVATION (EXPERIMENTAL)>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | =head2 PAGI_REUSE Format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | The variable is a comma-separated list of C<addr:port:fd> entries: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | # TCP listeners | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | PAGI_REUSE=127.0.0.1:8080:3,0.0.0.0:8443:4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | # Unix socket listeners | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | PAGI_REUSE=unix:/run/pagi/app.sock:5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | # Mixed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | PAGI_REUSE=0.0.0.0:8080:3,unix:/run/pagi/app.sock:4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | Each entry encodes the address the socket is bound to and the file descriptor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | number to use. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | =head2 Fd Matching | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | When starting, C<_collect_inherited_fds()> parses C<PAGI_REUSE> and/or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | C<LISTEN_FDS>, building a table of C<< address => fd >> pairs. During | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | C<listen()>, each configured listener looks up its own address in the table. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | If a match is found, the existing fd is used instead of calling C<bind()> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | C<listen()>. This allows the kernel's accept queue to be preserved across | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | restarts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | =head2 File Descriptor Inheritance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | To ensure that listening socket fds are inherited across C<exec()>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | PAGI::Server sets C<$^F = 1023> before creating sockets. Perl uses C<$^F> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | (the maximum system file descriptor, equivalent to C<POSIX_OPEN_MAX> in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | spirit) to decide which fds receive the C<FD_CLOEXEC> close-on-exec flag: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | fds with numbers greater than C<$^F> get C<FD_CLOEXEC> set automatically. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | By raising C<$^F> to 1023, listen socket fds remain open across C<exec()> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | without requiring explicit C<fcntl> calls. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | =head1 HOT RESTART (EXPERIMENTAL) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | B<This feature is experimental.> The API and signal behaviour are subject | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | to change in future releases. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | Deploying new code normally requires a server restart. During that restart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | there is a gap — however brief — where the listening socket is closed and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | incoming connections are dropped. Hot restart eliminates this gap by having | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | the old master fork and exec a brand-new master process that B<inherits> the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | already-open listening sockets. Both the old master and the new master serve | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | requests during the transition; clients never see a refused connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | =head2 How It Works | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | =item 1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | An admin sends C<SIGUSR2> to the running master: C<kill -USR2 E<lt>master_pidE<gt>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | =item 2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | The old master sets C<PAGI_REUSE> (encoding each listening socket fd) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | C<PAGI_MASTER_PID> (its own PID) in the environment, then calls C<fork()> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | followed immediately by C<exec()> of the original C<pagi-server> command | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | (reconstructed from C<PAGI_ARGV>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | =item 3. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | The new master starts, finds the inherited file descriptors via C<PAGI_REUSE>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | and reuses the existing listening sockets rather than calling C<bind()>/C<listen()> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | again. The kernel's accept queue is preserved — no connections are dropped. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | =item 4. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | The new master spawns its worker pool and waits for each worker to complete | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | the lifespan startup handshake (heartbeat). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | =item 5. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | Once all workers are healthy, the new master sends C<SIGTERM> to the old | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | master (read from C<PAGI_MASTER_PID>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | =item 6. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | The old master receives C<SIGTERM>, finishes in-flight requests within its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | shutdown timeout, and exits cleanly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | =head2 HUP vs USR2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | HUP — Rolling worker restart. Workers are replaced one by one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | Code loaded at master startup (middleware, startup modules) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | is NOT reloaded. Use for: config changes picked up per-worker. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | USR2 — Full master re-exec. Everything reloaded from disk including | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | the perl binary, all modules, middleware stack. Use for: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | code deploys, Perl upgrades, PAGI::Server upgrades. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | =head2 Deploy Workflow | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | # Deploy new code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | rsync -a ./lib/ /opt/myapp/lib/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | # Hot restart (zero downtime) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | kill -USR2 $(cat /var/run/myapp/pagi.pid) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | # Verify (optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | curl http://localhost:8080/health | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | =head2 systemd Unit File | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | [Service] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | Type=forking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | PIDFile=/var/run/myapp/pagi.pid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | ExecStart=/usr/local/bin/pagi-server \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | --host 0.0.0.0 --port 8080 \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | --workers 4 --pid /var/run/myapp/pagi.pid \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | --daemonize /opt/myapp/app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | ExecReload=/bin/kill -USR2 $MAINPID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | KillMode=process | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | =head2 Failure Handling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | The design ensures the old master never stops until the new master explicitly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | sends C<SIGTERM>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | =item * B<Fork failure> — The old master logs the error and continues serving. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | No new master is started. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | =item * B<Exec failure> — The forked child exits before loading any code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | The old master notices (via C<waitpid>) and continues serving. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | =item * B<New master crash during startup> — The old master never receives | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | C<SIGTERM> and continues serving indefinitely. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | =item * B<Workers fail lifespan startup> — The new master exits without | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | sending C<SIGTERM>. The old master is unaffected. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | =head2 PERL5LIB and Module Paths | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | When the new master is C<exec>'d it inherits the process environment, but | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | B<not> any C<-I> flags that were on the original command line. If your | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | application uses C<-Ilib>, ensure C<PERL5LIB> is set in the environment or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | in the systemd unit file so the re-exec'd process can find your modules. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | Alternatively, use the C<--lib> flag (C<pagi-server --lib ./lib ./app.pl>), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | which is captured in C<PAGI_ARGV> and replayed on re-exec. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | =head1 WINDOWS SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | B<PAGI::Server does not support Windows.> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | The server relies on Unix-specific features that are not available on Windows: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | =item * B<Unix signals> - SIGTERM, SIGINT, SIGHUP for graceful shutdown and worker management | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | =item * B<fork()> - Multi-worker mode requires real process forking, not thread emulation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | =item * B<IO::Async internals> - The event loop has Unix-specific optimizations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | For Windows development, consider using WSL (Windows Subsystem for Linux) to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | run PAGI::Server in a Linux environment. The PAGI specification and middleware | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | components can still be developed and unit-tested on Windows, but the reference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | server implementation requires a Unix-like operating system. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | my $server = PAGI::Server->new(%options); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | Creates a new PAGI::Server instance. Options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | =item app => $app (required) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | Either a PAGI application coderef with signature | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | C<async sub ($scope, $receive, $send)> or an instantiated application-provider | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | object implementing C<to_app>. A provider is normalized exactly once during | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | construction, and C<to_app> must return a coderef. Package-name strings are not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | providers. C<configure(app =E<gt> $app)> applies the same normalization rules. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | Only the normalized coderef is retained for per-connection dispatch. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | =item host => $host | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | Bind address (IP address or hostname). Default: C<'127.0.0.1'> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | The default binds only to the loopback interface, accepting connections only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | from localhost. This is B<intentionally secure by default> - development | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | servers won't accidentally be exposed to the network. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | B<Common values:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | '127.0.0.1' - Localhost only (default, secure for development) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | '0.0.0.0' - All IPv4 interfaces (required for remote access) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | '::' - All IPv6 interfaces (may also accept IPv4) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | '192.168.1.100' - Specific interface only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | B<For headless servers or production deployments> where remote clients need | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | to connect, bind to all interfaces: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | host => '0.0.0.0', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | port => 8080, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | B<Security note:> When binding to C<0.0.0.0>, ensure appropriate firewall | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | rules are in place. For production, consider a reverse proxy (nginx, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | =item port => $port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | Bind port. Default: 5000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | =item socket => $path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | B<Experimental.> Unix domain socket path for listening instead of TCP host:port. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | B<Mutually exclusive> with C<host>, C<port>, and C<listen>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | See L</UNIX DOMAIN SOCKET SUPPORT (EXPERIMENTAL)> for details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | socket => '/tmp/pagi.sock', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | =item socket_mode => $mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | Set file permissions on the Unix domain socket after creation. The value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | should be a numeric mode (e.g., C<0660>). If not specified, the socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | inherits the default permissions from the process umask. Silently ignored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | if C<socket> is not set. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | socket => '/tmp/pagi.sock', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | socket_mode => 0660, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | =item listen => \@specs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | B<Experimental.> Array of listener specifications for multi-endpoint listening. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | Each spec is a hashref with either C<< { host, port } >> for TCP or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | C<< { socket, socket_mode } >> for Unix domain sockets. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | B<Mutually exclusive> with C<host>, C<port>, C<socket>, and C<socket_mode>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | See L</MULTI-LISTENER SUPPORT (EXPERIMENTAL)> for details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | listen => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | { host => '0.0.0.0', port => 8080 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | { socket => '/tmp/pagi.sock', socket_mode => 0660 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | =item ssl => \%config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | Optional TLS/HTTPS configuration. B<Requires additional modules> - see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | L</ENABLING TLS SUPPORT> below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | Configuration keys: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | =item cert_file => $path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | Path to the SSL certificate file (PEM format). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | =item key_file => $path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | Path to the SSL private key file (PEM format). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | =item ca_file => $path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | Optional path to CA certificate for client verification. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | =item verify_client => $bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | If true, require and verify client certificates. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | =item min_version => $version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | Minimum TLS version -- a B<floor>, not a pin: the server negotiates the highest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | version the client also supports at or above this. Default: C<'TLSv1_2'> (so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | TLS 1.3 is used with modern clients). Options: C<'TLSv1_2'>, C<'TLSv1_3'>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | =item cipher_list => $string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | OpenSSL cipher list. Default uses modern secure ciphers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | ssl => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | cert_file => '/path/to/server.crt', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | key_file => '/path/to/server.key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | =item disable_tls => $bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | Force-disable TLS even if ssl config is provided. Useful for testing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | TLS configuration parsing without actually enabling TLS. Default: false. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | =item extensions => \%extensions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | Extensions to advertise (e.g., { fullflush => {} }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | =item access_log => $filehandle | undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | Access log filehandle. Default: STDERR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | Set to C<undef> to disable access logging entirely. This eliminates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | per-request I/O overhead, improving throughput by 5-15% depending on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | workload. Useful for benchmarking or when access logs are handled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | externally (e.g., by a reverse proxy). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | # Disable access logging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | access_log => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | =item access_log_format => $format_or_preset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | Access log format string or preset name. Default: C<'clf'> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | Named presets: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | clf - PAGI default: IP, timestamp, method/path, status, duration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | combined - Apache combined: adds Referer and User-Agent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | common - Apache common: adds response size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | tiny - Minimal: method, path, status, duration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | Custom format strings use Apache-style atoms. See L</ACCESS LOG FORMAT>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | access_log_format => 'combined', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | =item log_level => $level | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | Controls the verbosity of server log messages. Default: 'info' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | Valid levels (from least to most verbose): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | =item * B<error> - Only errors (application errors, fatal conditions) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | =item * B<warn> - Warnings and errors (connection issues, timeouts) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | =item * B<info> - Informational messages and above (startup, shutdown, worker spawning) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | =item * B<debug> - Everything (verbose diagnostics, frame-level details) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | log_level => 'debug', # Very verbose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | B<CLI:> C<--log-level debug> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | C<log_level> is the only control over how much the server says. In | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | multi-worker mode it governs the workers as well as the master: a worker's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | messages carry a C<Worker N (pid):> prefix, and an unprefixed line is the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | master's. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | =item logger => $coderef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | Where the server's own diagnostics go. Receives one hashref per message: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | logger => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | my ($event) = @_; # { level, message, category } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | $log_dispatch->log( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | level => $event->{level} eq 'fatal' ? 'critical' : $event->{level}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | message => "[$event->{category}] $event->{message}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | C<level> is one of C<debug>, C<info>, C<warn>, C<error>, C<fatal> -- the five | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | L<PSGI> and Rack settled on. C<message> carries no trailing newline. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | C<category> is C<PAGI::Server>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | A coderef rather than a logging object on purpose: frameworks disagree about | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | level names -- Log::Dispatch has no C<fatal> and silently discards a message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | sent at that level -- so the closure is where the translation belongs: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | use Log::Dispatch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | my $log = Log::Dispatch->new(outputs => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | ['Screen', min_level => 'debug', newline => 1], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | ['File', min_level => 'warning', newline => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | filename => '/var/log/pagi/server.log', mode => 'append'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | my %LEVEL = (fatal => 'critical'); # Log::Dispatch is syslog-named | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | log_level => 'debug', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | logger => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | my ($event) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | $log->log( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | level => $LEVEL{ $event->{level} } // $event->{level}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | message => "[$event->{category}] $event->{message}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | Note the two thresholds doing different jobs. C<log_level> is the server's: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | below it, nothing reaches the sink at all. C<min_level> on each output is the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | operator's: the screen takes everything, the file takes problems only. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | server's threshold is a floor, not a policy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | C<< examples/13-custom-logging >> is a runnable version of this, along with a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | structured-JSON variant. If you only want the diagnostics in a file rather than | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | reshaped, C<< pagi-server --error-log FILE >> does that without a runner script. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | Default: emit C<< $event->{message} >> to C<STDERR> with a trailing newline, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | which is what the server did before the destination was replaceable. Messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | below C<log_level> never reach the sink at all. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | B<This logger is deliberately not published into the request scope.> It is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | the operator's channel, filtered by the operator's C<log_level>; an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | application's diagnostics should not disappear because someone set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | C<--log-level error> to quiet the server down. Applications and middleware | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | have their own arrangements -- see L<PAGI::Tools> -- and a runner that wants | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | one destination for both can pass the same coderef to each. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | =item quiet => $bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | B<Deprecated.> A spelling of C<< log_level => 'error' >>, kept so existing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | callers keep working. An explicit C<log_level> wins when both are supplied. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | In earlier releases this suppressed everything below C<error> B<regardless | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | of> C<log_level>, and workers were started with it set -- so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | C<--log-level debug> combined with C<--workers> reported nothing from the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | processes actually serving traffic. Use C<< log_level => 'error' >> instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | B<CLI:> C<-q>, C<--quiet> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | =item workers => $count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | Number of worker processes for multi-worker mode. Default: 0 (single process mode). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | When set to a value greater than 0, the server uses a pre-fork model: it spawns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | C<$count> independent worker processes, each with its own event loop, and each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | running the C<lifespan> startup/shutdown cycle independently. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | B<Workers do not share memory.> Each worker is a separate process. Anything your | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | application keeps in memory -- a cache, a counter, a registry, an open database | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | handle, a pub/sub hub stored in C<< $scope->{state} >> -- exists once B<per | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | worker> and does not propagate to the others. This is inherent to C<fork>, and it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | is exactly how the wider async ecosystem behaves: gunicorn/uvicorn workers, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | processes on separate hosts, are isolated the same way. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | The practical consequences: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | =item * A B<stateless> application -- or one whose shared state lives in an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | external store such as a database or Redis -- scales across workers with no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | special handling. This is the common case. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | =item * An application that relies on B<in-memory shared state> will see it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | diverge: each worker holds its own copy, and a value written by one worker is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | invisible to the rest. Open per-worker resources (database connections and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | like) in the C<lifespan> startup -- which runs in each worker -- rather than | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | before calling C<run()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | =item * To share B<events> across workers -- one worker publishes, handlers on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | every worker receive -- you need an external broker, not in-memory state. See | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | L<PAGI::Middleware::Channels>; its Redis backend is the cross-process layer, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | same role Django Channels' channel layer plays for ASGI. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | The same boundary applies when you scale to multiple B<nodes> (for example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | Kubernetes pods): nothing in process memory crosses it. In-memory state is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | single-process; anything that must be shared has to live outside the process. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | =item listener_backlog => $number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | Value for the listener queue size. Default: 2048 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | When in multi worker mode, the queue size for those workers inherits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | from this value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | B<Which process consumes it:> the listening process -- the parent in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | traditional (shared-socket) mode, or each worker in C<reuseport> mode. In | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | both cases it's the master's configured value being read at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | socket-creation time, not an independently configurable per-worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | option. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | =item reuseport => $bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | Enable SO_REUSEPORT mode for multi-worker servers. Default: 0 (disabled). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | When enabled, each worker process creates its own listening socket with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | SO_REUSEPORT, allowing the kernel to load-balance incoming connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | across workers. This can reduce accept() contention and improve p99 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | latency under high concurrency. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | B<Traditional mode (reuseport=0):> Parent creates one socket before forking, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | all workers inherit and share that socket. Workers compete on a single | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | accept queue (potential thundering herd). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | B<Reuseport mode (reuseport=1):> Each worker creates its own socket with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | SO_REUSEPORT. The kernel distributes connections across sockets, each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | worker has its own accept queue (reduced contention). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | B<Platform notes:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | =item * B<Linux 3.9+>: Full kernel-level load balancing. Recommended for high | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | concurrency workloads. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | =item * B<macOS/BSD>: SO_REUSEPORT allows multiple binds but does NOT provide | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | kernel load balancing. May actually decrease performance compared to shared | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | socket mode. Use with caution - benchmark before deploying. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | B<Which process consumes it:> read by the parent to decide whether to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | create one shared listening socket or let each worker create its own; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | each worker then acts on this same master-configured value when creating | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | its own socket. It is not an independently configurable per-worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | option. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | =item max_receive_queue => $count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | Maximum number of messages that can be queued in the WebSocket receive queue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | before the connection is closed. This is a DoS protection mechanism. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | B<Unit:> Message count (not bytes). Each WebSocket text or binary frame counts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | as one message regardless of size. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | B<Default:> 1000 messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | B<When exceeded:> The server sends a WebSocket close frame with code 1008 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | (Policy Violation) and reason "Message queue overflow", then closes the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | This value is also advertised to the application as the C<max_receive_queue> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | key on the websocket scope, per L<PAGI::Spec::Www/"WebSocket Scope"> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | always present, since this cap is always enforced. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | B<Tuning guidelines:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | =item * B<Memory impact:> Each queued message holds the full message payload. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | With default of 1000 messages and average 1KB messages, worst case is ~1MB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | per slow connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | =item * B<Workers:> Total memory risk = workers × max_connections × max_receive_queue × avg_message_size. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | For 4 workers, 100 connections each, 1000 queue, 1KB average = 400MB worst case. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | =item * B<Fast consumers:> If your app processes messages quickly, the queue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | rarely grows. Default of 1000 is generous for most applications. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | =item * B<Slow consumers:> If your app does expensive processing per message, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | consider lowering to 100-500 to limit memory exposure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | =item * B<High throughput:> If you have trusted clients sending rapid bursts, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | you may increase to 5000-10000, but monitor memory usage. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | B<CLI:> C<--max-receive-queue 500> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | =item max_ws_frame_size => $bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | Maximum size in bytes for a single WebSocket frame payload. When a client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | sends a frame larger than this limit, the connection is closed with a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | protocol error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | B<Unit:> Bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | B<Default:> 65536 (64KB) - matches Protocol::WebSocket default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | B<When exceeded:> The server closes the connection. The error is logged as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | "PAGI connection error: Payload is too big." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | This value is also advertised to the application as the C<max_frame_size> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | key on the websocket scope, per L<PAGI::Spec::Www/"WebSocket Scope">, when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | enforced. Set to C<0> to disable the limit; the scope then omits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | C<max_frame_size> rather than advertising a cap that isn't enforced. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | B<Tuning guidelines:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | =item * B<Small messages:> For chat apps or control messages, default 64KB is plenty. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | =item * B<File uploads:> For binary data transfer via WebSocket, increase to 1MB-16MB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | depending on expected file sizes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | =item * B<Memory impact:> Each connection can buffer up to max_ws_frame_size bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | during frame parsing. High values increase memory per connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | =item * B<DoS protection:> Lower values limit memory exhaustion from malicious clients | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | sending oversized frames. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | B<CLI:> C<--max-ws-frame-size 1048576> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | =item max_connections => $count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | Maximum number of concurrent connections before returning HTTP 503. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | B<Default: 1000> (same as Mojolicious). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | When at capacity, new connections receive a 503 Service Unavailable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | response with a Retry-After header. This prevents resource exhaustion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | under heavy load. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | B<Which process consumes it:> in multi-worker mode, each worker enforces | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | this limit independently against its own accepted connections -- it is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | per-worker cap, not a total shared across all workers. C<workers> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | workers each configured with C<max_connections> => N gives an effective | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | ceiling of roughly C<workers * N> connections across the whole server. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | max_connections => 5000, # Higher limit for production | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | B<CLI:> C<--max-connections 5000> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | B<Monitoring:> Use C<< $server->connection_count >> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | C<< $server->effective_max_connections >> to monitor usage. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | B<Production Tuning:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | For high-traffic production deployments, you'll likely want to increase | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | this value. The optimal setting depends on your workload, available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | memory, and system file descriptor limits. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | I<Linux - Check and increase file descriptor limits:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | # Check current limits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | ulimit -n # Soft limit (per-process) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | cat /proc/sys/fs/file-max # System-wide limit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | # Increase for current session | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | ulimit -n 65536 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | # Permanent: add to /etc/security/limits.conf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | * soft nofile 65536 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | * hard nofile 65536 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | # Or for systemd services, in your unit file: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | [Service] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | LimitNOFILE=65536 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | I<macOS - Check and increase file descriptor limits:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | # Check current limits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | ulimit -n # Soft limit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | sysctl kern.maxfilesperproc # Per-process max | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1256 | # Increase for current session | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | ulimit -n 65536 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | # Permanent: add to /etc/launchd.conf or use launchctl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | sudo launchctl limit maxfiles 65536 200000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1262 | I<Rule of thumb:> Set C<max_connections> to roughly 80% of your file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1263 | descriptor limit to leave headroom for database connections, log files, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1264 | and other resources. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | =item write_high_watermark => $bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | B<Power user setting.> Maximum bytes to buffer in the socket write queue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | before applying backpressure. When exceeded, C<< $send->() >> calls will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | pause until the buffer drains below C<write_low_watermark>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | Default: 65536 (64KB). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | This prevents unbounded memory growth when the server writes data faster | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | than the client can receive it. The default matches Python's asyncio | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | transport defaults, providing a good balance between throughput and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | memory efficiency. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | B<When to adjust:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1282 | =item * B<Increase> (e.g., 256KB-1MB) for high-throughput bulk transfers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1283 | where you want fewer context switches and higher throughput at the cost | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | of more per-connection memory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | =item * B<Decrease> (e.g., 16KB-32KB) if supporting many concurrent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | connections where memory efficiency is critical. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1291 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | # High-throughput file server - larger buffers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1294 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | write_high_watermark => 262144, # 256KB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | write_low_watermark => 65536, # 64KB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1298 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | =item write_low_watermark => $bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | B<Power user setting.> Threshold below which sending resumes after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | backpressure was applied. Must be less than or equal to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1304 | C<write_high_watermark>. Default: 16384 (16KB, which is high/4). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | A larger gap between high and low watermarks reduces oscillation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | (frequent pause/resume cycles). A smaller gap provides more responsive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | backpressure but may increase context switching. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | # Minimize oscillation with wider gap | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1313 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | write_high_watermark => 131072, # 128KB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | write_low_watermark => 16384, # 16KB (8:1 ratio) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1317 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | =item max_body_size => $bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | Maximum request body size in bytes. Default: 10,000,000 (10MB). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | Set to 0 for unlimited (not recommended for public-facing servers). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1324 | Requests with Content-Length exceeding this limit receive HTTP 413 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | (Payload Too Large). Chunked requests are also checked as data arrives. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1327 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1331 | max_body_size => 50_000_000, # 50MB for file uploads | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | # Unlimited (use with caution) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | max_body_size => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1338 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1340 | B<CLI:> C<--max-body-size 50000000> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1342 | B<Security note:> Without a body size limit, attackers can exhaust server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1343 | memory with large requests. The 10MB default balances security with common | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1344 | use cases (file uploads, JSON payloads). Increase for specific needs, or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1345 | use 0 only behind a reverse proxy that enforces its own limit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1347 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1349 | =item * A listening socket is created before forking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1351 | =item * Worker processes are spawned using C<< $loop->fork() >> which properly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1352 | handles IO::Async's C<$ONE_TRUE_LOOP> singleton | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1354 | =item * Each worker gets a fresh event loop and runs lifespan startup independently | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1356 | =item * Workers that exit are automatically respawned via C<< $loop->watch_process() >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1358 | =item * Workers that fail lifespan startup (exit code 2) are B<not> respawned, since | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1359 | the failure would just repeat. If B<every> worker fails startup so that no workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1360 | remain, the master logs an error and exits non-zero, rather than holding the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1361 | listening socket with nothing serving -- so a process supervisor can restart it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1363 | =item * SIGTERM/SIGINT triggers graceful shutdown of all workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1365 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1367 | =item max_header_size => $bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1369 | Maximum size in bytes of the request line and of the combined header block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1370 | for an HTTP/1.x request. Default: 8192 (8KB). A request line exceeding this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1371 | limit receives HTTP 414 (URI Too Long); a header block exceeding it receives | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1372 | HTTP 431 (Request Header Fields Too Large). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1374 | B<CLI:> C<--max-header-size 16384> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1376 | =item max_header_count => $count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1377 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1378 | Maximum number of header fields permitted in an HTTP/1.x request. Default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1379 | 100. Requests with more headers receive HTTP 431 (Request Header Fields Too | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1380 | Large). This bounds memory and CPU spent parsing maliciously large header | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1381 | sets. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1382 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1383 | B<CLI:> C<--max-header-count 200> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1385 | =item sync_file_threshold => $bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1387 | Threshold in bytes for synchronous file reads. Files smaller than this value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1388 | are read synchronously in the event loop; larger files use async I/O via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1389 | a worker pool. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1391 | B<Default:> 65536 (64KB) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1393 | Set to 0 for fully async file reads. This is recommended for: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1395 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1397 | =item * Network filesystems (NFS, SMB, cloud storage) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1399 | =item * High-latency storage (spinning disks under load) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1401 | =item * Docker volumes with overlay filesystem | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1403 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1404 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1405 | The default (64KB) is optimized for local SSDs where small synchronous reads | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1406 | are faster than the overhead of async I/O. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1408 | B<CLI:> C<--sync-file-threshold NUM> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1410 | =item max_requests => $count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1412 | Maximum number of requests a worker process will handle before restarting. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1413 | After serving this many requests, the worker gracefully shuts down and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1414 | parent spawns a replacement. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1416 | B<Default:> 0 (disabled - workers run indefinitely) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1418 | B<When to use:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1419 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1420 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1422 | =item * Long-running deployments where gradual memory growth is a concern | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1424 | =item * Applications with known memory leaks that can't be easily fixed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1426 | =item * Defense against slow memory growth (~6.5 bytes/request observed in PAGI) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1428 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1430 | B<Note:> Only applies in multi-worker mode (C<< workers > 0 >>). In single-worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1431 | mode, this setting is ignored. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1433 | B<CLI:> C<--max-requests 10000> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1434 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1435 | Example: With 4 workers and max_requests=10000, total capacity before any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1436 | restart is 40,000 requests. Workers restart individually without downtime. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1437 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1438 | =item timeout => $seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1440 | Connection idle timeout in seconds. Closes connections that are idle between | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1441 | requests (applies to keep-alive connections waiting for the next request). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1442 | The internal disconnect reason recorded for this expiry follows | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1443 | L<PAGI::Spec::Www/"Standard Disconnect Reasons">: C<idle_timeout> when no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1444 | request has ever completed on this connection (the client connected but | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1445 | never sent one), C<keepalive_timeout> once a request has completed and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1446 | kept-alive connection then idles out waiting for the next one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1448 | B<Default:> 60 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1450 | B<Performance note:> Each connection with a non-zero timeout creates a timer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1451 | that is reset on every read event. For maximum throughput in high-performance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1452 | scenarios, set C<timeout =E<gt> 0> to disable the idle timer entirely. This | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1453 | eliminates timer management overhead but means idle connections will never | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1454 | be automatically closed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1455 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1456 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1458 | # Disable idle timeout for maximum performance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1459 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1460 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1461 | timeout => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1462 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1463 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1464 | # Short timeout to reclaim connections quickly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1465 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1466 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1467 | timeout => 30, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1468 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1470 | B<CLI:> C<--timeout 0> or C<--timeout 30> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1471 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1472 | B<Note:> This differs from C<request_timeout> (stall timeout during active | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1473 | request processing). The C<timeout> applies between requests; the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1474 | C<request_timeout> applies during a request. For WebSocket and SSE, use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1475 | C<ws_idle_timeout> and C<sse_idle_timeout> respectively. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1477 | =item lifespan_startup_timeout => $seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1478 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1479 | Maximum time in seconds to wait for the lifespan application to signal startup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1480 | (by sending C<lifespan.startup.complete> or C<lifespan.startup.failed>). If the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1481 | lifespan handler neither signals startup nor returns within this window, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1482 | server logs an error and aborts startup rather than blocking forever. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1483 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1484 | B<Default:> 30. C<< lifespan_startup_timeout => 0 >> is rejected at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1485 | construction and by C<configure()>: the PAGI Lifespan spec requires that a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1486 | server B<must not> block startup indefinitely waiting for a lifespan signal, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1487 | so there is no way to disable this bound. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1488 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1489 | =item lifespan_mode => 'auto' | 'on' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1490 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1491 | Controls how the server treats the lifespan protocol. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1493 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1495 | =item * B<auto> (default) -- run the lifespan handler if the application has one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1496 | If the application declines lifespan (by raising on the lifespan scope, or by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1497 | returning without signalling startup), the server continues without it. This is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1498 | the interoperable default: an application that does not implement lifespan still | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1499 | runs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1500 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1501 | =item * B<on> -- require lifespan. A decline at startup is B<fatal>: the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1502 | logs it and refuses to start. Use this for applications whose startup must | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1503 | succeed (for example, one that opens a database pool in lifespan). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1504 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1505 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1506 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1507 | An explicit C<lifespan.startup.failed> aborts startup in all modes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1508 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1509 | There is no C<off> mode: the PAGI Lifespan spec requires every server instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1510 | or worker to send the lifespan events and forbids an "off" switch that skips | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1511 | the protocol entirely ("Skipping the lifespan protocol entirely is not a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1512 | conforming option. ... A server must not offer an 'off' switch for this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1513 | protocol."). Passing C<< lifespan_mode => 'off' >> is rejected at construction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1514 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1515 | B<Multi-worker mode:> each worker forked under C<workers> runs its own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1516 | lifespan handshake against its own copy of the application and inherits both | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1517 | C<lifespan_mode> and C<lifespan_startup_timeout> from the master's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1518 | configuration -- a worker never silently falls back to the C<auto> default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1519 | when the master was configured with C<on>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1520 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1521 | =item request_timeout => $seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1522 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1523 | Maximum time in seconds a request can stall without any I/O activity before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1524 | being terminated. This is a "stall timeout" - the timer resets whenever data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1525 | is read from the client or written to the client. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1526 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1527 | B<Default:> 0 (disabled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1528 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1529 | B<Why disabled by default:> Creating per-request timers adds overhead that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1530 | impacts throughput on high-performance workloads. For maximum performance, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1531 | this is disabled by default. Most production deployments run behind a reverse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1532 | proxy (nginx, haproxy) which provides its own timeout protection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1533 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1534 | B<When to enable:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1535 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1536 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1537 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1538 | =item * Running PAGI directly without a reverse proxy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1539 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1540 | =item * Small/internal apps where simplicity matters more than max throughput | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1541 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1542 | =item * Untrusted clients that might send data slowly or hang | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1544 | =item * Defense against application bugs that cause requests to hang indefinitely | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1545 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1546 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1547 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1548 | B<How it works:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1549 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1550 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1551 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1552 | =item * Timer starts when request processing begins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1553 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1554 | =item * Timer resets on any read activity (receiving request body) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1556 | =item * Timer resets on any write activity (sending response) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1557 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1558 | =item * If timer expires (no I/O for N seconds), connection is closed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1559 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1560 | =item * Not used for WebSocket/SSE (they have C<ws_idle_timeout>/C<sse_idle_timeout>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1561 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1562 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1563 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1564 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1565 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1566 | # Enable 30 second stall timeout (recommended when not behind proxy) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1567 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1568 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1569 | request_timeout => 30, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1570 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1571 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1572 | B<CLI:> C<--request-timeout 30> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1573 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1574 | B<Note:> This differs from C<timeout> (idle connection timeout). The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1575 | C<timeout> applies between requests on keep-alive connections. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1576 | C<request_timeout> applies during active request processing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1577 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1578 | =item ws_idle_timeout => $seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1579 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1580 | Maximum time in seconds a WebSocket connection can be idle without any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1581 | activity (no messages sent or received) before being closed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1582 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1583 | B<Default:> 0 (disabled - WebSocket connections can be idle indefinitely) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1585 | When enabled, the timer resets on: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1586 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1587 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1588 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1589 | =item * Sending any WebSocket frame (accept, send, ping, close) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1590 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1591 | =item * Receiving any WebSocket frame from client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1592 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1593 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1594 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1595 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1596 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1597 | # Close idle WebSocket connections after 5 minutes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1598 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1599 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1600 | ws_idle_timeout => 300, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1601 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1602 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1603 | B<CLI:> C<--ws-idle-timeout 300> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1604 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1605 | B<Note:> For more sophisticated keep-alive behavior with ping/pong, use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1606 | C<< $ws->keepalive($interval, $timeout) >> for protocol-level ping/pong. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1607 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1608 | =item sse_idle_timeout => $seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1609 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1610 | Maximum time in seconds an SSE connection can be idle without any events | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1611 | being sent before being closed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1612 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1613 | B<Default:> 0 (disabled - SSE connections can be idle indefinitely) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1614 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1615 | The timer resets each time an event is sent to the client (including | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1616 | comments and the initial headers). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1617 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1618 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1619 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1620 | # Close idle SSE connections after 2 minutes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1621 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1622 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1623 | sse_idle_timeout => 120, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1624 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1625 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1626 | B<CLI:> C<--sse-idle-timeout 120> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1627 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1628 | B<Note:> For SSE connections that may be legitimately idle, use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1629 | C<< $sse->keepalive($interval) >> to send periodic comment keepalives. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1630 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1631 | B<HTTP/2 caveat:> Over HTTP/2 this timeout applies at the connection level, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1632 | not per-stream. See L<PAGI::Server::Connection/SSE Idle Timeout over HTTP/2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1633 | for details and recommendations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1634 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1635 | =item heartbeat_timeout => $seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1636 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1637 | Worker liveness timeout in seconds. Only active in multi-worker mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1638 | (C<< workers > 0 >>). Has no effect when running without workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1639 | (C<< workers == 0 >>) — use C<timeout> for idle connection management there. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1640 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1641 | Each worker sends a heartbeat to the parent process via a Unix pipe at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1642 | an interval of C<heartbeat_timeout / 5>. The parent checks for missed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1643 | heartbeats every C<heartbeat_timeout / 2>. If a worker has not sent a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1644 | heartbeat within C<heartbeat_timeout> seconds, the parent kills it with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1645 | SIGKILL and respawns a replacement. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1646 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1647 | B<Which process consumes it:> both. The worker reads it to compute how | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1648 | often it writes a heartbeat ping; the parent reads it to compute how | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1649 | often it checks for missed heartbeats and how long to wait before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1650 | declaring a worker dead. Each worker process gets its own copy of this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1651 | value (propagated at spawn), so it always matches what the parent is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1652 | checking against. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1653 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1654 | B<What this detects:> Event loop starvation — when the worker's event | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1655 | loop is completely blocked and cannot process any events. This happens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1656 | with blocking syscalls (C<sleep()>, synchronous DNS, blocking database | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1657 | drivers), deadlocks, runaway CPU-bound computation, or any code that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1658 | does not yield to the event loop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1659 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1660 | B<What this does NOT detect:> Slow async operations. A request handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1661 | that does C<< await $db->query(...) >> for 5 minutes is fine — the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1662 | C<await> returns control to the event loop, so heartbeats continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1663 | normally. This value should be larger than the maximum time you expect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1664 | any single operation to block the event loop without yielding. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1665 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1666 | B<Default:> 50 (seconds). Set to 0 to disable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1667 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1668 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1669 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1670 | # Tighter heartbeat for latency-sensitive service | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1671 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1672 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1673 | workers => 4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1674 | heartbeat_timeout => 20, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1675 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1676 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1677 | # Disable heartbeat monitoring | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1678 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1679 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1680 | workers => 4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1681 | heartbeat_timeout => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1682 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1683 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1684 | B<CLI:> C<--heartbeat-timeout 20> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1685 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1686 | =item shutdown_timeout => $seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1687 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1688 | Maximum time in seconds to wait for graceful shutdown to complete. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1689 | Default: 30. On SIGTERM/SIGINT the server stops accepting new connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1690 | and waits up to this many seconds for in-flight requests to finish before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1691 | forcing remaining connections closed. In multi-worker mode it also bounds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1692 | how long the master waits for a worker to exit after SIGTERM before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1693 | escalating to SIGKILL. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1694 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1695 | B<CLI:> C<--shutdown-timeout 30> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1696 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1697 | =item validate_events => $bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1698 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1699 | B<Deprecated, no-op.> Outbound event validation (shape and send sequencing, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1700 | per the PAGI spec) is always performed on every send path, in every | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1701 | environment; there is no way to disable it. This option is accepted for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1702 | backward compatibility, is ignored, and will be removed in a future release. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1703 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1704 | =item loop_type => $backend | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1705 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1706 | Specifies the IO::Async::Loop subclass to use when calling C<run()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1707 | This option is ignored when embedding the server in an existing loop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1708 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1709 | B<Which process consumes it:> whichever process creates its own fresh | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1710 | loop -- the single (or embedding) process calling C<run()>, and, in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1711 | multi-worker mode, each worker. Workers always build a brand-new loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1712 | after forking (there is nothing to embed into post-fork), so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1713 | C<loop_type> applies there the same way it does under C<run()>, even | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1714 | though the master's own loop may itself be an embedder-supplied one that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1715 | ignores this option. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1716 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1717 | B<Default:> Auto-detect (IO::Async chooses the best available backend) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1718 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1719 | B<Common values:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1720 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1721 | 'EPoll' - Linux epoll (recommended for Linux) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1722 | 'EV' - libev-based (cross-platform, requires EV module) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1723 | 'Poll' - POSIX poll() (portable fallback) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1724 | 'Select' - select() (most portable, least scalable) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1725 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1726 | B<Example:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1727 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1728 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1729 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1730 | loop_type => 'EPoll', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1731 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1732 | $server->run; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1733 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1734 | B<CLI:> C<--loop EPoll> (via pagi-server) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1735 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1736 | B<Note:> The specified backend module must be installed. For example, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1737 | C<loop_type =E<gt> 'EPoll'> requires L<IO::Async::Loop::EPoll>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1738 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1739 | =item h2_max_concurrent_streams => $count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1740 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1741 | B<(Experimental - HTTP/2 support may change in future releases.)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1742 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1743 | Maximum number of concurrent HTTP/2 streams per connection. Each stream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1744 | represents an in-flight request/response exchange, bounding concurrent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1745 | resource consumption. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1746 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1747 | B<Note:> this does I<not> defend the HTTP/2 Rapid Reset attack | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1748 | (CVE-2023-44487) — reset streams never count against this limit. Rapid Reset is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1749 | defended by the incoming-C<RST_STREAM> rate limit; see C<h2_rst_rate_limit>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1750 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1751 | B<Default:> 100 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1752 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1753 | This matches Apache httpd, H2O, and Hypercorn defaults. The RFC 7540 default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1754 | is unlimited, but 100 is the industry consensus for a safe maximum. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1755 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1756 | B<Tuning:> Increase for API gateways handling many small concurrent requests. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1757 | Decrease for memory-constrained environments or when each request is expensive. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1758 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1759 | =item h2_rst_rate_limit => { burst => 1000, rate => 33 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1760 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1761 | B<(Experimental - HTTP/2.)> Token-bucket rate limit on incoming C<RST_STREAM> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1762 | frames, defending the HTTP/2 Rapid Reset attack (CVE-2023-44487). C<burst> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1763 | tokens are available immediately and refill at C<rate> per second; each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1764 | C<RST_STREAM> consumes one. When the bucket empties the server sends GOAWAY and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1765 | closes the connection. B<On by default> at nghttp2's defaults (burst 1000, rate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1766 | 33), which only an abusive create-and-reset flood will hit. Raise the values to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1767 | loosen. Requires C<Net::HTTP2::nghttp2> >= 0.008 (nghttp2 >= 1.57). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1768 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1769 | =item h2_initial_window_size => $bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1770 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1771 | B<(Experimental)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1772 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1773 | Initial HTTP/2 flow control window size per stream, in bytes. Controls how | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1774 | much data a client can send before the server must acknowledge receipt. Also | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1775 | affects how much response data the server can buffer per stream before the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1776 | client acknowledges. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1777 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1778 | B<Default:> 65535 (64KB minus 1, the RFC 7540 default) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1779 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1780 | B<Tuning:> Increase to 131072-262144 for high-throughput file upload/download | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1781 | workloads where the default window causes flow control stalls on high-latency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1782 | connections. The tradeoff is higher per-stream memory usage. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1783 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1784 | =item h2_max_frame_size => $bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1785 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1786 | B<(Experimental)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1787 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1788 | Maximum size of a single HTTP/2 frame payload, in bytes. Must be between | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1789 | 16384 (16KB, the RFC minimum) and 16777215 (16MB, the RFC maximum). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1790 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1791 | B<Default:> 16384 (16KB, the RFC 7540 default) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1792 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1793 | Most servers use the RFC default. Larger frames reduce framing overhead but | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1794 | increase head-of-line blocking within a stream. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1795 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1796 | =item h2_enable_push => $bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1797 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1798 | B<(Experimental)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1799 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1800 | Enable HTTP/2 server push (SETTINGS_ENABLE_PUSH). When enabled, the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1801 | can proactively push resources to the client before they are requested. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1802 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1803 | B<Default:> 0 (disabled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1804 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1805 | Server push is effectively deprecated. Chrome removed support in 2022, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1806 | and nginx deprecated it in version 1.25.1. Unless you have a specific use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1807 | case requiring server push, leave this disabled. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1808 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1809 | =item h2_enable_connect_protocol => $bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1810 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1811 | B<(Experimental)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1812 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1813 | Enable the Extended CONNECT protocol (RFC 8441, SETTINGS_ENABLE_CONNECT_PROTOCOL). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1814 | Required for WebSocket-over-HTTP/2 tunneling. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1815 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1816 | B<Default:> 1 (enabled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1817 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1818 | When enabled, clients can use the Extended CONNECT method with a C<:protocol> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1819 | pseudo-header to establish WebSocket connections over HTTP/2 streams. Disable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1820 | this only if you do not need WebSocket support over HTTP/2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1821 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1822 | =item h2_max_header_list_size => $bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1823 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1824 | B<(Experimental)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1825 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1826 | Maximum total size of the header block that the server will accept, in bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1827 | This is the sum of all header name lengths, value lengths, and 32-byte per-entry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1828 | overhead as defined by RFC 7540 Section 6.5.2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1829 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1830 | B<Default:> 65536 (64KB) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1831 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1832 | Matches Hypercorn and Node.js defaults. Provides a guard against header-based | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1833 | memory exhaustion attacks while being generous enough for normal use including | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1834 | large cookies and authorization tokens. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1835 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1836 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1837 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1838 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1839 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1840 | =head2 listen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1841 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1842 | my $future = $server->listen; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1843 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1844 | Starts listening for connections. Returns a Future that completes when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1845 | the server is ready to accept connections. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1846 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1847 | =head2 shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1848 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1849 | my $future = $server->shutdown; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1850 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1851 | Initiates graceful shutdown. Returns a Future that completes when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1852 | shutdown is complete. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1853 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1854 | =head2 port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1855 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1856 | my $port = $server->port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1857 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1858 | Returns the bound port number. Useful when port => 0 is used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1859 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1860 | =head2 socket_path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1861 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1862 | my $path = $server->socket_path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1863 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1864 | Returns the Unix socket path of the first Unix socket listener, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1865 | or C<undef> if no Unix socket listeners are configured. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1866 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1867 | =head2 listeners | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1868 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1869 | my $listeners = $server->listeners; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1870 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1871 | Returns an arrayref of all normalized listener specifications. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1872 | Each entry is a hashref with C<type> (C<'tcp'> or C<'unix'>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1873 | and type-specific keys (C<host>/C<port> for TCP, C<path> for Unix). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1874 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1875 | =head2 is_running | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1876 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1877 | my $bool = $server->is_running; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1878 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1879 | Returns true if the server is accepting connections. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1880 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1881 | =head2 connection_count | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1882 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1883 | my $count = $server->connection_count; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1884 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1885 | Returns the current number of active connections. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1886 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1887 | =head2 effective_max_connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1888 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1889 | my $max = $server->effective_max_connections; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1890 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1891 | Returns the effective maximum connections limit. If C<max_connections> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1892 | was set explicitly, returns that value. Otherwise returns the default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1893 | of 1000. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1894 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1895 | =head1 FILE RESPONSE STREAMING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1896 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1897 | PAGI::Server supports efficient file streaming via the C<file> and C<fh> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1898 | keys in C<http.response.body> events: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1899 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1900 | # Stream entire file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1901 | await $send->({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1902 | type => 'http.response.body', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1903 | file => '/path/to/file.mp4', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1904 | more => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1905 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1906 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1907 | # Stream partial file (for Range requests) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1908 | await $send->({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1909 | type => 'http.response.body', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1910 | file => '/path/to/file.mp4', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1911 | offset => 1000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1912 | length => 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1913 | more => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1914 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1915 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1916 | # Stream from filehandle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1917 | open my $fh, '<:raw', $file; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1918 | await $send->({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1919 | type => 'http.response.body', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1920 | fh => $fh, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1921 | length => $size, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1922 | more => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1923 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1924 | close $fh; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1925 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1926 | The server streams files in 64KB chunks to avoid memory bloat. Small files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1927 | (under 64KB) are read synchronously for speed; larger files use async I/O | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1928 | via a worker pool (L<PAGI::Server::AsyncFile>, backed by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1929 | L<IO::Async::Function>) to avoid blocking the event loop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1930 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1931 | B<Deploy note:> a very large C<RLIMIT_NOFILE> (soft file-descriptor limit) on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1932 | the host -- around a million, the kind some container base images or systemd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1933 | units set by default -- makes each worker in this async-file-I/O pool take | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1934 | over a second to start (measured: approximately 1.025s/worker at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1935 | C<nofile=1048576>). The cost is C<IO::Async::Internals::ChildManager>'s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1936 | post-fork file-descriptor-table sweep (it walks C<0 .. SC_OPEN_MAX>, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1937 | path C<IO::Async::Function> uses to spawn a worker via C<spawn_child>); the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1938 | sweep's cost scales with the configured limit, not with how many descriptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1939 | are actually open. This is specific to this worker pool: C<workers> worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1940 | processes spawn via a bare C<fork> (no such sweep) and are unaffected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1941 | (measured: approximately 0.026s at the same C<nofile> setting). If large | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1942 | files are served often enough that AsyncFile worker startup latency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1943 | matters, cap C<RLIMIT_NOFILE> to a realistic value for the deployment (a few | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1944 | tens of thousands is enough for very high concurrency; see C<ulimit -n> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1945 | under L</System Tuning>) before starting the server. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1946 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1947 | =head2 Production Recommendations for Static Files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1948 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1949 | B<For production deployments, we strongly recommend delegating static file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1950 | serving to a reverse proxy:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1951 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1952 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1953 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1954 | =item 1. B<Use nginx, Apache, or a CDN> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1955 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1956 | Place a reverse proxy in front of PAGI::Server and let it handle static | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1957 | files directly. This provides: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1958 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1959 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1960 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1961 | =item * Optimized file serving with kernel sendfile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1962 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1963 | =item * Efficient caching and compression | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1964 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1965 | =item * Protection from slow client attacks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1966 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1967 | =item * HTTP/2 and HTTP/3 support | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1968 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1969 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1970 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1971 | =item 2. B<Use L<PAGI::Middleware::XSendfile>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1972 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1973 | For files that require authentication or authorization, use the XSendfile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1974 | middleware to delegate file serving to the reverse proxy: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1975 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1976 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1977 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1978 | my $app = builder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1979 | enable 'XSendfile', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1980 | type => 'X-Accel-Redirect', # For Nginx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1981 | mapping => { '/var/www/protected/' => '/internal/' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1982 | $my_app; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1983 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1984 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1985 | See L<PAGI::Middleware::XSendfile> for details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1986 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1987 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1988 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1989 | =head1 ENABLING TLS SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1990 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1991 | PAGI::Server supports HTTPS/TLS connections, but requires additional modules | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1992 | that are not installed by default. This keeps the base installation minimal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1993 | for users who don't need TLS. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1994 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1995 | =head2 When You Need TLS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1996 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1997 | You need TLS if you want to: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1998 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1999 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2000 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2001 | =item * Serve HTTPS traffic directly from PAGI::Server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2002 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2003 | =item * Test TLS locally during development | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2004 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2005 | =item * Use client certificate authentication | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2006 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2007 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2008 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2009 | You B<don't> need TLS if you: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2010 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2011 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2012 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2013 | =item * Use a reverse proxy (nginx, Apache) that handles TLS termination | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2014 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2015 | =item * Only serve HTTP traffic on localhost for development | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2016 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2017 | =item * Deploy behind a load balancer that provides TLS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2018 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2019 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2020 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2021 | B<Production recommendation:> Use a reverse proxy (nginx, HAProxy, etc.) for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2022 | TLS termination. They offer better performance, easier certificate management, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2023 | and battle-tested security. PAGI::Server's TLS support is primarily for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2024 | development and testing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2025 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2026 | =head2 Installing TLS Modules | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2027 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2028 | To enable TLS support, install the required modules: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2029 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2030 | B<Using cpanm:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2031 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2032 | cpanm IO::Async::SSL IO::Socket::SSL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2033 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2034 | B<Using system packages (Debian/Ubuntu):> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2035 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2036 | apt-get install libio-socket-ssl-perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2037 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2038 | B<Using system packages (RHEL/CentOS):> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2039 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2040 | yum install perl-IO-Socket-SSL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2041 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2042 | B<Verifying installation:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2043 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2044 | perl -MIO::Async::SSL -MIO::Socket::SSL -e 'print "TLS modules installed\n"' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2045 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2046 | =head2 Basic TLS Configuration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2047 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2048 | Once the modules are installed, configure TLS with certificate and key files: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2049 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2050 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2051 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2052 | host => '0.0.0.0', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2053 | port => 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2054 | ssl => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2055 | cert_file => '/path/to/server.crt', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2056 | key_file => '/path/to/server.key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2057 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2058 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2059 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2060 | =head2 Generating Self-Signed Certificates (Development) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2061 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2062 | For local development and testing, you can generate a self-signed certificate: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2063 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2064 | B<Quick self-signed certificate (1 year validity):> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2065 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2066 | openssl req -x509 -newkey rsa:4096 -nodes \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2067 | -keyout server.key -out server.crt -days 365 \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2068 | -subj "/CN=localhost" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2069 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2070 | B<With Subject Alternative Names (recommended):> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2071 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2072 | # Create config file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2073 | cat > ssl.conf <<EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2074 | [req] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2075 | distinguished_name = req_distinguished_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2076 | x509_extensions = v3_req | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2077 | prompt = no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2078 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2079 | [req_distinguished_name] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2080 | CN = localhost | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2081 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2082 | [v3_req] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2083 | subjectAltName = @alt_names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2084 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2085 | [alt_names] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2086 | DNS.1 = localhost | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2087 | DNS.2 = *.localhost | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2088 | IP.1 = 127.0.0.1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2089 | EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2090 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2091 | # Generate certificate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2092 | openssl req -x509 -newkey rsa:4096 -nodes \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2093 | -keyout server.key -out server.crt -days 365 \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2094 | -config ssl.conf -extensions v3_req | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2095 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2096 | B<Testing your TLS configuration:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2097 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2098 | # Start server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2099 | pagi-server --app myapp.pl --ssl-cert server.crt --ssl-key server.key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2101 | # Test with curl (ignore self-signed cert warning) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2102 | curl -k https://localhost:5000/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2104 | B<Production certificates:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2106 | For production, use certificates from a trusted CA (Let's Encrypt, etc.): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2108 | # Let's Encrypt with certbot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2109 | certbot certonly --standalone -d yourdomain.com | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2111 | # Then configure PAGI::Server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2112 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2113 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2114 | ssl => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2115 | cert_file => '/etc/letsencrypt/live/yourdomain.com/fullchain.pem', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2116 | key_file => '/etc/letsencrypt/live/yourdomain.com/privkey.pem', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2117 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2118 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2120 | =head2 Advanced TLS Configuration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2122 | See the C<ssl> option in L</CONSTRUCTOR> for details on: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2124 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2126 | =item * Client certificate verification (C<verify_client>, C<ca_file>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2128 | =item * TLS version requirements (C<min_version>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2130 | =item * Custom cipher suites (C<cipher_list>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2132 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2134 | =head1 ENABLING HTTP/2 SUPPORT (EXPERIMENTAL) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2136 | B<HTTP/2 support is experimental.> The API and behavior may change in future | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2137 | releases. Please report issues and provide feedback. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2139 | PAGI::Server provides native HTTP/2 support via the nghttp2 C library | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2140 | (L<Net::HTTP2::nghttp2>). When enabled, the server supports both TLS-based | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2141 | HTTP/2 (h2 via ALPN negotiation) and cleartext HTTP/2 (h2c). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2143 | =head2 Requirements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2145 | L<Net::HTTP2::nghttp2> must be installed, which requires the nghttp2 C library: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2147 | # Install the C library | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2148 | brew install nghttp2 # macOS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2149 | apt-get install libnghttp2-dev # Debian/Ubuntu | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2151 | # Install the Perl bindings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2152 | cpanm Net::HTTP2::nghttp2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2154 | =head2 Enabling HTTP/2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2156 | B<Via CLI (pagi-server):> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2158 | # HTTP/2 over TLS (recommended for production) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2159 | pagi-server --http2 --ssl-cert cert.pem --ssl-key key.pem --app myapp.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2161 | # HTTP/2 cleartext (h2c, for development/testing) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2162 | pagi-server --http2 --app myapp.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2164 | B<Via constructor:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2166 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2167 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2168 | http2 => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2169 | ssl => { cert_file => 'cert.pem', key_file => 'key.pem' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2170 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2172 | =head2 How It Works | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2174 | With TLS, the server advertises C<h2> and C<http/1.1> via ALPN during the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2175 | TLS handshake. Clients that support HTTP/2 will negotiate C<h2> automatically; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2176 | others fall back to HTTP/1.1 transparently. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2178 | Without TLS, the server detects HTTP/2 via the client connection preface | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2179 | (h2c mode). HTTP/1.1 clients are handled normally. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2181 | =head2 HTTP/2 Features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2183 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2185 | =item * Stream multiplexing (100 concurrent streams per connection by default) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2187 | =item * HPACK header compression | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2189 | =item * Per-stream and connection-level flow control | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2191 | =item * GOAWAY graceful session shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2193 | =item * Stream state validation (RST_STREAM on protocol violations) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2195 | =item * WebSocket over HTTP/2 via Extended CONNECT (RFC 8441) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2197 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2199 | =head2 Conformance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2201 | Tested against h2spec (the HTTP/2 conformance test suite): B<137/146 (93.8%)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2202 | All 9 remaining failures are shared with the bare nghttp2 C library and cannot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2203 | be fixed at the application level. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2205 | Load tested with h2load: 60,000 requests across 50 concurrent connections with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2206 | zero failures. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2208 | See L<PAGI::Server::Compliance> for full compliance details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2210 | =head2 Configuration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2212 | HTTP/2 protocol settings are tuned via constructor options prefixed with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2213 | C<h2_>. See L</CONSTRUCTOR> for details on: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2215 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2217 | =item * C<h2_max_concurrent_streams> - Max streams per connection (default: 100) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2219 | =item * C<h2_rst_rate_limit> - Incoming C<RST_STREAM> rate limit; HTTP/2 Rapid Reset defense (default burst 1000 / rate 33) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2221 | =item * C<h2_initial_window_size> - Flow control window (default: 65535) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2223 | =item * C<h2_max_frame_size> - Max frame payload (default: 16384) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2225 | =item * C<h2_enable_push> - Server push (default: disabled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2227 | =item * C<h2_enable_connect_protocol> - WebSocket over HTTP/2 (default: enabled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2229 | =item * C<h2_max_header_list_size> - Max header block size (default: 65536) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2231 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2233 | =head1 SIGNAL HANDLING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2235 | PAGI::Server responds to Unix signals for process management. Signal behavior | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2236 | differs between single-worker and multi-worker modes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2238 | =head2 Supported Signals | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2240 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2242 | =item B<SIGTERM> - Graceful shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2244 | Initiates graceful shutdown. The server stops accepting new connections, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2245 | waits for active requests to complete (up to C<shutdown_timeout> seconds), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2246 | then exits. In multi-worker mode, SIGTERM is forwarded to all workers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2248 | kill -TERM <pid> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2250 | =item B<SIGINT> - Graceful shutdown (Ctrl-C) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2252 | Same behavior as SIGTERM. Triggered by Ctrl-C in the terminal. In multi-worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2253 | mode, the parent process catches SIGINT and coordinates shutdown of all workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2254 | to ensure proper lifespan.shutdown handling. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2256 | kill -INT <pid> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2257 | # or press Ctrl-C in terminal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2259 | =item B<SIGHUP> - Graceful worker restart (multi-worker only) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2261 | Performs a zero-downtime worker restart by spawning new workers before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2262 | terminating old ones. Useful for recycling workers to reclaim leaked memory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2263 | or reset per-worker state without dropping active connections. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2265 | B<Note:> This does NOT reload application code. New workers fork from the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2266 | existing parent process and inherit the same loaded code. For code deploys, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2267 | perform a full server restart (SIGTERM + start). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2269 | kill -HUP <pid> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2271 | In single-worker mode, SIGHUP is logged but ignored (no graceful restart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2272 | possible without multiple workers). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2274 | =item B<SIGTTIN> - Increase worker count (multi-worker only) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2276 | Spawns an additional worker process. Use this to scale up capacity dynamically. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2278 | kill -TTIN <pid> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2280 | =item B<SIGTTOU> - Decrease worker count (multi-worker only) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2282 | Gracefully terminates one worker process. The minimum worker count is 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2283 | sending SIGTTOU when only one worker remains has no effect. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2285 | kill -TTOU <pid> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2287 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2289 | =head2 Signal Handling in Multi-Worker Mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2291 | When running with C<< workers => N >> (where N > 1): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2293 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2295 | =item * Parent process manages the worker pool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2297 | =item * Workers handle requests; parent handles signals | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2299 | =item * SIGTERM/SIGINT to parent triggers coordinated shutdown of all workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2301 | =item * Each worker runs lifespan.shutdown before exiting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2303 | =item * Workers that crash are automatically respawned | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2305 | =item * Heartbeat monitoring detects workers with blocked event loops and replaces them automatically (see C<heartbeat_timeout>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2307 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2309 | =head2 Examples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2311 | B<Zero-downtime deployment:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2313 | # Deploy new code, then signal graceful restart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2314 | kill -HUP $(cat /var/run/pagi.pid) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2316 | B<Scale workers based on load:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2318 | # Add workers during peak hours | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2319 | kill -TTIN $(cat /var/run/pagi.pid) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2320 | kill -TTIN $(cat /var/run/pagi.pid) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2322 | # Remove workers during quiet periods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2323 | kill -TTOU $(cat /var/run/pagi.pid) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2325 | B<Graceful shutdown for maintenance:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2326 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2327 | # Stop accepting new connections, drain existing ones | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2328 | kill -TERM $(cat /var/run/pagi.pid) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2330 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2332 | 672 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=84 SCAR=44.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2333 | 672 | my ($self, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2335 | # Establish the log destination and threshold before anything else, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2336 | # a problem found while validating the remaining parameters can be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2337 | # reported through the channel the caller configured. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2338 | # Facts the runner knows and the server does not -- which application is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2339 | # being served, which mode resolved and why. Rendered in the startup block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2340 | # rather than printed separately, so startup reads as one thing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2341 | 672 | ▶ | $self->{startup_notes} = delete $params->{startup_notes} // []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"startup_notes"} // []
MC/DC100%1/1
delete $params->{"startup_notes"} // []
delete $params->{"startup_notes"}
Truth table100%2/2
A
A= delete $params->{"startup_notes"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2342 | 672 | $self->{logger} = delete $params->{logger}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2343 | die "Invalid logger - must be a coderef taking one hashref\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2344 | 672 | ▶ | if defined $self->{logger} && ref($self->{logger}) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"logger"} and ref $self->{"logger"} ne "CODE"
MC/DC0%0/2
defined $self->{"logger"} and ref $self->{"logger"} ne "CODE"
defined $self->{"logger"}
ref $self->{"logger"} ne "CODE"
Truth table66%2/3
A and B
A= defined $self->{"logger"}B= ref $self->{"logger"} ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2345 | # quiet is a deprecated spelling of log_level => 'error'. An explicit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2346 | # log_level wins: a threshold must not be overridden by a second control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2347 | 672 | my $quiet = delete $params->{quiet}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2348 | $self->{log_level} = delete $params->{log_level} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2349 | 672 | ▶ | // ($quiet ? 'error' : 'info'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
delete $params->{"log_level"} // ($quiet ? "error" : "info")
MC/DC100%1/1
delete $params->{"log_level"} // ($quiet ? "error" : "info")
delete $params->{"log_level"}
Truth table100%2/2
A
A= delete $params->{"log_level"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2350 | # Validate log level | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2351 | 672 | my %valid_levels = (debug => 1, info => 2, warn => 3, error => 4); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2352 | die "Invalid log_level '$self->{log_level}' - must be one of: debug, info, warn, error\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2353 | 672 | ▶ | unless $valid_levels{$self->{log_level}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2354 | 671 | $self->{_log_level_num} = $valid_levels{$self->{log_level}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2356 | 671 | my $app = delete $params->{app}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2357 | 671 | ▶ | die "app is required" unless defined $app; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2358 | 670 | $self->{app} = PAGI::Server::AppNormalizer::normalize_app($app, 'app'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2360 | # Extract listener-related params | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2361 | 669 | my $listen = delete $params->{listen}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2362 | 669 | my $socket = delete $params->{socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2363 | 669 | my $socket_mode = delete $params->{socket_mode}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2364 | 669 | my $host = delete $params->{host}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2365 | 669 | my $port = delete $params->{port}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2366 | 669 | $self->{ssl} = delete $params->{ssl}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2367 | 669 | ▶ | $self->{disable_tls} = delete $params->{disable_tls} // 0; # Extract early for validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"disable_tls"} // 0
MC/DC100%1/1
delete $params->{"disable_tls"} // 0
delete $params->{"disable_tls"}
Truth table100%2/2
A
A= delete $params->{"disable_tls"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2369 | # Validate SSL certificate files at startup (fail fast) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2370 | # Skip validation if TLS is explicitly disabled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2371 | 669 | ▶ | if (my $ssl = $self->{ssl}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2372 | 57 | ▶ | if ($self->{disable_tls}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2373 | # Skip TLS setup and cert validation — ssl config is stored but not applied | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2374 | 2 | $self->_log(warn => "PAGI::Server: TLS disabled via disable_tls option, ssl config ignored"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2375 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2376 | 55 | ▶ | if (my $cert = $ssl->{cert_file}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2377 | 53 | ▶ | die "SSL certificate file not found: $cert\n" unless -e $cert; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2378 | 52 | ▶ | die "SSL certificate file not readable: $cert\n" unless -r $cert; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2380 | 53 | ▶ | if (my $key = $ssl->{key_file}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2381 | 52 | ▶ | die "SSL key file not found: $key\n" unless -e $key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2382 | 51 | ▶ | die "SSL key file not readable: $key\n" unless -r $key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2383 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2384 | 52 | ▶ | if (my $ca = $ssl->{ca_file}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2385 | 9 | ▶ | die "SSL CA file not found: $ca\n" unless -e $ca; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2386 | 8 | ▶ | die "SSL CA file not readable: $ca\n" unless -r $ca; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2387 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2388 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2389 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2391 | # Normalize all listener forms to $self->{listeners} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2392 | 665 | ▶ | if ($listen) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2393 | # Explicit listen array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2394 | 31 | ▶ | die "Cannot specify both 'listen' and 'host' options\n" if defined $host; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2395 | 29 | ▶ | die "Cannot specify both 'listen' and 'port' options\n" if defined $port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2396 | 29 | ▶ | die "Cannot specify both 'listen' and 'socket' options\n" if defined $socket; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2397 | 29 | ▶ | die "Cannot specify both 'listen' and 'socket_mode' options\n" if defined $socket_mode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2398 | 29 | ▶ | die "'listen' must be a non-empty arrayref\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $listen eq "ARRAY" and @$listen
MC/DC50%1/2
ref $listen eq "ARRAY" and @$listen
ref $listen eq "ARRAY"
@$listen
Truth table66%2/3
A and B
A= ref $listen eq "ARRAY"B= @$listen
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2399 | unless ref $listen eq 'ARRAY' && @$listen; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2401 | 27 | $self->{listeners} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2402 | 27 | for my $spec (@$listen) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2403 | 34 | ▶ | die "Each listen spec must be a hashref\n" unless ref $spec eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2404 | 34 | ▶ | if ($spec->{socket}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2405 | 9 | ▶ | die "Cannot specify both 'socket' and 'host' in a listen spec\n" if $spec->{host}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2406 | 7 | ▶ | die "Cannot specify both 'socket' and 'port' in a listen spec\n" if $spec->{port}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2407 | 7 | push @{$self->{listeners}}, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2408 | type => 'unix', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2409 | path => $spec->{socket}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2410 | socket_mode => $spec->{socket_mode}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2411 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2412 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2413 | die "TCP listen spec requires both 'host' and 'port'\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2414 | 25 | ▶ | unless defined $spec->{host} && defined $spec->{port}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $spec->{"host"} and defined $spec->{"port"}
MC/DC50%1/2
defined $spec->{"host"} and defined $spec->{"port"}
defined $spec->{"host"}
defined $spec->{"port"}
Truth table66%2/3
A and B
A= defined $spec->{"host"}B= defined $spec->{"port"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2415 | 23 | push @{$self->{listeners}}, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2416 | type => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2417 | host => $spec->{host}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2418 | port => $spec->{port}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2419 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2420 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2421 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2422 | 23 | $self->{host} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2423 | 23 | $self->{port} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2424 | } elsif (defined $socket) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2425 | # Socket sugar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2426 | 19 | ▶ | die "Cannot specify both 'socket' and 'host' options\n" if defined $host; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2427 | 17 | ▶ | die "Cannot specify both 'socket' and 'port' options\n" if defined $port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2428 | $self->{listeners} = [{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2429 | 15 | type => 'unix', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2430 | path => $socket, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2431 | socket_mode => $socket_mode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2432 | }]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2433 | 15 | $self->{host} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2434 | 15 | $self->{port} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2435 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2436 | # Host/port sugar (backward compatible default) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2437 | 615 | ▶ | $host //= '127.0.0.1'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$host //= "127.0.0.1"
MC/DC100%1/1
$host //= "127.0.0.1"
$host
Truth table100%2/2
A
A= $host
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2438 | 615 | ▶ | $port //= 5000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$port //= 5000
MC/DC100%1/1
$port //= 5000
$port
Truth table100%2/2
A
A= $port
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2439 | $self->{listeners} = [{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2440 | 615 | type => 'tcp', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2441 | host => $host, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2442 | port => $port, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2443 | }]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2444 | 615 | $self->{host} = $host; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2445 | 615 | $self->{port} = $port; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2446 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2448 | # Apply server-wide SSL to all TCP listeners | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2449 | 653 | ▶ | if ($self->{ssl}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2450 | 53 | for my $listener (@{$self->{listeners}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2451 | 53 | ▶ | if ($listener->{type} eq 'tcp') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2452 | 53 | $listener->{ssl} = $self->{ssl}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2453 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2454 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2455 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2456 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2457 | 653 | ▶ | $self->{extensions} = delete $params->{extensions} // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"extensions"} // {}
MC/DC100%1/1
delete $params->{"extensions"} // {}
delete $params->{"extensions"}
Truth table100%2/2
A
A= delete $params->{"extensions"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2458 | 653 | ▶ | $self->{access_log} = exists $params->{access_log} ? delete $params->{access_log} : \*STDERR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2459 | 653 | ▶ | $self->{access_log_format} = delete $params->{access_log_format} // 'clf'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"access_log_format"} // "clf"
MC/DC100%1/1
delete $params->{"access_log_format"} // "clf"
delete $params->{"access_log_format"}
Truth table100%2/2
A
A= delete $params->{"access_log_format"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2460 | $self->{_access_log_formatter} = $self->_compile_access_log_format( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2461 | $self->{access_log_format} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2462 | 653 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2463 | 653 | ▶ | $self->{timeout} = delete $params->{timeout} // 60; # Connection idle timeout (seconds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"timeout"} // 60
MC/DC100%1/1
delete $params->{"timeout"} // 60
delete $params->{"timeout"}
Truth table100%2/2
A
A= delete $params->{"timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2464 | 653 | ▶ | $self->{max_header_size} = delete $params->{max_header_size} // 8192; # Max header size in bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"max_header_size"} // 8192
MC/DC100%1/1
delete $params->{"max_header_size"} // 8192
delete $params->{"max_header_size"}
Truth table100%2/2
A
A= delete $params->{"max_header_size"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2465 | 653 | ▶ | $self->{max_header_count} = delete $params->{max_header_count} // 100; # Max number of headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"max_header_count"} // 100
MC/DC100%1/1
delete $params->{"max_header_count"} // 100
delete $params->{"max_header_count"}
Truth table100%2/2
A
A= delete $params->{"max_header_count"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2466 | 653 | ▶ | $self->{max_body_size} = delete $params->{max_body_size} // 10_000_000; # Max body size in bytes (10MB default, 0 = unlimited) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"max_body_size"} // 10000000
MC/DC100%1/1
delete $params->{"max_body_size"} // 10000000
delete $params->{"max_body_size"}
Truth table100%2/2
A
A= delete $params->{"max_body_size"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2467 | 653 | ▶ | $self->{workers} = delete $params->{workers} // 0; # Number of worker processes (0 = single process) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"workers"} // 0
MC/DC100%1/1
delete $params->{"workers"} // 0
delete $params->{"workers"}
Truth table100%2/2
A
A= delete $params->{"workers"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2468 | 653 | ▶ | $self->{max_requests} = delete $params->{max_requests} // 0; # 0 = unlimited | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"max_requests"} // 0
MC/DC100%1/1
delete $params->{"max_requests"} // 0
delete $params->{"max_requests"}
Truth table100%2/2
A
A= delete $params->{"max_requests"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2469 | 653 | ▶ | $self->{listener_backlog} = delete $params->{listener_backlog} // 2048; # Listener queue size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $params->{"listener_backlog"} // 2048
MC/DC0%0/1
delete $params->{"listener_backlog"} // 2048
delete $params->{"listener_backlog"}
Truth table50%1/2
A
A= delete $params->{"listener_backlog"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2470 | 653 | ▶ | $self->{shutdown_timeout} = delete $params->{shutdown_timeout} // 30; # Graceful shutdown timeout (seconds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"shutdown_timeout"} // 30
MC/DC100%1/1
delete $params->{"shutdown_timeout"} // 30
delete $params->{"shutdown_timeout"}
Truth table100%2/2
A
A= delete $params->{"shutdown_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2471 | 653 | ▶ | $self->{reuseport} = delete $params->{reuseport} // 0; # SO_REUSEPORT mode for multi-worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $params->{"reuseport"} // 0
MC/DC0%0/1
delete $params->{"reuseport"} // 0
delete $params->{"reuseport"}
Truth table50%1/2
A
A= delete $params->{"reuseport"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2472 | 653 | ▶ | $self->{max_receive_queue} = delete $params->{max_receive_queue} // 1000; # Max WebSocket receive queue size (messages) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"max_receive_queue"} // 1000
MC/DC100%1/1
delete $params->{"max_receive_queue"} // 1000
delete $params->{"max_receive_queue"}
Truth table100%2/2
A
A= delete $params->{"max_receive_queue"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2473 | 653 | ▶ | $self->{max_ws_frame_size} = delete $params->{max_ws_frame_size} // 65536; # Max WebSocket frame size in bytes (64KB default) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"max_ws_frame_size"} // 65536
MC/DC100%1/1
delete $params->{"max_ws_frame_size"} // 65536
delete $params->{"max_ws_frame_size"}
Truth table100%2/2
A
A= delete $params->{"max_ws_frame_size"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2474 | 653 | ▶ | $self->{max_connections} = delete $params->{max_connections} // 0; # 0 = use default (1000) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"max_connections"} // 0
MC/DC100%1/1
delete $params->{"max_connections"} // 0
delete $params->{"max_connections"}
Truth table100%2/2
A
A= delete $params->{"max_connections"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2475 | 653 | ▶ | $self->{sync_file_threshold} = delete $params->{sync_file_threshold} // 65536; # Threshold for sync file reads (0=always async) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"sync_file_threshold"} // 65536
MC/DC100%1/1
delete $params->{"sync_file_threshold"} // 65536
delete $params->{"sync_file_threshold"}
Truth table100%2/2
A
A= delete $params->{"sync_file_threshold"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2476 | 653 | ▶ | $self->{request_timeout} = delete $params->{request_timeout} // 0; # Request stall timeout in seconds (0 = disabled, default for performance) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"request_timeout"} // 0
MC/DC100%1/1
delete $params->{"request_timeout"} // 0
delete $params->{"request_timeout"}
Truth table100%2/2
A
A= delete $params->{"request_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2477 | 653 | ▶ | $self->{ws_idle_timeout} = delete $params->{ws_idle_timeout} // 0; # WebSocket idle timeout (0 = disabled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"ws_idle_timeout"} // 0
MC/DC100%1/1
delete $params->{"ws_idle_timeout"} // 0
delete $params->{"ws_idle_timeout"}
Truth table100%2/2
A
A= delete $params->{"ws_idle_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2478 | 653 | ▶ | $self->{sse_idle_timeout} = delete $params->{sse_idle_timeout} // 0; # SSE idle timeout (0 = disabled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"sse_idle_timeout"} // 0
MC/DC100%1/1
delete $params->{"sse_idle_timeout"} // 0
delete $params->{"sse_idle_timeout"}
Truth table100%2/2
A
A= delete $params->{"sse_idle_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2479 | 653 | ▶ | $self->{heartbeat_timeout} = delete $params->{heartbeat_timeout} // 50; # Worker heartbeat timeout (0 = disabled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"heartbeat_timeout"} // 50
MC/DC100%1/1
delete $params->{"heartbeat_timeout"} // 50
delete $params->{"heartbeat_timeout"}
Truth table100%2/2
A
A= delete $params->{"heartbeat_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2480 | 653 | ▶ | $self->{lifespan_startup_timeout} = delete $params->{lifespan_startup_timeout} // 30; # Max wait for lifespan startup signal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"lifespan_startup_timeout"} // 30
MC/DC100%1/1
delete $params->{"lifespan_startup_timeout"} // 30
delete $params->{"lifespan_startup_timeout"}
Truth table100%2/2
A
A= delete $params->{"lifespan_startup_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2481 | die "Invalid lifespan_startup_timeout '0' - the PAGI Lifespan spec requires that a server must not block startup indefinitely waiting for a lifespan signal\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2482 | 653 | ▶ | if $self->{lifespan_startup_timeout} == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2483 | 652 | ▶ | $self->{lifespan_mode} = delete $params->{lifespan_mode} // 'auto'; # auto (default) | on (decline is fatal) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"lifespan_mode"} // "auto"
MC/DC100%1/1
delete $params->{"lifespan_mode"} // "auto"
delete $params->{"lifespan_mode"}
Truth table100%2/2
A
A= delete $params->{"lifespan_mode"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2484 | die "Invalid lifespan_mode '$self->{lifespan_mode}' - must be 'auto' or 'on' ('off' is nonconforming: the PAGI Lifespan spec forbids skipping the protocol)\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2485 | 652 | ▶ | unless $self->{lifespan_mode} =~ /\A(?:auto|on)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2486 | 649 | ▶ | $self->{write_high_watermark} = delete $params->{write_high_watermark} // 65536; # 64KB - pause sending above this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"write_high_watermark"} // 65536
MC/DC100%1/1
delete $params->{"write_high_watermark"} // 65536
delete $params->{"write_high_watermark"}
Truth table100%2/2
A
A= delete $params->{"write_high_watermark"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2487 | 649 | ▶ | $self->{write_low_watermark} = delete $params->{write_low_watermark} // 16384; # 16KB - resume sending below this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"write_low_watermark"} // 16384
MC/DC100%1/1
delete $params->{"write_low_watermark"} // 16384
delete $params->{"write_low_watermark"}
Truth table100%2/2
A
A= delete $params->{"write_low_watermark"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2488 | 649 | $self->{loop_type} = delete $params->{loop_type}; # Optional loop backend (EPoll, EV, Poll, etc.) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2489 | 649 | ▶ | if (my $lt = $self->{loop_type}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2490 | 12 | ▶ | die "Invalid loop_type '$lt': must contain only letters, digits, and ::\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2491 | unless $lt =~ /\A[A-Za-z][A-Za-z0-9_]*(?:::[A-Za-z][A-Za-z0-9_]*)*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2493 | # Deprecated: core event validation is mandatory; this flag is retained for compatibility and controls nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2494 | $self->{validate_events} = delete $params->{validate_events} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2495 | 647 | ▶ | // (($ENV{PAGI_ENV} // '') eq 'development' ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
delete $params->{"validate_events"} // (($ENV{"PAGI_ENV"} // "") eq "development" ? 1 : 0)
$ENV{"PAGI_ENV"} // ""
MC/DC100%1/1
delete $params->{"validate_events"} // (($ENV{"PAGI_ENV"} // "") eq "development" ? 1 : 0)
delete $params->{"validate_events"}
Truth table100%2/2
A
A= delete $params->{"validate_events"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2497 | # HTTP/2 support (opt-in, experimental) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2498 | 647 | ▶ | $self->{http2} = delete $params->{http2} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"http2"} // 0
MC/DC100%1/1
delete $params->{"http2"} // 0
delete $params->{"http2"}
Truth table100%2/2
A
A= delete $params->{"http2"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2499 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2500 | # HTTP/2 protocol settings (only used when http2 is enabled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2501 | 647 | ▶ | my $h2_max_concurrent_streams = delete $params->{h2_max_concurrent_streams} // 100; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"h2_max_concurrent_streams"} // 100
MC/DC100%1/1
delete $params->{"h2_max_concurrent_streams"} // 100
delete $params->{"h2_max_concurrent_streams"}
Truth table100%2/2
A
A= delete $params->{"h2_max_concurrent_streams"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2502 | 647 | ▶ | my $h2_initial_window_size = delete $params->{h2_initial_window_size} // 65535; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"h2_initial_window_size"} // 65535
MC/DC100%1/1
delete $params->{"h2_initial_window_size"} // 65535
delete $params->{"h2_initial_window_size"}
Truth table100%2/2
A
A= delete $params->{"h2_initial_window_size"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2503 | 647 | ▶ | my $h2_max_frame_size = delete $params->{h2_max_frame_size} // 16384; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"h2_max_frame_size"} // 16384
MC/DC100%1/1
delete $params->{"h2_max_frame_size"} // 16384
delete $params->{"h2_max_frame_size"}
Truth table100%2/2
A
A= delete $params->{"h2_max_frame_size"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2504 | 647 | ▶ | my $h2_enable_push = delete $params->{h2_enable_push} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"h2_enable_push"} // 0
MC/DC100%1/1
delete $params->{"h2_enable_push"} // 0
delete $params->{"h2_enable_push"}
Truth table100%2/2
A
A= delete $params->{"h2_enable_push"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2505 | 647 | ▶ | my $h2_enable_connect_protocol = delete $params->{h2_enable_connect_protocol} // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"h2_enable_connect_protocol"} // 1
MC/DC100%1/1
delete $params->{"h2_enable_connect_protocol"} // 1
delete $params->{"h2_enable_connect_protocol"}
Truth table100%2/2
A
A= delete $params->{"h2_enable_connect_protocol"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2506 | 647 | ▶ | my $h2_max_header_list_size = delete $params->{h2_max_header_list_size} // 65536; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $params->{"h2_max_header_list_size"} // 65536
MC/DC100%1/1
delete $params->{"h2_max_header_list_size"} // 65536
delete $params->{"h2_max_header_list_size"}
Truth table100%2/2
A
A= delete $params->{"h2_max_header_list_size"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2507 | 647 | ▶ | my $h2_rst_rate_limit = delete $params->{h2_rst_rate_limit} // { burst => 1000, rate => 33 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $params->{"h2_rst_rate_limit"} // {"burst", 1000, "rate", 33}
MC/DC0%0/1
delete $params->{"h2_rst_rate_limit"} // {"burst", 1000, "rate", 33}
delete $params->{"h2_rst_rate_limit"}
Truth table50%1/2
A
A= delete $params->{"h2_rst_rate_limit"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2508 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2509 | 647 | $self->{running} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2510 | 647 | $self->{bound_port} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2511 | 647 | $self->{listener} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2512 | 647 | $self->{connections} = {}; # Hash keyed by refaddr for O(1) add/remove | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2513 | $self->{protocol} = PAGI::Server::Protocol::HTTP1->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2514 | max_header_size => $self->{max_header_size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2515 | max_header_count => $self->{max_header_count}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2516 | 647 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2517 | 647 | $self->{state} = {}; # Shared state from lifespan | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2518 | 647 | $self->{worker_pids} = {}; # Track worker PIDs in multi-worker mode | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2519 | 647 | $self->{is_worker} = 0; # True if this is a worker process | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2520 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2521 | # Initialize HTTP/2 protocol handler if enabled and available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2522 | 647 | ▶ | if ($self->{http2}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2523 | 227 | ▶ | if ($HTTP2_AVAILABLE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2524 | 227 | $self->{http2_protocol} = PAGI::Server::Protocol::HTTP2->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2525 | max_concurrent_streams => $h2_max_concurrent_streams, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2526 | initial_window_size => $h2_initial_window_size, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2527 | max_frame_size => $h2_max_frame_size, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2528 | enable_push => $h2_enable_push, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2529 | enable_connect_protocol => $h2_enable_connect_protocol, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2530 | max_header_list_size => $h2_max_header_list_size, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2531 | h2_rst_rate_limit => $h2_rst_rate_limit, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2532 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2533 | 227 | $self->{http2_enabled} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2534 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2535 | # h2c mode: HTTP/2 over cleartext (no TLS) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2536 | 227 | ▶ | if (!$self->{ssl}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2537 | 220 | $self->{h2c_enabled} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2538 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2539 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2540 | 0 | die <<"END_HTTP2_ERROR"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2541 | HTTP/2 support requested but Net::HTTP2::nghttp2 is not installed, or is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2542 | 0 | older than @{[ PAGI::Server::Protocol::HTTP2::MIN_NGHTTP2_VERSION() ]}. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2544 | To install: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2545 | cpanm Net::HTTP2::nghttp2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2546 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2547 | Or disable HTTP/2: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2548 | http2 => 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2549 | END_HTTP2_ERROR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2550 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2551 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2552 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2553 | 647 | $self->SUPER::_init($params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2554 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2556 | 651 | ▶ | sub configure { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
configure: called CC=38 SCAR=53.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2557 | 651 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2558 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2559 | 651 | ▶ | if (exists $params{app}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2560 | 1 | my $app = delete $params{app}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2561 | 1 | $self->{app} = PAGI::Server::AppNormalizer::normalize_app($app, 'app'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2562 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2563 | 651 | ▶ | if (exists $params{host}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2564 | 0 | $self->{host} = delete $params{host}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2565 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2566 | 651 | ▶ | if (exists $params{port}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2567 | 0 | $self->{port} = delete $params{port}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2568 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2569 | 651 | ▶ | if (exists $params{socket}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2570 | 0 | delete $params{socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2571 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2572 | 651 | ▶ | if (exists $params{socket_mode}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2573 | 0 | delete $params{socket_mode}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2574 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2575 | 651 | ▶ | if (exists $params{listen}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2576 | 0 | delete $params{listen}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2577 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2578 | 651 | ▶ | if (exists $params{ssl}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2579 | 0 | $self->{ssl} = delete $params{ssl}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2580 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2581 | 651 | ▶ | if (exists $params{extensions}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2582 | 0 | $self->{extensions} = delete $params{extensions}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2583 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2584 | 651 | ▶ | if (exists $params{access_log}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2585 | 0 | $self->{access_log} = delete $params{access_log}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2586 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2587 | 651 | ▶ | if (exists $params{access_log_format}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2588 | 1 | $self->{access_log_format} = delete $params{access_log_format}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2589 | $self->{_access_log_formatter} = $self->_compile_access_log_format( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2590 | $self->{access_log_format} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2591 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2592 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2593 | 651 | ▶ | if (exists $params{logger}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2594 | 0 | my $logger = delete $params{logger}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2595 | 0 | ▶ | die "Invalid logger - must be a coderef taking one hashref\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $logger and ref $logger ne "CODE"
MC/DC0%0/2
defined $logger and ref $logger ne "CODE"
defined $logger
ref $logger ne "CODE"
Truth table0%0/3
A and B
A= defined $loggerB= ref $logger ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2596 | if defined $logger && ref($logger) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2597 | 0 | $self->{logger} = $logger; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2598 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2599 | 651 | ▶ | if (exists $params{quiet}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2600 | # Deprecated spelling of log_level => 'error'; ignored when log_level | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2601 | # is supplied in the same call, which is the control that wins. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2602 | 0 | my $quiet = delete $params{quiet}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2603 | 0 | ▶ | if ($quiet && !exists $params{log_level}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$quiet and not exists $params{"log_level"}
MC/DC0%0/2
$quiet and not exists $params{"log_level"}
$quiet
not exists $params{"log_level"}
Truth table0%0/3
A and not(B)
A= $quietB= not exists $params{"log_level"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2604 | 0 | $params{log_level} = 'error'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2605 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2606 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2607 | 651 | ▶ | if (exists $params{log_level}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2608 | 0 | my $level = delete $params{log_level}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2609 | 0 | my %valid_levels = (debug => 1, info => 2, warn => 3, error => 4); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2610 | die "Invalid log_level '$level' - must be one of: debug, info, warn, error\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2611 | 0 | ▶ | unless $valid_levels{$level}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2612 | 0 | $self->{log_level} = $level; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2613 | 0 | $self->{_log_level_num} = $valid_levels{$level}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2614 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2615 | 651 | ▶ | if (exists $params{timeout}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2616 | 0 | $self->{timeout} = delete $params{timeout}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2617 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2618 | 651 | ▶ | if (exists $params{max_header_size}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2619 | 0 | $self->{max_header_size} = delete $params{max_header_size}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2620 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2621 | 651 | ▶ | if (exists $params{max_header_count}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2622 | 0 | $self->{max_header_count} = delete $params{max_header_count}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2623 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2624 | 651 | ▶ | if (exists $params{max_body_size}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2625 | 0 | $self->{max_body_size} = delete $params{max_body_size}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2626 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2627 | 651 | ▶ | if (exists $params{workers}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2628 | 0 | $self->{workers} = delete $params{workers}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2629 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2630 | 651 | ▶ | if (exists $params{max_requests}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2631 | 0 | $self->{max_requests} = delete $params{max_requests}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2632 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2633 | 651 | ▶ | if (exists $params{listener_backlog}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2634 | 0 | $self->{listener_backlog} = delete $params{listener_backlog}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2635 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2636 | 651 | ▶ | if (exists $params{shutdown_timeout}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2637 | 0 | $self->{shutdown_timeout} = delete $params{shutdown_timeout}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2638 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2639 | 651 | ▶ | if (exists $params{lifespan_startup_timeout}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2640 | 1 | my $timeout = delete $params{lifespan_startup_timeout}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2641 | 1 | ▶ | die "Invalid lifespan_startup_timeout '0' - the PAGI Lifespan spec requires that a server must not block startup indefinitely waiting for a lifespan signal\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2642 | if $timeout == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2643 | 0 | $self->{lifespan_startup_timeout} = $timeout; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2644 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2645 | 650 | ▶ | if (exists $params{lifespan_mode}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2646 | 1 | my $mode = delete $params{lifespan_mode}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2647 | 1 | ▶ | die "Invalid lifespan_mode '$mode' - must be 'auto' or 'on' ('off' is nonconforming: the PAGI Lifespan spec forbids skipping the protocol)\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2648 | unless $mode =~ /\A(?:auto|on)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2649 | 0 | $self->{lifespan_mode} = $mode; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2650 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2651 | 649 | ▶ | if (exists $params{max_receive_queue}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2652 | 0 | $self->{max_receive_queue} = delete $params{max_receive_queue}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2653 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2654 | 649 | ▶ | if (exists $params{max_ws_frame_size}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2655 | 0 | $self->{max_ws_frame_size} = delete $params{max_ws_frame_size}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2656 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2657 | 649 | ▶ | if (exists $params{max_connections}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2658 | 0 | $self->{max_connections} = delete $params{max_connections}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2659 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2660 | 649 | ▶ | if (exists $params{request_timeout}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2661 | 0 | $self->{request_timeout} = delete $params{request_timeout}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2662 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2663 | 649 | ▶ | if (exists $params{ws_idle_timeout}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2664 | 0 | $self->{ws_idle_timeout} = delete $params{ws_idle_timeout}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2665 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2666 | 649 | ▶ | if (exists $params{sse_idle_timeout}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2667 | 0 | $self->{sse_idle_timeout} = delete $params{sse_idle_timeout}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2668 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2669 | 649 | ▶ | if (exists $params{http2}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2670 | 0 | $self->{http2} = delete $params{http2}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2671 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2672 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2673 | 649 | $self->SUPER::configure(%params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2674 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2675 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2676 | # Log levels: debug=1, info=2, warn=3, error=4, fatal=5. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2677 | # fatal is sortable but is not a valid log_level: a fatal threshold would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2678 | # silence errors. It exists so the five levels PSGI and Rack converged on can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2679 | # be handed to a sink unchanged. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2680 | 153 | my %_LOG_LEVELS = (debug => 1, info => 2, warn => 3, error => 4, fatal => 5); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2681 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2682 | # Emits exactly what the server emitted before the destination was replaceable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2683 | 146 | ▶ | my $_DEFAULT_LOGGER = sub { warn "$_[0]{message}\n" }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2684 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2685 | 1188 | ▶ | sub _log { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2686 | 1188 | my ($self, $level, $msg, $category) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2687 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2688 | 1188 | ▶ | my $level_num = $_LOG_LEVELS{$level} // 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_LOG_LEVELS{$level} // 2
MC/DC0%0/1
$_LOG_LEVELS{$level} // 2
$_LOG_LEVELS{$level}
Truth table50%1/2
A
A= $_LOG_LEVELS{$level}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2689 | 1188 | ▶ | return if $level_num < $self->{_log_level_num}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2690 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2691 | # The payload promises a message with no trailing newline, but most of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2692 | # these interpolate $@ or $! from an ordinary die "...\n". Make the promise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2693 | # true here rather than asking every sink author to chomp. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2694 | 155 | $msg =~ s/\n+\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2695 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2696 | # Workers and the master share one destination, so a worker's messages say | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2697 | # which process they came from. An unprefixed line is the master's. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2698 | 155 | ▶ | $msg = "Worker $self->{worker_num} ($$): $msg" if $self->{is_worker}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2699 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2700 | 155 | ▶ | ($self->{logger} // $_DEFAULT_LOGGER)->({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
$category // "PAGI::Server"
$self->{"logger"} // $_DEFAULT_LOGGER
MC/DC100%1/1
$category // "PAGI::Server"
$category
MC/DC0%0/2
$self->{"logger"} // $_DEFAULT_LOGGER
$self->{"logger"}
$_DEFAULT_LOGGER
Truth table100%2/2
A
A= $category
Truth table66%2/3
A // B
A= $self->{"logger"}B= $_DEFAULT_LOGGER
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2701 | level => $level, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2702 | message => $msg, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2703 | # Classes that log through the server name themselves, so a sink can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2704 | # separate connection noise from lifecycle events. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2705 | category => $category // 'PAGI::Server', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2706 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2707 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2708 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2709 | # The spec distribution's version, or undef. PAGI::Server has no runtime | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2710 | # dependency on PAGI, so the banner names it only when it is actually there. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2711 | # Kept separate so a test can make the spec appear absent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2712 | 0 | ▶ | sub _pagi_spec_version { return eval { require PAGI; PAGI->VERSION } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pagi_spec_version: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2713 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2714 | # A fact worth showing in the startup block rather than on a ragged line of its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2715 | # own. Workers never reach the banner -- it is emitted from _create_loop, which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2716 | # the worker path does not call -- so they report immediately instead, keeping | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2717 | # their Worker N prefix. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2718 | 119 | ▶ | sub _startup_note { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_startup_note: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2719 | 119 | my ($self, $label, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2720 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2721 | # The label carries the subject inside the block ("lifespan not supported"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2722 | # on its own line it has to be restored, or the message loses what it is about. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2723 | 119 | ▶ | return $self->_log(info => "$label $text") if $self->{is_worker}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2724 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2725 | 119 | ▶ | push @{ $self->{_startup_notes} ||= [] }, [$label, $text]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_startup_notes"} ||= []
MC/DC0%0/1
$self->{"_startup_notes"} ||= []
$self->{"_startup_notes"}
Truth table50%1/2
A
A= $self->{"_startup_notes"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2726 | 119 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2727 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2728 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2729 | # The startup block: what is running and where, then everything worth knowing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2730 | # about it, aligned. One builder for all four listen shapes, which is what stops | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2731 | # them drifting apart. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2732 | 317 | ▶ | sub _startup_banner { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_startup_banner: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2733 | 317 | my ($self, $where, $per_worker) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2734 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2735 | 317 | ▶ | my $identity = 'PAGI::Server ' . (__PACKAGE__->VERSION // 'unknown'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
__PACKAGE__->VERSION // "unknown"
MC/DC0%0/1
__PACKAGE__->VERSION // "unknown"
__PACKAGE__->VERSION
Truth table50%1/2
A
A= __PACKAGE__->VERSION
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2736 | 317 | ▶ | if (defined(my $spec = $self->_pagi_spec_version)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2737 | 0 | $identity .= " (PAGI $spec)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2738 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2739 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2740 | 317 | my $loop_class = ref($self->loop); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2741 | 317 | $loop_class =~ s/^IO::Async::Loop:://; # Shorten for display | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2742 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2743 | 317 | my $max_conn = $self->effective_max_connections; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2744 | 317 | ▶ | $max_conn .= '/worker' if $per_worker; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2745 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2746 | my @notes = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2747 | 317 | ▶ | @{ $self->{startup_notes} || [] }, # contributed by the runner | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'startup_notes'} || []
MC/DC0%0/1
$self->{'startup_notes'} || []
$self->{'startup_notes'}
Truth table50%1/2
A
A= $self->{'startup_notes'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2748 | 317 | ▶ | @{ $self->{_startup_notes} || [] }, # gathered during our own startup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'_startup_notes'} || []
MC/DC100%1/1
$self->{'_startup_notes'} || []
$self->{'_startup_notes'}
Truth table100%2/2
A
A= $self->{'_startup_notes'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2749 | ['loop', sprintf('%s, max_conn %s, http2 %s, tls %s, future_xs %s', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2750 | $loop_class, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2751 | $max_conn, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2752 | $self->_http2_status_string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2753 | $self->_tls_status_string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2754 | $self->_future_xs_status_string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2755 | )], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2756 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2757 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2758 | 317 | my $width = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2759 | 317 | ▶ | for (@notes) { $width = length $_->[0] if length $_->[0] > $width } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2760 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2761 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2762 | "$identity listening on $where", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2763 | 317 | map { sprintf(' %-*s %s', $width, $_->[0], $_->[1]) } @notes, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2764 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2765 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2766 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2767 | # Returns a human-readable TLS status string for the startup banner | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2768 | 317 | ▶ | sub _tls_status_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tls_status_string: called CC=5 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2769 | 317 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2770 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2771 | 317 | ▶ | if ($self->{disable_tls}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2772 | 0 | ▶ | return $TLS_AVAILABLE ? 'disabled' : 'n/a (disabled)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2773 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2774 | 317 | ▶ | if ($self->{tls_enabled}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2775 | 40 | return 'on'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2776 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2777 | 277 | ▶ | return $TLS_AVAILABLE ? 'available' : 'not installed'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2778 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2779 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2780 | # Returns a human-readable HTTP/2 status string for the startup banner | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2781 | 317 | ▶ | sub _http2_status_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_http2_status_string: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2782 | 317 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2783 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2784 | 317 | ▶ | if ($self->{http2_enabled}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2785 | 3 | ▶ | return $self->{h2c_enabled} ? 'on (h2c)' : 'on'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2786 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2787 | 314 | ▶ | return $HTTP2_AVAILABLE ? 'available' : 'not installed'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2788 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2789 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2790 | # Returns a human-readable Future::XS status string for the startup banner | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2791 | 317 | ▶ | sub _future_xs_status_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_future_xs_status_string: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2792 | 317 | ▶ | return 'on' if $FUTURE_XS_ENABLED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2793 | 317 | ▶ | return 'available' if $FUTURE_XS_AVAILABLE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2794 | 317 | return 'not installed'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2795 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2796 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2797 | # Check if TLS modules are available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2798 | 50 | ▶ | sub _check_tls_available { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_tls_available: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2799 | 50 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2800 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2801 | # Allow forcing TLS off for testing — return false to skip TLS setup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2802 | 50 | ▶ | if ($self->{disable_tls}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2803 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2804 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2805 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2806 | 50 | ▶ | return 1 if $TLS_AVAILABLE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2807 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2808 | 0 | die <<"END_TLS_ERROR"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2809 | TLS support requested but required modules not installed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2810 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2811 | To enable HTTPS/TLS support, install: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2812 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2813 | cpanm IO::Async::SSL IO::Socket::SSL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2814 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2815 | Or on Debian/Ubuntu: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2816 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2817 | apt-get install libio-socket-ssl-perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2818 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2819 | Then restart your application. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2820 | END_TLS_ERROR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2821 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2822 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2823 | # Build SSL configuration parameters for use by both single-worker and multi-worker modes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2824 | # Returns a hashref of SSL params (including SSL_reuse_ctx) or undef if no SSL configured. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2825 | 296 | ▶ | sub _build_ssl_config { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_ssl_config: called CC=15 SCAR=27.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2826 | 296 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2827 | 296 | ▶ | my $ssl = $self->{ssl} or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2828 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2829 | 44 | ▶ | return unless $self->_check_tls_available; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2830 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2831 | 44 | my %ssl_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2832 | 44 | $ssl_params{SSL_server} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2833 | 44 | ▶ | $ssl_params{SSL_cert_file} = $ssl->{cert_file} if $ssl->{cert_file}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2834 | 44 | ▶ | $ssl_params{SSL_key_file} = $ssl->{key_file} if $ssl->{key_file}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2835 | # SSL_version as a true floor: negotiate the highest version the client also | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2836 | # supports at or above min_version, so TLS 1.3 is offered. IO::Socket::SSL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2837 | # pins to an exact version for a bare 'TLSv1_2' (the trailing ':' does NOT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2838 | # mean "or higher"), so instead start from auto-negotiation ('SSLv23') and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2839 | # exclude every protocol below the floor. A min_version outside the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2840 | # documented TLSv1_2/TLSv1_3 set is passed through unchanged. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2841 | 44 | my @tls_order = qw(SSLv2 SSLv3 TLSv1 TLSv1_1 TLSv1_2 TLSv1_3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2842 | 44 | ▶ | my $min_version = $ssl->{min_version} // 'TLSv1_2'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ssl->{"min_version"} // "TLSv1_2"
MC/DC100%1/1
$ssl->{"min_version"} // "TLSv1_2"
$ssl->{"min_version"}
Truth table100%2/2
A
A= $ssl->{"min_version"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2843 | 44 | ▶ | if (grep { $_ eq $min_version } @tls_order) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2844 | 44 | my @below_floor; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2845 | 44 | for my $v (@tls_order) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2846 | 221 | ▶ | last if $v eq $min_version; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2847 | 177 | push @below_floor, "!$v"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2848 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2849 | 44 | $ssl_params{SSL_version} = join(':', 'SSLv23', @below_floor); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2850 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2851 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2852 | 0 | $ssl_params{SSL_version} = $min_version; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2853 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2854 | $ssl_params{SSL_cipher_list} = $ssl->{cipher_list} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2855 | 44 | ▶ | // 'ECDHE+AESGCM:DHE+AESGCM:ECDHE+CHACHA20:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ssl->{"cipher_list"} // "ECDHE+AESGCM:DHE+AESGCM:ECDHE+CHACHA20:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
MC/DC100%1/1
$ssl->{"cipher_list"} // "ECDHE+AESGCM:DHE+AESGCM:ECDHE+CHACHA20:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
$ssl->{"cipher_list"}
Truth table100%2/2
A
A= $ssl->{"cipher_list"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2856 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2857 | 44 | ▶ | if ($ssl->{verify_client}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2858 | # SSL_VERIFY_PEER (0x01) | SSL_VERIFY_FAIL_IF_NO_PEER_CERT (0x02) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2859 | 8 | $ssl_params{SSL_verify_mode} = 0x03; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2860 | 8 | ▶ | $ssl_params{SSL_ca_file} = $ssl->{ca_file} if $ssl->{ca_file}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2861 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2862 | 36 | $ssl_params{SSL_verify_mode} = 0x00; # SSL_VERIFY_NONE | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2863 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2864 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2865 | # ALPN negotiation for HTTP/2 support | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2866 | 44 | ▶ | if ($self->{http2} && $HTTP2_AVAILABLE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"http2"} and $PAGI::Server::HTTP2_AVAILABLE
MC/DC50%1/2
$self->{"http2"} and $PAGI::Server::HTTP2_AVAILABLE
$self->{"http2"}
$PAGI::Server::HTTP2_AVAILABLE
Truth table66%2/3
A and B
A= $self->{"http2"}B= $PAGI::Server::HTTP2_AVAILABLE
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2867 | 3 | $ssl_params{SSL_alpn_protocols} = ['h2', 'http/1.1']; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2868 | 3 | $self->{http2_enabled} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2869 | 3 | $self->{h2c_enabled} = 0; # TLS mode, not cleartext | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2870 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2871 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2872 | # Pre-create shared SSL context to avoid per-connection CA bundle parsing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2873 | 44 | my $ssl_ctx = IO::Socket::SSL::SSL_Context->new(\%ssl_params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2874 | 44 | $self->{_ssl_ctx} = $ssl_ctx; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2875 | 44 | $ssl_params{SSL_reuse_ctx} = $ssl_ctx; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2876 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2877 | # Mark TLS enabled and auto-add tls extension | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2878 | 44 | $self->{tls_enabled} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2879 | 44 | ▶ | $self->{extensions}{tls} = {} unless exists $self->{extensions}{tls}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2880 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2881 | 44 | return \%ssl_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2882 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2883 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2884 | =head2 run | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2885 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2886 | $server->run; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2887 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2888 | Standalone entry point that creates an event loop, starts the server, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2889 | and runs until shutdown. This is the simplest way to run a PAGI server: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2890 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2891 | my $server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2892 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2893 | port => 8080, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2894 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2895 | $server->run; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2896 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2897 | For embedding in an existing IO::Async application, use the traditional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2898 | pattern instead: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2899 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2900 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2901 | $loop->add($server); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2902 | $server->listen->get; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2903 | $loop->run; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2904 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2905 | The C<run()> method handles: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2906 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2907 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2908 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2909 | =item * Creating the event loop (respecting C<loop_type> if set) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2910 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2911 | =item * Adding the server to the loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2912 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2913 | =item * Starting the listener | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2914 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2915 | =item * Setting up signal handlers for graceful shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2916 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2917 | =item * Running the event loop until shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2918 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2919 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2920 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2921 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2922 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2923 | 3 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=5 SCAR=21.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2924 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2925 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2926 | 3 | my $loop = $self->_create_loop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2927 | 3 | $loop->add($self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2928 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2929 | # Start listening with error handling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2930 | 3 | eval { $self->listen->get }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2931 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2932 | 0 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2933 | 0 | my $port = $self->{port}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2934 | 0 | ▶ | if ($error =~ /Address already in use/i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2935 | 0 | die "Error: Port $port is already in use\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2936 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2937 | elsif ($error =~ /Permission denied/i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2938 | 0 | die "Error: Permission denied to bind to port $port\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2939 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2940 | 0 | die "Error starting server: $error\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2941 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2942 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2943 | # Run the event loop (signal handlers were set up by listen()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2944 | 1 | $loop->run; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2945 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2946 | # If the loop stopped because every worker failed to start (multi-worker), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2947 | # exit non-zero so a process supervisor restarts us rather than seeing a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2948 | # clean exit from a server that never served anything. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2949 | 1 | ▶ | if ($self->{_workers_failed}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2950 | 1 | die "All workers failed to start; no workers remain\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2951 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2952 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2953 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2954 | # Create an event loop, respecting loop_type config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2955 | 19 | ▶ | sub _create_loop { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_loop: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2956 | 19 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2957 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2958 | 19 | ▶ | if (my $loop_type = $self->{loop_type}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2959 | 2 | ▶ | die "Invalid loop_type '$loop_type': must contain only letters, digits, and ::\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2960 | unless $loop_type =~ /\A[A-Za-z][A-Za-z0-9_]*(?:::[A-Za-z][A-Za-z0-9_]*)*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2961 | 2 | my $loop_class = "IO::Async::Loop::$loop_type"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2962 | 2 | (my $loop_file = "$loop_class.pm") =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2963 | 2 | ▶ | eval { require $loop_file } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2964 | or die "Cannot load loop backend '$loop_type': $@\n" . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2965 | "Install it with: cpanm $loop_class\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2966 | 2 | return $loop_class->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2967 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2968 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2969 | 17 | require IO::Async::Loop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2970 | 17 | return IO::Async::Loop->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2971 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2972 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2973 | # Listener fds this process's live PAGI::Server instances have published in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2974 | # PAGI_REUSE. The env var is ambient process state meant for the exec-based | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2975 | # hot-restart handoff; without this registry, a SECOND server constructed in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2976 | # the same process would collect a sibling's live listener as "inherited" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2977 | # and reap it as unmatched -- closing it out from under both the sibling and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2978 | # the event loop (IO::Async::Loop::Epoll croaks on the ensuing fd reuse). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2979 | # Across exec the interpreter restarts and the registry is empty, so real | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2980 | # hot-restart inheritance is unaffected; forked workers inherit the copy and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2981 | # correctly leave the master's fds alone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2982 | 153 | my %OWNED_REUSE_FDS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2983 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2984 | 311 | ▶ | async sub listen { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
listen: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2985 | 311 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2986 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2987 | 311 | ▶ | return if $self->{running}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2988 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2989 | # Multi-worker mode uses a completely different code path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2990 | 311 | ▶ | if ($self->{workers} && $self->{workers} > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"workers"} and $self->{"workers"} > 0
MC/DC50%1/2
$self->{"workers"} and $self->{"workers"} > 0
$self->{"workers"}
$self->{"workers"} > 0
Truth table66%2/3
A and B
A= $self->{"workers"}B= $self->{"workers"} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2991 | 27 | return $self->_listen_multiworker; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2992 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2993 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2994 | 284 | return await $self->_listen_singleworker; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2995 | 153 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2996 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2997 | # Single-worker mode - uses IO::Async normally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2998 | 284 | ▶ | async sub _listen_singleworker { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_listen_singleworker: called CC=42 SCAR=43.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2999 | 284 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3000 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3001 | 284 | weaken(my $weak_self = $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3002 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3003 | # Run lifespan startup before accepting connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3004 | 284 | my $startup_result = await $self->_run_lifespan_startup; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3005 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3006 | 284 | ▶ | if (!$startup_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3007 | 4 | ▶ | my $message = $startup_result->{message} // 'Lifespan startup failed'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$startup_result->{"message"} // "Lifespan startup failed"
MC/DC0%0/1
$startup_result->{"message"} // "Lifespan startup failed"
$startup_result->{"message"}
Truth table50%1/2
A
A= $startup_result->{"message"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3008 | 4 | $self->_log(error => "PAGI Server startup failed: $message"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3009 | 4 | die "Lifespan startup failed: $message\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3010 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3011 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3012 | # Collect any inherited fds (from PAGI_REUSE or LISTEN_FDS) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3013 | 280 | my $inherited = $self->_collect_inherited_fds; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3014 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3015 | # Iterate over listeners array, creating one IO::Async::Listener per spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3016 | 280 | my @listen_entries; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3017 | 280 | for my $spec (@{$self->{listeners}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3018 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3019 | # Check for inherited fd matching this spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3020 | 283 | ▶ | my $match_key = $spec->{type} eq 'unix' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3021 | ? "unix:$spec->{path}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3022 | : "$spec->{host}:$spec->{port}"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3023 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3024 | 283 | ▶ | if (my $inh = delete $inherited->{$match_key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3025 | # Reuse inherited fd — skip bind/listen entirely | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3026 | 3 | $spec->{_inherited} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3027 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3028 | 3 | my $handle = $inh->{handle}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3029 | 3 | ▶ | if (!$handle) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3030 | 2 | ▶ | my $class = $inh->{type} eq 'unix' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3031 | ? 'IO::Socket::UNIX' : 'IO::Socket::INET'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3032 | 2 | ▶ | require IO::Socket::UNIX if $inh->{type} eq 'unix'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3033 | 2 | ▶ | $handle = $class->new_from_fd($inh->{fd}, 'r') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3034 | or die "Cannot open inherited fd $inh->{fd}: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3035 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3036 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3037 | 3 | ▶ | if ($inh->{type} eq 'tcp' && $handle->can('sockport')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$inh->{"type"} eq "tcp" and $handle->can("sockport")
MC/DC100%2/2
$inh->{"type"} eq "tcp" and $handle->can("sockport")
$inh->{"type"} eq "tcp"
$handle->can("sockport")
Truth table100%3/3
A and B
A= $inh->{"type"} eq "tcp"B= $handle->can("sockport")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3038 | 1 | $spec->{port} = $handle->sockport; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3039 | 1 | ▶ | $self->{bound_port} //= $spec->{port}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"bound_port"} //= $spec->{"port"}
MC/DC0%0/2
$self->{"bound_port"} //= $spec->{"port"}
$self->{"bound_port"}
$spec->{"port"}
Truth table33%1/3
A //= B
A= $self->{"bound_port"}B= $spec->{"port"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3040 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3041 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3042 | 3 | my $spec_ref = $spec; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3043 | 3 | weaken(my $weak_inner = $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3044 | my $listener = IO::Async::Listener->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3045 | handle => $handle, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3046 | 2 | ▶ | on_stream => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3047 | 2 | my ($l, $stream) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3048 | 2 | ▶ | return unless $weak_inner; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3049 | 2 | $weak_inner->_on_connection($stream, $spec_ref); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3050 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3051 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3052 | 3 | $self->add_child($listener); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3053 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3054 | 3 | $self->_log(info => "Reusing inherited fd $inh->{fd} for $match_key" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3055 | . " (source: $inh->{source})"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3056 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3057 | 3 | push @listen_entries, { listener => $listener, spec => $spec }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3058 | 3 | next; # Skip normal bind/listen | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3059 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3060 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3061 | 280 | my $spec_copy = $spec; # capture for closure | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3062 | my $listener = IO::Async::Listener->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3063 | 295 | ▶ | on_stream => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3064 | 295 | my ($listener, $stream) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3065 | 295 | ▶ | return unless $weak_self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3066 | 295 | $weak_self->_on_connection($stream, $spec_copy); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3067 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3068 | 280 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3069 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3070 | 280 | $self->add_child($listener); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3071 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3072 | # Build listener options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3073 | my %listen_opts = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3074 | queuesize => $self->{listener_backlog}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3075 | 280 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3076 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3077 | 280 | ▶ | if ($spec->{type} eq 'unix') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3078 | # Remove stale socket file if it exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3079 | 10 | ▶ | unlink $spec->{path} if -e $spec->{path}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3080 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3081 | $listen_opts{addr} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3082 | family => 'unix', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3083 | socktype => 'stream', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3084 | path => $spec->{path}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3085 | 10 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3086 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3087 | 10 | ▶ | if ($self->{tls_enabled}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3088 | 0 | $self->_log(info => "Note: TLS is configured but does not apply to Unix socket $spec->{path}"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3089 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3090 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3091 | # TCP listener | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3092 | $listen_opts{addr} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3093 | family => 'inet', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3094 | socktype => 'stream', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3095 | ip => $spec->{host}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3096 | port => $spec->{port}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3097 | 270 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3098 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3099 | # Add SSL options if configured (TCP only) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3100 | 270 | ▶ | if (my $ssl_params = $self->_build_ssl_config) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3101 | 34 | $listen_opts{extensions} = ['SSL']; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3102 | 34 | %listen_opts = (%listen_opts, %$ssl_params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3104 | 4 | ▶ | $listen_opts{on_ssl_error} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3105 | 4 | ▶ | return unless $weak_self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3106 | 4 | $weak_self->_log(debug => "SSL handshake failed: $_[0]"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3107 | 34 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3111 | # Set restrictive umask for Unix socket bind to prevent brief | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3112 | # permission window (CVE-2023-45145 pattern in Redis) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3113 | 280 | my $old_umask; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3114 | 280 | ▶ | if ($spec->{type} eq 'unix') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3115 | 10 | $old_umask = umask(0177); # Owner-only until chmod | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3118 | # Start listening ($^F raised so fd survives exec for hot restart) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3119 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3120 | 280 | local $^F = 1023; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3121 | 280 | await $listener->listen(%listen_opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3124 | # Restore umask after bind | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3125 | 280 | ▶ | umask($old_umask) if defined $old_umask; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3127 | # Configure accept error handler after listen() to avoid SSL extension conflicts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3128 | 280 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3129 | $listener->configure( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3130 | 0 | ▶ | on_accept_error => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3131 | 0 | my ($listener, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3132 | 0 | ▶ | return unless $weak_self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3133 | 0 | $weak_self->_on_accept_error($error); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3134 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3135 | 280 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3136 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3137 | 280 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3138 | 280 | $self->_log(debug => "Could not configure on_accept_error (likely SSL listener): $@"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3141 | # Post-listen setup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3142 | 280 | ▶ | if ($spec->{type} eq 'unix') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3143 | # Apply socket permissions if configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3144 | 10 | ▶ | if (defined $spec->{socket_mode}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3145 | 1 | chmod $spec->{socket_mode}, $spec->{path}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3147 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3148 | # Store the actual bound port from the listener's read handle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3149 | 270 | my $socket = $listener->read_handle; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3150 | 270 | ▶ | if ($socket && $socket->can('sockport')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$socket and $socket->can("sockport")
MC/DC0%0/2
$socket and $socket->can("sockport")
$socket
$socket->can("sockport")
Truth table33%1/3
A and B
A= $socketB= $socket->can("sockport")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3151 | 270 | my $bound = $socket->sockport; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3152 | 270 | $spec->{port} = $bound; # update spec with actual port | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3153 | 270 | ▶ | $self->{bound_port} //= $bound; # first TCP port wins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"bound_port"} //= $bound
MC/DC0%0/2
$self->{"bound_port"} //= $bound
$self->{"bound_port"}
$bound
Truth table33%1/3
A //= B
A= $self->{"bound_port"}B= $bound
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3157 | # Register in PAGI_REUSE for hot restart fd inheritance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3158 | 280 | my $rh = $listener->read_handle; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3159 | 280 | ▶ | if ($rh) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3160 | 280 | my $fd = fileno($rh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3161 | 280 | ▶ | if (defined $fd) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3162 | 280 | ▶ | my $reuse_key = $spec->{type} eq 'unix' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3163 | ? "unix:$spec->{path}:$fd" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3164 | : "$spec->{host}:$spec->{port}:$fd"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3165 | 280 | $spec->{_reuse_key} = $reuse_key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3166 | 280 | $spec->{_reuse_fd} = $fd; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3167 | 280 | $OWNED_REUSE_FDS{$fd} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3168 | 280 | ▶ | $ENV{PAGI_REUSE} = length($ENV{PAGI_REUSE} // '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$ENV{"PAGI_REUSE"} // ""
MC/DC100%1/1
$ENV{"PAGI_REUSE"} // ""
$ENV{"PAGI_REUSE"}
Truth table100%2/2
A
A= $ENV{"PAGI_REUSE"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3169 | ? "$ENV{PAGI_REUSE},$reuse_key" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3170 | : $reuse_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3171 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3172 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3174 | 280 | push @listen_entries, { listener => $listener, spec => $spec }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3177 | # Warn about unmatched inherited fds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3178 | 280 | for my $key (sort keys %$inherited) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3179 | 0 | my $inh = $inherited->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3180 | 0 | $self->_log(warn => "Inherited fd $inh->{fd} ($key) does not match " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3181 | . "any listener spec — closing"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3182 | 0 | ▶ | if ($inh->{handle}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3183 | 0 | close($inh->{handle}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3184 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3185 | 0 | POSIX::close($inh->{fd}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3186 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3189 | 280 | $self->{_listen_entries} = \@listen_entries; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3190 | # Backward compat: keep $self->{listener} pointing to first entry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3191 | 280 | ▶ | $self->{listener} = $listen_entries[0]{listener} if @listen_entries; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3192 | 280 | $self->{running} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3194 | # Set up signal handlers for graceful shutdown (single-worker mode) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3195 | # Note: Windows doesn't support Unix signals, so this is skipped there | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3196 | 280 | unless (WIN32) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3197 | 280 | my $shutdown_triggered = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3198 | 0 | ▶ | my $shutdown_handler = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3199 | 0 | my ($signal) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3200 | 0 | ▶ | return if $shutdown_triggered; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3201 | 0 | $shutdown_triggered = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3202 | # Say so. A graceful stop that prints nothing is indistinguishable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3203 | # from the process being killed, which is the one thing an operator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3204 | # watching a terminal wants to be able to tell apart. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3205 | 0 | $self->_log(info => "Received $signal, shutting down"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3206 | $self->adopt_future( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3207 | 0 | ▶ | $self->shutdown->on_done(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3208 | 0 | $self->_log(info => 'Shutdown complete'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3209 | 0 | $self->loop->stop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3210 | 0 | ▶ | })->on_fail(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3211 | 0 | my ($error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3212 | 0 | $self->_log(error => "Shutdown error: $error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3213 | 0 | $self->loop->stop; # Still stop even on error | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3214 | }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3215 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3216 | 280 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3217 | 0 | ▶ | $self->loop->watch_signal(TERM => sub { $shutdown_handler->('TERM') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3218 | 0 | ▶ | $self->loop->watch_signal(INT => sub { $shutdown_handler->('INT') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3220 | # HUP in single-worker mode just warns (graceful restart requires multi-worker) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3221 | 280 | my $weak_self = $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3222 | 280 | weaken($weak_self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3223 | 0 | ▶ | $self->loop->watch_signal(HUP => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3224 | 0 | ▶ | $weak_self->_log(warn => "Received HUP signal (graceful restart only works in multi-worker mode)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3225 | if $weak_self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3226 | 280 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3228 | 0 | ▶ | $self->loop->watch_signal(USR2 => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3229 | 0 | ▶ | $weak_self->_log(warn => "Received USR2 signal (hot restart only works in multi-worker mode)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3230 | if $weak_self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3231 | 280 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3234 | # Warn if access_log is a terminal (slow for benchmarks) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3235 | 280 | ▶ | if ($self->{access_log} && -t $self->{access_log}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"access_log"} and -t $self->{"access_log"}
MC/DC0%0/2
$self->{"access_log"} and -t $self->{"access_log"}
$self->{"access_log"}
-t $self->{"access_log"}
Truth table66%2/3
A and B
A= $self->{"access_log"}B= -t $self->{"access_log"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3236 | 0 | $self->_startup_note(note => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3237 | 'access_log is a terminal; use a file when benchmarking'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3238 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3240 | # Log listening banner | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3241 | 280 | ▶ | my $scheme = $self->{tls_enabled} ? 'https' : 'http'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3242 | 280 | my @addrs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3243 | 280 | for my $entry (@listen_entries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3244 | 283 | my $s = $entry->{spec}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3245 | 283 | ▶ | push @addrs, $s->{type} eq 'unix' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3246 | ? "unix:$s->{path}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3247 | : "$scheme://$s->{host}:$s->{port}/"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3248 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3249 | 280 | $self->_log(info => $_) for $self->_startup_banner(join(', ', @addrs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3251 | # Warn in production if using default max_connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3252 | 280 | ▶ | if (($ENV{PAGI_ENV} // '') eq 'production' && !$self->{max_connections}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
($ENV{"PAGI_ENV"} // "") eq "production" and not $self->{"max_connections"}
$ENV{"PAGI_ENV"} // ""
MC/DC0%0/2
($ENV{"PAGI_ENV"} // "") eq "production" and not $self->{"max_connections"}
$ENV{"PAGI_ENV"}
not $self->{"max_connections"}
Truth table33%1/3
A and not(B)
A= $ENV{"PAGI_ENV"}B= not $self->{"max_connections"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3253 | 0 | $self->_log(warn => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3254 | "Using default max_connections (1000). For production, consider tuning this value " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3255 | "based on your workload. See 'perldoc PAGI::Server' for guidance." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3256 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3257 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3259 | 280 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3260 | 153 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3262 | # Multi-worker mode - forks workers, each with their own event loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3263 | 27 | ▶ | sub _listen_multiworker { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_listen_multiworker: called CC=50 SCAR=64.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3264 | 27 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3266 | 27 | my $workers = $self->{workers}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3267 | 27 | my $reuseport = $self->{reuseport}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3269 | # Create all listening sockets before forking workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3270 | 27 | my @listen_entries; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3272 | # Collect any inherited fds (from PAGI_REUSE or LISTEN_FDS) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3273 | 27 | my $inherited = $self->_collect_inherited_fds; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3275 | 27 | for my $spec (@{$self->{listeners}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3276 | 27 | my $socket; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3278 | # Check for inherited fd matching this spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3279 | 27 | ▶ | my $match_key = $spec->{type} eq 'unix' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3280 | ? "unix:$spec->{path}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3281 | : "$spec->{host}:$spec->{port}"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3283 | 27 | ▶ | if (my $inh = delete $inherited->{$match_key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3284 | 0 | $spec->{_inherited} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3286 | 0 | ▶ | if ($inh->{handle}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3287 | 0 | $socket = $inh->{handle}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3288 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3289 | 0 | ▶ | my $class = $inh->{type} eq 'unix' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3290 | ? 'IO::Socket::UNIX' : 'IO::Socket::INET'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3291 | 0 | ▶ | require IO::Socket::UNIX if $inh->{type} eq 'unix'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3292 | 0 | ▶ | $socket = $class->new_from_fd($inh->{fd}, 'r') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3293 | or die "Cannot open inherited fd $inh->{fd}: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3296 | 0 | ▶ | if ($inh->{type} eq 'tcp' && $socket->can('sockport')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$inh->{"type"} eq "tcp" and $socket->can("sockport")
MC/DC0%0/2
$inh->{"type"} eq "tcp" and $socket->can("sockport")
$inh->{"type"} eq "tcp"
$socket->can("sockport")
Truth table0%0/3
A and B
A= $inh->{"type"} eq "tcp"B= $socket->can("sockport")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3297 | 0 | $spec->{bound_port} = $socket->sockport; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3298 | 0 | ▶ | $self->{bound_port} //= $spec->{bound_port}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"bound_port"} //= $spec->{"bound_port"}
MC/DC0%0/2
$self->{"bound_port"} //= $spec->{"bound_port"}
$self->{"bound_port"}
$spec->{"bound_port"}
Truth table0%0/3
A //= B
A= $self->{"bound_port"}B= $spec->{"bound_port"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3299 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3301 | 0 | $self->_log(info => "Reusing inherited fd $inh->{fd} for $match_key" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3302 | . " (source: $inh->{source})"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3303 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3304 | elsif ($spec->{type} eq 'unix') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3305 | # Unix socket: parent creates, workers inherit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3306 | 0 | ▶ | unlink $spec->{path} if -e $spec->{path}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3308 | # Set restrictive umask for bind (CVE-2023-45145 mitigation) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3309 | 0 | my $old_umask = umask(0177); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3311 | 0 | require IO::Socket::UNIX; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3312 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3313 | 0 | local $^F = 1023; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3314 | $socket = IO::Socket::UNIX->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3315 | Local => $spec->{path}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3316 | Type => Socket::SOCK_STREAM(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3317 | Listen => $self->{listener_backlog}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3318 | 0 | ▶ | ) or die "Cannot create Unix socket $spec->{path}: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3319 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3321 | 0 | umask($old_umask); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3323 | 0 | ▶ | if (defined $spec->{socket_mode}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3324 | chmod($spec->{socket_mode}, $spec->{path}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3325 | 0 | ▶ | or die "Cannot chmod $spec->{path}: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3328 | # Register in PAGI_REUSE for hot restart fd inheritance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3329 | 0 | ▶ | if ($socket && !$spec->{_inherited}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$socket and not $spec->{"_inherited"}
MC/DC0%0/2
$socket and not $spec->{"_inherited"}
$socket
not $spec->{"_inherited"}
Truth table0%0/3
A and not(B)
A= $socketB= not $spec->{"_inherited"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3330 | 0 | my $fd = fileno($socket); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3331 | 0 | my $reuse_key = "unix:$spec->{path}:$fd"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3332 | 0 | $spec->{_reuse_key} = $reuse_key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3333 | 0 | $spec->{_reuse_fd} = $fd; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3334 | 0 | $OWNED_REUSE_FDS{$fd} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3335 | 0 | ▶ | $ENV{PAGI_REUSE} = length($ENV{PAGI_REUSE} // '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$ENV{"PAGI_REUSE"} // ""
MC/DC0%0/1
$ENV{"PAGI_REUSE"} // ""
$ENV{"PAGI_REUSE"}
Truth table0%0/2
A
A= $ENV{"PAGI_REUSE"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3336 | ? "$ENV{PAGI_REUSE},$reuse_key" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3337 | : $reuse_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3338 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3339 | } elsif ($reuseport) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3340 | # reuseport TCP: probe to get port, workers create their own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3341 | # Note: reuseport sockets are not registered in PAGI_REUSE because | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3342 | # each worker creates its own socket. fd inheritance for reuseport | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3343 | # mode is not currently supported — use shared-socket mode for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3344 | # hot restart / systemd socket activation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3345 | my $probe_socket = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3346 | LocalAddr => $spec->{host}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3347 | LocalPort => $spec->{port}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3348 | 0 | ▶ | Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3349 | Listen => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3350 | ReuseAddr => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3351 | ReusePort => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3352 | ) or die "Cannot bind to $spec->{host}:$spec->{port}: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3353 | 0 | $spec->{bound_port} = $probe_socket->sockport; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3354 | 0 | ▶ | $self->{bound_port} //= $spec->{bound_port}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"bound_port"} //= $spec->{"bound_port"}
MC/DC0%0/2
$self->{"bound_port"} //= $spec->{"bound_port"}
$self->{"bound_port"}
$spec->{"bound_port"}
Truth table0%0/3
A //= B
A= $self->{"bound_port"}B= $spec->{"bound_port"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3355 | 0 | close($probe_socket); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3356 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3357 | # Shared-socket TCP: parent creates, workers inherit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3358 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3359 | 27 | local $^F = 1023; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3360 | $socket = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3361 | LocalAddr => $spec->{host}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3362 | LocalPort => $spec->{port}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3363 | Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3364 | Listen => $self->{listener_backlog}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3365 | 27 | ▶ | ReuseAddr => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3366 | Blocking => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3367 | ) or die "Cannot create listening socket on $spec->{host}:$spec->{port}: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3368 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3369 | 27 | $spec->{bound_port} = $socket->sockport; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3370 | 27 | ▶ | $self->{bound_port} //= $spec->{bound_port}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"bound_port"} //= $spec->{"bound_port"}
MC/DC0%0/2
$self->{"bound_port"} //= $spec->{"bound_port"}
$self->{"bound_port"}
$spec->{"bound_port"}
Truth table33%1/3
A //= B
A= $self->{"bound_port"}B= $spec->{"bound_port"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3372 | # Register in PAGI_REUSE for hot restart fd inheritance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3373 | 27 | ▶ | if ($socket && !$spec->{_inherited}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$socket and not $spec->{"_inherited"}
MC/DC0%0/2
$socket and not $spec->{"_inherited"}
$socket
not $spec->{"_inherited"}
Truth table33%1/3
A and not(B)
A= $socketB= not $spec->{"_inherited"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3374 | 27 | my $fd = fileno($socket); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3375 | 27 | my $reuse_key = "$spec->{host}:" . $socket->sockport . ":$fd"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3376 | 27 | $spec->{_reuse_key} = $reuse_key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3377 | 27 | $spec->{_reuse_fd} = $fd; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3378 | 27 | $OWNED_REUSE_FDS{$fd} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3379 | 27 | ▶ | $ENV{PAGI_REUSE} = length($ENV{PAGI_REUSE} // '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$ENV{"PAGI_REUSE"} // ""
MC/DC100%1/1
$ENV{"PAGI_REUSE"} // ""
$ENV{"PAGI_REUSE"}
Truth table100%2/2
A
A= $ENV{"PAGI_REUSE"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3380 | ? "$ENV{PAGI_REUSE},$reuse_key" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3381 | : $reuse_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3383 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3385 | 27 | push @listen_entries, { socket => $socket, spec => $spec }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3386 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3388 | # Warn about unmatched inherited fds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3389 | 27 | for my $key (sort keys %$inherited) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3390 | 0 | my $inh = $inherited->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3391 | 0 | $self->_log(warn => "Inherited fd $inh->{fd} ($key) does not match " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3392 | . "any listener spec — closing"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3393 | 0 | ▶ | if ($inh->{handle}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3394 | 0 | close($inh->{handle}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3395 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3396 | 0 | POSIX::close($inh->{fd}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3397 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3398 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3400 | 27 | $self->{_listen_entries} = \@listen_entries; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3401 | # Backward compat: keep listen_socket pointing to first entry's socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3402 | 27 | ▶ | $self->{listen_socket} = $listen_entries[0]{socket} if @listen_entries && $listen_entries[0]{socket}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@listen_entries and $listen_entries[0]{"socket"}
MC/DC0%0/2
@listen_entries and $listen_entries[0]{"socket"}
@listen_entries
$listen_entries[0]{"socket"}
Truth table33%1/3
A and B
A= @listen_entriesB= $listen_entries[0]{"socket"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3404 | 27 | $self->{running} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3406 | # Validate TLS modules and set tls_enabled before forking workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3407 | 27 | ▶ | if ($self->{ssl}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3408 | 6 | $self->_check_tls_available; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3409 | 6 | $self->{tls_enabled} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3410 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3412 | 27 | ▶ | my $scheme = $self->{ssl} ? 'https' : 'http'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3413 | 27 | ▶ | my $mode = $reuseport ? 'reuseport' : 'shared-socket'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3415 | # Warn if access_log is a terminal (slow for benchmarks) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3416 | 27 | ▶ | if ($self->{access_log} && -t $self->{access_log}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"access_log"} and -t $self->{"access_log"}
MC/DC0%0/2
$self->{"access_log"} and -t $self->{"access_log"}
$self->{"access_log"}
-t $self->{"access_log"}
Truth table66%2/3
A and B
A= $self->{"access_log"}B= -t $self->{"access_log"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3417 | 0 | $self->_startup_note(note => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3418 | 'access_log is a terminal; use a file when benchmarking'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3419 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3421 | # Log listening banner for all listeners | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3422 | 27 | my @addrs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3423 | 27 | for my $entry (@listen_entries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3424 | 27 | my $s = $entry->{spec}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3425 | 27 | ▶ | if ($s->{type} eq 'unix') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3426 | 0 | push @addrs, "unix:$s->{path}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3427 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3428 | 27 | ▶ | my $port = $s->{bound_port} // $s->{port}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$s->{"bound_port"} // $s->{"port"}
MC/DC0%0/2
$s->{"bound_port"} // $s->{"port"}
$s->{"bound_port"}
$s->{"port"}
Truth table33%1/3
A // B
A= $s->{"bound_port"}B= $s->{"port"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3429 | 27 | push @addrs, "$scheme://$s->{host}:$port/"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3430 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3431 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3432 | 27 | my $where = join(', ', @addrs) . " with $workers workers ($mode)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3433 | 27 | $self->_log(info => $_) for $self->_startup_banner($where, 'per worker'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3434 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3435 | # Warn in production if using default max_connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3436 | 27 | ▶ | if (($ENV{PAGI_ENV} // '') eq 'production' && !$self->{max_connections}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
($ENV{"PAGI_ENV"} // "") eq "production" and not $self->{"max_connections"}
$ENV{"PAGI_ENV"} // ""
MC/DC50%1/2
($ENV{"PAGI_ENV"} // "") eq "production" and not $self->{"max_connections"}
$ENV{"PAGI_ENV"}
not $self->{"max_connections"}
Truth table66%2/3
A and not(B)
A= $ENV{"PAGI_ENV"}B= not $self->{"max_connections"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3437 | 3 | $self->_log(warn => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3438 | "Using default max_connections (1000). For production, consider tuning this value " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3439 | "based on your workload. See 'perldoc PAGI::Server' for guidance." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3440 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3441 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3442 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3443 | 27 | my $loop = $self->loop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3444 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3445 | # Fork the workers FIRST, before setting up signal handlers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3446 | # This prevents children from inheriting the parent's sigpipe setup, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3447 | # which can cause issues with Ctrl-C signal delivery on macOS. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3448 | 27 | for my $i (1 .. $workers) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3449 | 40 | $self->_spawn_worker(\@listen_entries, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3450 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3451 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3452 | # Set up signal handlers for parent process AFTER forking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3453 | # Note: Windows doesn't support Unix signals, so this is skipped there | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3454 | 11 | unless (WIN32) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3455 | 11 | ▶ | $loop->watch_signal(TERM => sub { $self->_initiate_multiworker_shutdown('TERM') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3456 | 11 | ▶ | $loop->watch_signal(INT => sub { $self->_initiate_multiworker_shutdown('INT') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3457 | 0 | ▶ | $loop->watch_signal(HUP => sub { $self->_graceful_restart }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3458 | 0 | ▶ | $loop->watch_signal(TTIN => sub { $self->_increase_workers }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3459 | 0 | ▶ | $loop->watch_signal(TTOU => sub { $self->_decrease_workers }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3460 | 0 | ▶ | $loop->watch_signal(USR2 => sub { $self->_hot_restart }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3461 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3463 | # Start heartbeat monitor if enabled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3464 | 11 | ▶ | if ($self->{heartbeat_timeout} && $self->{heartbeat_timeout} > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"heartbeat_timeout"} and $self->{"heartbeat_timeout"} > 0
MC/DC0%0/2
$self->{"heartbeat_timeout"} and $self->{"heartbeat_timeout"} > 0
$self->{"heartbeat_timeout"}
$self->{"heartbeat_timeout"} > 0
Truth table33%1/3
A and B
A= $self->{"heartbeat_timeout"}B= $self->{"heartbeat_timeout"} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3465 | 11 | my $hb_timeout = $self->{heartbeat_timeout}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3466 | 11 | my $check_interval = $hb_timeout / 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3467 | 11 | weaken(my $weak_self = $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3468 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3469 | my $hb_check_timer = IO::Async::Timer::Periodic->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3470 | interval => $check_interval, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3471 | 0 | ▶ | on_tick => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3472 | 0 | ▶ | return unless $weak_self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3473 | 0 | ▶ | return if $weak_self->{shutting_down}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3475 | 0 | my $now = time(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3476 | 0 | for my $pid (keys %{$weak_self->{worker_pids}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3477 | 0 | my $info = $weak_self->{worker_pids}{$pid}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3478 | 0 | ▶ | next unless $info->{heartbeat_rd}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3479 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3480 | # Drain all available heartbeat bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3481 | 0 | ▶ | while (sysread($info->{heartbeat_rd}, my $buf, 64)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3482 | 0 | $info->{last_heartbeat} = $now; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3483 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3484 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3485 | # Kill if heartbeat expired | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3486 | 0 | ▶ | if ($now - $info->{last_heartbeat} > $hb_timeout) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3487 | 0 | $weak_self->_log(warn => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3488 | "Worker $pid (worker $info->{worker_num}) heartbeat " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3489 | "timeout after ${hb_timeout}s, sending SIGKILL"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3490 | 0 | kill 'KILL', $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3491 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3493 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3494 | 11 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3495 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3496 | 11 | $self->add_child($hb_check_timer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3497 | 11 | $hb_check_timer->start; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3498 | 11 | $self->{_heartbeat_check_timer} = $hb_check_timer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3499 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3500 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3501 | # Hot restart handoff: if we were spawned by USR2, signal the old master | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3502 | 11 | ▶ | if (my $old_master_pid = delete $ENV{PAGI_MASTER_PID}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3503 | 0 | $old_master_pid = int($old_master_pid); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3504 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3505 | # Wait for workers to be healthy before retiring old master | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3506 | # Delay = half the heartbeat timeout + 1 second buffer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3507 | 0 | ▶ | my $handoff_delay = ($self->{heartbeat_timeout} || 10) / 2 + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'heartbeat_timeout'} || 10
MC/DC0%0/1
$self->{'heartbeat_timeout'} || 10
$self->{'heartbeat_timeout'}
Truth table0%0/2
A
A= $self->{'heartbeat_timeout'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3508 | 0 | weaken(my $weak_self_handoff = $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3509 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3510 | $self->loop->watch_time( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3511 | after => $handoff_delay, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3512 | 0 | ▶ | code => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3513 | 0 | ▶ | return unless $weak_self_handoff; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3514 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3515 | 0 | my $worker_count = scalar keys %{$weak_self_handoff->{worker_pids}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3516 | 0 | ▶ | if ($worker_count == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3517 | 0 | $weak_self_handoff->_log(error => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3518 | "Hot restart: no workers running, not retiring old master $old_master_pid"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3519 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3520 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3521 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3522 | 0 | ▶ | if (kill(0, $old_master_pid)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3523 | 0 | $weak_self_handoff->_log(info => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3524 | "Hot restart: $worker_count workers healthy, " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3525 | . "sending SIGTERM to old master $old_master_pid"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3526 | 0 | kill('TERM', $old_master_pid); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3527 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3528 | 0 | $weak_self_handoff->_log(warn => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3529 | "Hot restart: old master $old_master_pid is no longer running"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3530 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3531 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3532 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3533 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3534 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3535 | # Return immediately - caller (Runner) will call $loop->run() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3536 | # This is consistent with single-worker mode behavior | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3537 | 11 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3538 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3539 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3540 | # Collect inherited file descriptors from PAGI_REUSE and LISTEN_FDS. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3541 | # Returns a hashref keyed by "host:port" or "unix:path", values are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3542 | # { fd, type, host/port/path, handle?, source } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3543 | 315 | ▶ | sub _collect_inherited_fds { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect_inherited_fds: called CC=20 SCAR=32.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3544 | 315 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3545 | 315 | my %inherited; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3546 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3547 | # Source 1: PAGI_REUSE (format: addr:port:fd,unix:path:fd,...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3548 | 315 | ▶ | if (my $reuse = $ENV{PAGI_REUSE}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3549 | 10 | for my $entry (split /,/, $reuse) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3550 | 13 | ▶ | if ($entry =~ /^unix:(.+):(\d+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3551 | 3 | my ($path, $fd) = ($1, int($2)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3552 | 3 | $inherited{"unix:$path"} = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3553 | fd => $fd, type => 'unix', path => $path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3554 | source => 'pagi_reuse', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3555 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3556 | } elsif ($entry =~ /^(\[.+?\]):(\d+):(\d+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3557 | 1 | my ($host, $port, $fd) = ($1, int($2), int($3)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3558 | 1 | $inherited{"$host:$port"} = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3559 | fd => $fd, type => 'tcp', host => $host, port => $port, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3560 | source => 'pagi_reuse', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3561 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3562 | } elsif ($entry =~ /^(.+):(\d+):(\d+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3563 | 7 | my ($host, $port, $fd) = ($1, int($2), int($3)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3564 | 7 | $inherited{"$host:$port"} = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3565 | fd => $fd, type => 'tcp', host => $host, port => $port, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3566 | source => 'pagi_reuse', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3567 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3568 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3569 | # Malformed entries silently skipped | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3570 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3571 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3572 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3573 | # Source 2: LISTEN_FDS (systemd socket activation) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3574 | 315 | my $listen_fds = $ENV{LISTEN_FDS}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3575 | 315 | ▶ | if (defined $listen_fds && $listen_fds =~ /^\d+$/ && $listen_fds > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $listen_fds and $listen_fds =~ /^\d+$/ and $listen_fds > 0
defined $listen_fds and $listen_fds =~ /^\d+$/
MC/DC33%1/3
defined $listen_fds and $listen_fds =~ /^\d+$/ and $listen_fds > 0
defined $listen_fds
$listen_fds =~ /^\d+$/
$listen_fds > 0
Truth table50%2/4
A and B and C
A= defined $listen_fdsB= $listen_fds =~ /^\d+$/C= $listen_fds > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3576 | 3 | ▶ | if (defined $ENV{LISTEN_PID} && $ENV{LISTEN_PID} == $$) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ENV{"LISTEN_PID"} and $ENV{"LISTEN_PID"} == $$
MC/DC50%1/2
defined $ENV{"LISTEN_PID"} and $ENV{"LISTEN_PID"} == $$
defined $ENV{"LISTEN_PID"}
$ENV{"LISTEN_PID"} == $$
Truth table66%2/3
A and B
A= defined $ENV{"LISTEN_PID"}B= $ENV{"LISTEN_PID"} == $$
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3577 | 1 | my $n = int($listen_fds); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3578 | 1 | for my $i (0 .. $n - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3579 | 1 | my $fd = 3 + $i; # SD_LISTEN_FDS_START | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3580 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3581 | 1 | my $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3582 | 1 | ▶ | unless (open($fh, '+<&=', $fd)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3583 | 0 | $self->_log(warn => "Cannot fdopen inherited fd $fd: $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3584 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3585 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3586 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3587 | 1 | my $addr = getsockname($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3588 | 1 | ▶ | unless ($addr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3589 | 0 | $self->_log(warn => "Cannot getsockname on inherited fd $fd: $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3590 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3591 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3592 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3593 | 1 | my $family = sockaddr_family($addr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3594 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3595 | 1 | ▶ | if ($family == AF_UNIX) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3596 | 0 | my $path = unpack_sockaddr_un($addr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3597 | 0 | my $key = "unix:$path"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3598 | 0 | ▶ | $inherited{$key} //= { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$inherited{$key} //= {"fd", $fd, "type", "unix", "path", $path, "handle", $fh, "source", "systemd"}
MC/DC0%0/1
$inherited{$key} //= {"fd", $fd, "type", "unix", "path", $path, "handle", $fh, "source", "systemd"}
$inherited{$key}
Truth table0%0/2
A
A= $inherited{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3599 | fd => $fd, type => 'unix', path => $path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3600 | handle => $fh, source => 'systemd', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3601 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3602 | } elsif ($family == AF_INET) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3603 | 1 | my ($port, $host_packed) = unpack_sockaddr_in($addr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3604 | 1 | my $host = Socket::inet_ntoa($host_packed); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3605 | 1 | my $key = "$host:$port"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3606 | 1 | ▶ | $inherited{$key} //= { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$inherited{$key} //= {"fd", $fd, "type", "tcp", "host", $host, "port", $port, "handle", $fh, "source", "systemd"}
MC/DC0%0/1
$inherited{$key} //= {"fd", $fd, "type", "tcp", "host", $host, "port", $port, "handle", $fh, "source", "systemd"}
$inherited{$key}
Truth table50%1/2
A
A= $inherited{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3607 | fd => $fd, type => 'tcp', host => $host, port => $port, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3608 | handle => $fh, source => 'systemd', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3609 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3610 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3611 | 0 | $self->_log(warn => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3612 | "Inherited fd $fd has unsupported address family $family"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3613 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3614 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3615 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3616 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3617 | # Always clean up systemd env vars (per sd_listen_fds spec) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3618 | 3 | delete @ENV{qw(LISTEN_FDS LISTEN_PID LISTEN_FDNAMES)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3619 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3620 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3621 | # Entries published by a live sibling server in THIS process are not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3622 | # inheritance -- leave them alone (neither claim nor close). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3623 | 315 | for my $key (keys %inherited) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3624 | 12 | ▶ | next unless $OWNED_REUSE_FDS{ $inherited{$key}{fd} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3625 | 3 | $self->_log(debug => "PAGI_REUSE entry $key belongs to a live server " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3626 | . "in this process; leaving it alone"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3627 | 3 | delete $inherited{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3628 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3629 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3630 | 315 | return \%inherited; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3631 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3632 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3633 | # Initiate graceful shutdown in multi-worker mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3634 | 8 | ▶ | sub _initiate_multiworker_shutdown { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_initiate_multiworker_shutdown: called CC=25 SCAR=39.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3635 | 8 | my ($self, $signal) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3636 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3637 | 8 | ▶ | return if $self->{shutting_down}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3638 | 8 | $self->{shutting_down} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3639 | 8 | $self->{running} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3640 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3641 | # See the single-worker handler: a silent graceful stop cannot be told | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3642 | # apart from the process being killed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3643 | 8 | ▶ | $self->_log(info => "Received $signal, shutting down $self->{workers} workers") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3644 | if defined $signal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3645 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3646 | # Stop heartbeat monitoring — shutdown escalation timer handles stuck workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3647 | 8 | ▶ | if ($self->{_heartbeat_check_timer}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3648 | 8 | $self->{_heartbeat_check_timer}->stop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3649 | 8 | $self->remove_child($self->{_heartbeat_check_timer}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3650 | 8 | delete $self->{_heartbeat_check_timer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3651 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3652 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3653 | # Close all listen sockets to stop accepting new connections | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3654 | # (skip during hot restart — new master is using these fds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3655 | 8 | ▶ | if (!$self->{_hot_restart_in_progress}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3656 | 8 | ▶ | for my $entry (@{$self->{_listen_entries} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_listen_entries"} // []
MC/DC0%0/1
$self->{"_listen_entries"} // []
$self->{"_listen_entries"}
Truth table50%1/2
A
A= $self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3657 | 8 | ▶ | if ($entry->{socket}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3658 | 8 | close($entry->{socket}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3659 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3660 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3661 | 8 | ▶ | if ($self->{listen_socket}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3662 | 8 | delete $self->{listen_socket}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3663 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3664 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3665 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3666 | # Clean up PAGI_REUSE entries (skip during hot restart) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3667 | 8 | ▶ | if (!$self->{_hot_restart_in_progress}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3668 | 8 | ▶ | for my $entry (@{$self->{_listen_entries} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_listen_entries"} // []
MC/DC0%0/1
$self->{"_listen_entries"} // []
$self->{"_listen_entries"}
Truth table50%1/2
A
A= $self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3669 | 8 | my $key = $entry->{spec}{_reuse_key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3670 | 8 | ▶ | if ($key && defined $ENV{PAGI_REUSE}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$key and defined $ENV{"PAGI_REUSE"}
MC/DC0%0/2
$key and defined $ENV{"PAGI_REUSE"}
$key
defined $ENV{"PAGI_REUSE"}
Truth table33%1/3
A and B
A= $keyB= defined $ENV{"PAGI_REUSE"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3671 | 8 | $ENV{PAGI_REUSE} =~ s/(?:^|,)\Q$key\E//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3672 | delete $OWNED_REUSE_FDS{ $entry->{spec}{_reuse_fd} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3673 | 8 | ▶ | if defined $entry->{spec}{_reuse_fd}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3674 | 8 | ▶ | $ENV{PAGI_REUSE} =~ s/^,// if defined $ENV{PAGI_REUSE}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3675 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3676 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3677 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3678 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3679 | # Clean up Unix socket files (skip inherited, skip during hot restart) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3680 | 8 | ▶ | if (!$self->{_hot_restart_in_progress}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3681 | 8 | ▶ | for my $entry (@{$self->{_listen_entries} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_listen_entries"} // []
MC/DC0%0/1
$self->{"_listen_entries"} // []
$self->{"_listen_entries"}
Truth table50%1/2
A
A= $self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3682 | 8 | ▶ | if ($entry->{spec}{type} eq 'unix' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$entry->{"spec"}{"type"} eq "unix" and not $entry->{"spec"}{"_inherited"} and -e $entry->{"spec"}{"path"}
$entry->{"spec"}{"type"} eq "unix" and not $entry->{"spec"}{"_inherited"}
MC/DC0%0/3
$entry->{"spec"}{"type"} eq "unix" and not $entry->{"spec"}{"_inherited"} and -e $entry->{"spec"}{"path"}
$entry->{"spec"}{"type"} eq "unix"
not $entry->{"spec"}{"_inherited"}
-e $entry->{"spec"}{"path"}
Truth table25%1/4
A and not(B) and C
A= $entry->{"spec"}{"type"} eq "unix"B= not $entry->{"spec"}{"_inherited"}C= -e $entry->{"spec"}{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3683 | && !$entry->{spec}{_inherited} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3684 | && -e $entry->{spec}{path}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3685 | 0 | unlink $entry->{spec}{path}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3686 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3687 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3688 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3689 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3690 | # Signal all workers to shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3691 | 8 | for my $pid (keys %{$self->{worker_pids}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3692 | 12 | kill 'TERM', $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3693 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3694 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3695 | # If no workers, stop the loop immediately | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3696 | 8 | ▶ | if (!keys %{$self->{worker_pids}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3697 | 0 | $self->loop->stop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3698 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3699 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3700 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3701 | # Escalate to SIGKILL after shutdown_timeout for workers that ignore SIGTERM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3702 | 8 | ▶ | my $timeout = $self->{shutdown_timeout} // 30; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"shutdown_timeout"} // 30
MC/DC0%0/1
$self->{"shutdown_timeout"} // 30
$self->{"shutdown_timeout"}
Truth table50%1/2
A
A= $self->{"shutdown_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3703 | 8 | weaken(my $weak_self = $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3704 | $self->{_shutdown_kill_timer} = $self->loop->watch_time( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3705 | after => $timeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3706 | 0 | ▶ | code => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3707 | 0 | ▶ | return unless $weak_self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3708 | 0 | for my $pid (keys %{$weak_self->{worker_pids}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3709 | 0 | $weak_self->_log(warn => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3710 | "Worker $pid did not exit after ${timeout}s, sending SIGKILL"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3711 | 0 | kill 'KILL', $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3712 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3713 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3714 | 8 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3715 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3716 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3717 | # Graceful restart: replace all workers one by one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3718 | 0 | ▶ | sub _graceful_restart { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_graceful_restart: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3719 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3720 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3721 | 0 | ▶ | return if $self->{shutting_down}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3722 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3723 | 0 | $self->_log(info => "Received HUP, performing graceful restart"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3724 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3725 | # Signal all current workers to shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3726 | # watch_process callbacks will respawn them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3727 | 0 | ▶ | my $timeout = $self->{shutdown_timeout} // 30; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"shutdown_timeout"} // 30
MC/DC0%0/1
$self->{"shutdown_timeout"} // 30
$self->{"shutdown_timeout"}
Truth table0%0/2
A
A= $self->{"shutdown_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3728 | 0 | for my $pid (keys %{$self->{worker_pids}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3729 | 0 | kill 'TERM', $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3730 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3731 | # Escalate to SIGKILL if worker doesn't exit within shutdown_timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3732 | 0 | weaken(my $weak_self = $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3733 | $self->{_restart_kill_timers}{$pid} = $self->loop->watch_time( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3734 | after => $timeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3735 | 0 | ▶ | code => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3736 | 0 | ▶ | return unless $weak_self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3737 | 0 | ▶ | if (exists $weak_self->{worker_pids}{$pid}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3738 | 0 | $weak_self->_log(warn => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3739 | "Worker $pid did not exit after ${timeout}s during restart, sending SIGKILL"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3740 | 0 | kill 'KILL', $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3741 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3742 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3743 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3744 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3745 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3746 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3747 | # Hot restart: fork+exec a new master that inherits listen sockets via PAGI_REUSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3748 | 0 | ▶ | sub _hot_restart { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hot_restart: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3749 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3750 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3751 | 0 | ▶ | if ($self->{_hot_restart_in_progress}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3752 | 0 | $self->_log(warn => "Hot restart already in progress, ignoring USR2"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3753 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3754 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3755 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3756 | 0 | ▶ | if ($self->{shutting_down}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3757 | 0 | $self->_log(warn => "Server is shutting down, ignoring USR2"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3758 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3759 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3760 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3761 | 0 | $self->{_hot_restart_in_progress} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3762 | 0 | $self->_log(info => "Received USR2, starting hot restart"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3763 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3764 | # Store our PID so the new master can signal us when ready | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3765 | 0 | $ENV{PAGI_MASTER_PID} = $$; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3766 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3767 | # Fork and exec a new master process | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3768 | 0 | my $pid = fork(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3769 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3770 | 0 | ▶ | if (!defined $pid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3771 | 0 | $self->_log(error => "Hot restart fork failed: $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3772 | 0 | $self->{_hot_restart_in_progress} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3773 | 0 | delete $ENV{PAGI_MASTER_PID}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3774 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3775 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3776 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3777 | 0 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3778 | # Child: exec new master | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3779 | my @args = defined $ENV{PAGI_ARGV} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3780 | ? split(/\0/, $ENV{PAGI_ARGV}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3781 | 0 | ▶ | : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3782 | exec($^X, $0, @args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3783 | 0 | ▶ | or do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3784 | 0 | $self->_log(error => "Hot restart exec failed: $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3785 | 0 | POSIX::_exit(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3786 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3787 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3788 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3789 | # Parent: log and continue running | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3790 | 0 | $self->_log(info => "Hot restart: new master spawned as PID $pid"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3791 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3792 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3793 | # Increase worker pool by 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3794 | 0 | ▶ | sub _increase_workers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_increase_workers: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3795 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3796 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3797 | 0 | ▶ | return if $self->{shutting_down}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3798 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3799 | 0 | my $current = scalar keys %{$self->{worker_pids}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3800 | 0 | my $new_worker_num = $current + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3801 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3802 | 0 | $self->_log(info => "Received TTIN, spawning worker $new_worker_num (total: $new_worker_num)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3803 | 0 | $self->_spawn_worker($self->{_listen_entries}, $new_worker_num); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3804 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3805 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3806 | # Decrease worker pool by 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3807 | 0 | ▶ | sub _decrease_workers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decrease_workers: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3808 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3809 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3810 | 0 | ▶ | return if $self->{shutting_down}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3811 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3812 | 0 | my @pids = keys %{$self->{worker_pids}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3813 | 0 | ▶ | return unless @pids > 1; # Keep at least 1 worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3814 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3815 | 0 | my $victim_pid = $pids[-1]; # Kill most recent | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3816 | 0 | my $remaining = scalar(@pids) - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3817 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3818 | 0 | $self->_log(info => "Received TTOU, killing worker (remaining: $remaining)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3819 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3820 | # Mark as "don't respawn" by setting a flag before killing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3821 | 0 | $self->{_dont_respawn}{$victim_pid} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3822 | 0 | kill 'TERM', $victim_pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3823 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3824 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3825 | 40 | ▶ | sub _spawn_worker { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_spawn_worker: called CC=6 SCAR=19.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3826 | 40 | my ($self, $listen_entries, $worker_num) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3827 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3828 | 40 | my $loop = $self->loop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3829 | 40 | weaken(my $weak_self = $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3830 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3831 | # Create heartbeat pipe if enabled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3832 | 40 | my ($hb_rd, $hb_wr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3833 | 40 | ▶ | if ($self->{heartbeat_timeout} && $self->{heartbeat_timeout} > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"heartbeat_timeout"} and $self->{"heartbeat_timeout"} > 0
MC/DC0%0/2
$self->{"heartbeat_timeout"} and $self->{"heartbeat_timeout"} > 0
$self->{"heartbeat_timeout"}
$self->{"heartbeat_timeout"} > 0
Truth table33%1/3
A and B
A= $self->{"heartbeat_timeout"}B= $self->{"heartbeat_timeout"} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3834 | 40 | ▶ | pipe($hb_rd, $hb_wr) or die "Cannot create heartbeat pipe: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3835 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3836 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3837 | # Set IGNORE before fork - child inherits it. IO::Async only resets | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3838 | # CODE refs, so 'IGNORE' (a string) survives. Child must NOT call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3839 | # watch_signal(INT) or it will overwrite the IGNORE. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3840 | 40 | my $old_sigint = $SIG{INT}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3841 | 40 | $SIG{INT} = 'IGNORE' unless WIN32; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3842 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3843 | my $pid = $loop->fork( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3844 | 16 | ▶ | code => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3845 | 16 | ▶ | close($hb_rd) if $hb_rd; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3846 | 16 | $self->_run_as_worker($listen_entries, $worker_num, $hb_wr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3847 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3848 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3849 | 40 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3850 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3851 | # Restore parent's SIGINT handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3852 | 24 | $SIG{INT} = $old_sigint unless WIN32; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3853 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3854 | 24 | ▶ | die "Fork failed" unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3855 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3856 | # Parent — close write end, set read end non-blocking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3857 | 24 | ▶ | if ($hb_wr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3858 | 24 | close($hb_wr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3859 | 24 | $hb_rd->blocking(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3860 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3861 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3862 | # Parent - track the worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3863 | 24 | $self->{worker_pids}{$pid} = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3864 | worker_num => $worker_num, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3865 | started => time(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3866 | heartbeat_rd => $hb_rd, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3867 | last_heartbeat => time(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3868 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3869 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3870 | # Use watch_process to handle worker exit (replaces manual SIGCHLD handling) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3871 | 14 | ▶ | $loop->watch_process($pid => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=27.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3872 | 14 | my ($exit_pid, $exitcode) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3873 | 14 | ▶ | return unless $weak_self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3874 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3875 | # Close heartbeat pipe read end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3876 | 14 | ▶ | if (my $info = $weak_self->{worker_pids}{$exit_pid}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3877 | 14 | ▶ | close($info->{heartbeat_rd}) if $info->{heartbeat_rd}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3878 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3879 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3880 | # Remove from tracking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3881 | 14 | delete $weak_self->{worker_pids}{$exit_pid}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3882 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3883 | # Cancel per-worker restart kill timer if one exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3884 | 14 | ▶ | if (my $timer_id = delete $weak_self->{_restart_kill_timers}{$exit_pid}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3885 | 0 | $loop->unwatch_time($timer_id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3886 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3887 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3888 | # Check exit code: exit(2) = startup failure, don't respawn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3889 | 14 | my $exit_code = $exitcode >> 8; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3890 | 14 | ▶ | if ($exit_code == 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3891 | 4 | $weak_self->_log(warn => "Worker $worker_num startup failed, not respawning"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3892 | # Don't respawn - startup failure would just repeat | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3893 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3894 | # Respawn if still running and not shutting down | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3895 | ▶ | elsif ($weak_self->{running} && !$weak_self->{shutting_down}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$weak_self->{"running"} and not $weak_self->{"shutting_down"}
MC/DC0%0/2
$weak_self->{"running"} and not $weak_self->{"shutting_down"}
$weak_self->{"running"}
not $weak_self->{"shutting_down"}
Truth table33%1/3
A and not(B)
A= $weak_self->{"running"}B= not $weak_self->{"shutting_down"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3896 | # Don't respawn if this was a TTOU reduction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3897 | 0 | ▶ | unless (delete $weak_self->{_dont_respawn}{$exit_pid}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3898 | 0 | $weak_self->_spawn_worker($listen_entries, $worker_num); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3899 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3900 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3901 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3902 | # Check if all workers have exited. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3903 | 14 | ▶ | if (!keys %{$weak_self->{worker_pids}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3904 | 9 | ▶ | if ($weak_self->{shutting_down}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3905 | # Normal shutdown: stop once the last worker is gone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3906 | 7 | ▶ | if ($weak_self->{_shutdown_kill_timer}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3907 | 7 | $loop->unwatch_time($weak_self->{_shutdown_kill_timer}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3908 | 7 | delete $weak_self->{_shutdown_kill_timer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3909 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3910 | 7 | $loop->stop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3911 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3912 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3913 | # Not shutting down, yet no workers remain and none were | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3914 | # respawned -- e.g. every worker failed lifespan startup (exit 2). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3915 | # The master would otherwise sit holding the listening socket with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3916 | # zero workers serving and never exit. Like uvicorn (#1115/#1177), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3917 | # stop and exit non-zero so a supervisor detects the failure and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3918 | # restarts us. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3919 | 2 | $weak_self->_log(error => | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3920 | "All workers have exited and none were respawned; shutting down"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3921 | 2 | $weak_self->{_workers_failed} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3922 | 2 | $loop->stop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3923 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3924 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3925 | 24 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3926 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3927 | 24 | return $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3928 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3929 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3930 | 16 | ▶ | sub _run_as_worker { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_as_worker: called CC=26 SCAR=37.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3931 | 16 | my ($self, $listen_entries, $worker_num, $heartbeat_wr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3932 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3933 | # Note: $SIG{INT} = 'IGNORE' inherited from parent - do NOT call watch_signal(INT) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3934 | # or it will overwrite the IGNORE with a CODE ref! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3935 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3936 | # The loop itself is created below, after $worker_server exists, via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3937 | # $worker_server->_create_loop -- so it respects a configured loop_type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3938 | # the same way the single-process run() path does. (Note: $ONE_TRUE_LOOP | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3939 | # was already cleared by $loop->fork(), so this always creates a fresh loop.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3940 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3941 | # In reuseport mode, each worker creates its own TCP listening socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3942 | 16 | my $reuseport = $self->{reuseport}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3943 | 16 | for my $entry (@$listen_entries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3944 | 16 | my $spec = $entry->{spec}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3945 | 16 | ▶ | if (!$entry->{socket} && $reuseport && $spec->{type} eq 'tcp') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not $entry->{"socket"} and $reuseport and $spec->{"type"} eq "tcp"
not $entry->{"socket"} and $reuseport
MC/DC0%0/3
not $entry->{"socket"} and $reuseport and $spec->{"type"} eq "tcp"
not $entry->{"socket"}
$reuseport
$spec->{"type"} eq "tcp"
Truth table25%1/4
not(A) and B and C
A= not $entry->{"socket"}B= $reuseportC= $spec->{"type"} eq "tcp"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3946 | $entry->{socket} = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3947 | LocalAddr => $spec->{host}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3948 | LocalPort => $spec->{bound_port}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3949 | Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3950 | Listen => $self->{listener_backlog}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3951 | 0 | ▶ | ReuseAddr => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3952 | ReusePort => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3953 | Blocking => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3954 | ) or die "Worker $worker_num: Cannot create listening socket: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3955 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3956 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3957 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3958 | # Build listener specs for the worker server constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3959 | 16 | my @listen_specs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3960 | 16 | for my $entry (@$listen_entries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3961 | 16 | my $s = $entry->{spec}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3962 | 16 | ▶ | if ($s->{type} eq 'unix') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3963 | 0 | ▶ | push @listen_specs, { socket => $s->{path}, (defined $s->{socket_mode} ? (socket_mode => $s->{socket_mode}) : ()) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3964 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3965 | 16 | ▶ | push @listen_specs, { host => $s->{host}, port => $s->{port}, ($s->{ssl} ? (ssl => $s->{ssl}) : ()) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3966 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3967 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3968 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3969 | # Create a fresh server instance for this worker (single-worker mode) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3970 | my $worker_server = PAGI::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3971 | app => $self->{app}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3972 | listen => \@listen_specs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3973 | ssl => $self->{ssl}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3974 | http2 => $self->{http2}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3975 | extensions => $self->{extensions}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3976 | access_log => $self->{access_log}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3977 | log_level => $self->{log_level}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3978 | timeout => $self->{timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3979 | max_header_size => $self->{max_header_size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3980 | max_header_count => $self->{max_header_count}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3981 | max_body_size => $self->{max_body_size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3982 | max_requests => $self->{max_requests}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3983 | shutdown_timeout => $self->{shutdown_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3984 | request_timeout => $self->{request_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3985 | ws_idle_timeout => $self->{ws_idle_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3986 | sse_idle_timeout => $self->{sse_idle_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3987 | sync_file_threshold => $self->{sync_file_threshold}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3988 | max_receive_queue => $self->{max_receive_queue}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3989 | max_ws_frame_size => $self->{max_ws_frame_size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3990 | write_high_watermark => $self->{write_high_watermark}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3991 | write_low_watermark => $self->{write_low_watermark}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3992 | max_connections => $self->{max_connections}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3993 | heartbeat_timeout => $self->{heartbeat_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3994 | lifespan_mode => $self->{lifespan_mode}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3995 | lifespan_startup_timeout => $self->{lifespan_startup_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3996 | loop_type => $self->{loop_type}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3997 | 16 | workers => 0, # Single-worker mode in worker process | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3998 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3999 | 16 | $worker_server->{is_worker} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4000 | 16 | $worker_server->{worker_num} = $worker_num; # Store for lifespan scope | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4001 | 16 | $worker_server->{_request_count} = 0; # Track requests handled | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4002 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4003 | # Set bound_port from first TCP listener's socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4004 | 16 | for my $entry (@$listen_entries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4005 | 16 | ▶ | if ($entry->{spec}{type} eq 'tcp' && $entry->{socket} && $entry->{socket}->can('sockport')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$entry->{"spec"}{"type"} eq "tcp" and $entry->{"socket"} and $entry->{"socket"}->can("sockport")
$entry->{"spec"}{"type"} eq "tcp" and $entry->{"socket"}
MC/DC0%0/3
$entry->{"spec"}{"type"} eq "tcp" and $entry->{"socket"} and $entry->{"socket"}->can("sockport")
$entry->{"spec"}{"type"} eq "tcp"
$entry->{"socket"}
$entry->{"socket"}->can("sockport")
Truth table25%1/4
A and B and C
A= $entry->{"spec"}{"type"} eq "tcp"B= $entry->{"socket"}C= $entry->{"socket"}->can("sockport")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4006 | 16 | $worker_server->{bound_port} = $entry->{socket}->sockport; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4007 | 16 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4008 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4009 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4010 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4011 | # Create the worker's own event loop now, respecting loop_type the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4012 | # way the single-process run() path does (see _create_loop). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4013 | 16 | my $loop = $worker_server->_create_loop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4014 | 16 | $loop->add($worker_server); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4015 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4016 | # Build SSL config for this worker (each worker gets its own SSL context post-fork) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4017 | 16 | my $ssl_params = $worker_server->_build_ssl_config; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4018 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4019 | # Set up graceful shutdown on SIGTERM using IO::Async's signal watching | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4020 | # (raw $SIG handlers don't work reliably when the loop is running) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4021 | # Note: Windows doesn't support Unix signals, so this is skipped there | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4022 | # Note: We do NOT set up watch_signal(INT) here - workers inherit $SIG{INT}='IGNORE' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4023 | # from parent, so they ignore SIGINT (including Ctrl-C). Parent sends SIGTERM. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4024 | 16 | unless (WIN32) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4025 | 16 | my $shutdown_triggered = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4026 | 12 | ▶ | $loop->watch_signal(TERM => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4027 | 12 | ▶ | return if $shutdown_triggered; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4028 | 12 | $shutdown_triggered = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4029 | $worker_server->adopt_future( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4030 | 12 | ▶ | $worker_server->shutdown->on_done(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4031 | 12 | $loop->stop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4032 | 0 | ▶ | })->on_fail(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4033 | 0 | my ($error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4034 | 0 | $worker_server->_log(error => "shutdown error: $error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4035 | 0 | $loop->stop; # Still stop even on error | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4036 | }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4037 | 12 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4038 | 16 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4039 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4040 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4041 | # Run lifespan startup using a proper async wrapper | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4042 | 16 | my $startup_done = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4043 | 16 | my $startup_error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4044 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4045 | 16 | ▶ | my $startup_future = (async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4046 | 16 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4047 | 16 | my $startup_result = await $worker_server->_run_lifespan_startup; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4048 | 16 | ▶ | if (!$startup_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4049 | 4 | ▶ | $startup_error = $startup_result->{message} // 'Lifespan startup failed'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$startup_result->{"message"} // "Lifespan startup failed"
MC/DC0%0/1
$startup_result->{"message"} // "Lifespan startup failed"
$startup_result->{"message"}
Truth table50%1/2
A
A= $startup_result->{"message"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4050 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4051 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4052 | 16 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4053 | 0 | $startup_error = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4054 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4055 | 16 | $startup_done = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4056 | 16 | ▶ | $loop->stop if $startup_error; # Stop loop on error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4057 | 16 | })->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4058 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4059 | # Use adopt_future instead of retain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4060 | 16 | $worker_server->adopt_future($startup_future); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4061 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4062 | # Run the loop briefly to let async startup complete | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4063 | 16 | ▶ | $loop->loop_once while !$startup_done; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4064 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4065 | 16 | ▶ | if ($startup_error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4066 | 4 | $self->_log(error => "Worker $worker_num ($$): startup failed: $startup_error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4067 | 4 | for my $entry (@$listen_entries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4068 | 4 | ▶ | close($entry->{socket}) if $entry->{socket}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4069 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4070 | 4 | exit(2); # Exit code 2 = startup failure (don't respawn) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4071 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4072 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4073 | # Create IO::Async::Listener for each inherited socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4074 | 12 | weaken(my $weak_server = $worker_server); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4075 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4076 | 12 | for my $entry (@$listen_entries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4077 | 12 | ▶ | next unless $entry->{socket}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4078 | 12 | my $spec = $entry->{spec}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4079 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4080 | # Build SSL config for TCP listeners if needed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4081 | 12 | ▶ | my $use_ssl = ($ssl_params && $spec->{type} eq 'tcp'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$ssl_params && $spec->{'type'} eq 'tcp'
MC/DC50%1/2
$ssl_params && $spec->{'type'} eq 'tcp'
$ssl_params
$spec->{'type'} eq 'tcp'
Truth table66%2/3
A && B
A= $ssl_paramsB= $spec->{'type'} eq 'tcp'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4082 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4083 | my $listener = IO::Async::Listener->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4084 | handle => $entry->{socket}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4085 | 7 | ▶ | on_stream => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4086 | 7 | my ($listener, $stream) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4087 | 7 | ▶ | return unless $weak_server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4088 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4089 | 7 | ▶ | if ($use_ssl) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4090 | $loop->SSL_upgrade( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4091 | handle => $stream, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4092 | SSL_server => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4093 | SSL_reuse_ctx => $worker_server->{_ssl_ctx}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4094 | 3 | ▶ | )->on_done(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4095 | 3 | ▶ | $weak_server->_on_connection($stream, $spec) if $weak_server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4096 | 0 | ▶ | })->on_fail(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4097 | 0 | my ($failure) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4098 | 0 | ▶ | $weak_server->_log(debug => "SSL handshake failed: $failure") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4099 | if $weak_server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4100 | 3 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4101 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4102 | 4 | $weak_server->_on_connection($stream, $spec); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4104 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4105 | 12 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4107 | 12 | $worker_server->add_child($listener); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4109 | # Configure accept error handler - try but ignore if it fails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4110 | 12 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4111 | $listener->configure( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4112 | 0 | ▶ | on_accept_error => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4113 | 0 | my ($listener, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4114 | 0 | ▶ | return unless $weak_server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4115 | 0 | $weak_server->_on_accept_error($error); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4116 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4117 | 12 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4118 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4119 | # Silently ignore configuration errors in workers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4122 | # Set up heartbeat writer: periodically signal liveness to parent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4123 | 12 | ▶ | if ($heartbeat_wr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4124 | 12 | ▶ | my $interval = ($worker_server->{heartbeat_timeout} || 50) / 5; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$worker_server->{'heartbeat_timeout'} || 50
MC/DC0%0/1
$worker_server->{'heartbeat_timeout'} || 50
$worker_server->{'heartbeat_timeout'}
Truth table50%1/2
A
A= $worker_server->{'heartbeat_timeout'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4125 | my $hb_timer = IO::Async::Timer::Periodic->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4126 | interval => $interval, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4127 | 0 | ▶ | on_tick => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4128 | 0 | syswrite($heartbeat_wr, "\x00", 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4129 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4130 | 12 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4131 | 12 | $worker_server->add_child($hb_timer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4132 | 12 | $hb_timer->start; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4135 | 12 | $worker_server->{running} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4137 | # Run the event loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4138 | 12 | $loop->run; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4140 | # Clean up FDs before exit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4141 | 12 | ▶ | close($heartbeat_wr) if $heartbeat_wr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4142 | 12 | for my $entry (@$listen_entries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4143 | 12 | ▶ | close($entry->{socket}) if $entry->{socket}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4145 | 12 | exit(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4148 | 304 | ▶ | sub _on_connection { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_on_connection: called CC=13 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4149 | 304 | my ($self, $stream, $listener_spec) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4151 | 304 | weaken(my $weak_self = $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4153 | # Check if we're at capacity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4154 | 304 | my $max = $self->effective_max_connections; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4155 | 304 | ▶ | if ($self->connection_count >= $max) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4156 | # Over capacity - send 503 and close | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4157 | 3 | $self->_send_503_and_close($stream); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4158 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4161 | # Detect ALPN-negotiated protocol from TLS handle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4162 | 301 | my $alpn_protocol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4163 | 301 | ▶ | if ($self->{tls_enabled} && $self->{http2_enabled}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"tls_enabled"} and $self->{"http2_enabled"}
MC/DC100%2/2
$self->{"tls_enabled"} and $self->{"http2_enabled"}
$self->{"tls_enabled"}
$self->{"http2_enabled"}
Truth table100%3/3
A and B
A= $self->{"tls_enabled"}B= $self->{"http2_enabled"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4164 | 1 | ▶ | my $handle = $stream->write_handle // $stream->read_handle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$stream->write_handle // $stream->read_handle
MC/DC0%0/2
$stream->write_handle // $stream->read_handle
$stream->write_handle
$stream->read_handle
Truth table33%1/3
A // B
A= $stream->write_handleB= $stream->read_handle
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4165 | 1 | ▶ | if ($handle && $handle->can('alpn_selected')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$handle and $handle->can("alpn_selected")
MC/DC0%0/2
$handle and $handle->can("alpn_selected")
$handle
$handle->can("alpn_selected")
Truth table33%1/3
A and B
A= $handleB= $handle->can("alpn_selected")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4166 | 1 | $alpn_protocol = eval { $handle->alpn_selected }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4170 | my $conn = PAGI::Server::Connection->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4171 | stream => $stream, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4172 | app => $self->{app}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4173 | protocol => $self->{protocol}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4174 | server => $self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4175 | extensions => $self->{extensions}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4176 | state => $self->{state}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4177 | tls_enabled => $self->{tls_enabled} // 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4178 | timeout => $self->{timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4179 | request_timeout => $self->{request_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4180 | ws_idle_timeout => $self->{ws_idle_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4181 | sse_idle_timeout => $self->{sse_idle_timeout}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4182 | max_body_size => $self->{max_body_size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4183 | access_log => $self->{access_log}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4184 | _access_log_formatter => $self->{_access_log_formatter}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4185 | max_receive_queue => $self->{max_receive_queue}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4186 | max_ws_frame_size => $self->{max_ws_frame_size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4187 | sync_file_threshold => $self->{sync_file_threshold}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4188 | validate_events => $self->{validate_events}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4189 | write_high_watermark => $self->{write_high_watermark}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4190 | write_low_watermark => $self->{write_low_watermark}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4191 | transport_type => ($listener_spec && $listener_spec->{type}) // 'tcp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4192 | transport_path => ($listener_spec ? $listener_spec->{path} : undef), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4193 | ($self->{http2_enabled} ? ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4194 | h2_protocol => $self->{http2_protocol}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4195 | alpn_protocol => $alpn_protocol, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4196 | 301 | ▶ | h2c_enabled => $self->{h2c_enabled} // 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition55%5/9
$self->{"tls_enabled"} // 0
$listener_spec && $listener_spec->{"type"} // "tcp"
$listener_spec and $listener_spec->{"type"}
$self->{"h2c_enabled"} // 0
MC/DC100%1/1
$self->{"tls_enabled"} // 0
$self->{"tls_enabled"}
MC/DC0%0/2
$listener_spec && $listener_spec->{"type"} // "tcp"
$listener_spec
$listener_spec->{"type"}
MC/DC0%0/1
$self->{"h2c_enabled"} // 0
$self->{"h2c_enabled"}
Truth table100%2/2
A
A= $self->{"tls_enabled"}
Truth table33%1/3
A and B
A= $listener_specB= $listener_spec->{"type"}
Truth table50%1/2
A
A= $self->{"h2c_enabled"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4197 | ) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4198 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4200 | # Track the connection (O(1) hash insert) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4201 | 301 | $self->{connections}{refaddr($conn)} = $conn; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4203 | # Configure stream with callbacks BEFORE adding to loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4204 | 301 | $conn->start; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4206 | # Add stream to the loop so it can read/write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4207 | 301 | $self->add_child($stream); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4208 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4210 | 3 | ▶ | sub _send_503_and_close { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_send_503_and_close: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4211 | 3 | my ($self, $stream) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4213 | 3 | my $body = "503 Service Unavailable - Server at capacity\r\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4214 | my $response = join("\r\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4215 | "HTTP/1.1 503 Service Unavailable", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4216 | "Content-Type: text/plain", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4217 | "Content-Length: " . length($body), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4218 | "Connection: close", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4219 | "Retry-After: 5", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4220 | "Date: " . $self->{protocol}->format_date, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4221 | 3 | "", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4222 | $body | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4223 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4225 | # Configure stream with minimal on_read handler (required by IO::Async) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4226 | $stream->configure( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4227 | 2 | ▶ | on_read => sub { 0 }, # Ignore any incoming data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4228 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4230 | # Add stream to loop so it can write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4231 | 3 | $self->add_child($stream); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4233 | # Write response and close | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4234 | 3 | $stream->write($response); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4235 | 3 | $stream->close_when_empty; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4237 | 3 | $self->_log(warn => "Connection rejected: at capacity (" . $self->connection_count . "/" . $self->effective_max_connections . ")"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4238 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4240 | 0 | ▶ | sub _on_accept_error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_on_accept_error: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4241 | 0 | my ($self, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4243 | # EMFILE = "Too many open files" - we're out of file descriptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4244 | # ENFILE = System-wide FD limit reached | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4245 | 0 | ▶ | if ($error =~ /Too many open files|EMFILE|ENFILE/i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4246 | # Only log the first EMFILE in a burst (when we're not already paused) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4247 | 0 | ▶ | unless ($self->{_accept_paused}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4248 | 0 | $self->_log(warn => "Accept error (FD exhaustion): $error - pausing accept for 100ms"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4249 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4251 | # Pause accepting for a short time to let connections drain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4252 | 0 | $self->_pause_accepting(0.1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4253 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4254 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4255 | # Log other accept errors but don't crash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4256 | 0 | $self->_log(error => "Accept error: $error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4257 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4258 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4260 | 0 | ▶ | sub _pause_accepting { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pause_accepting: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4261 | 0 | my ($self, $duration) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4263 | 0 | ▶ | return if $self->{_accept_paused}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4264 | 0 | $self->{_accept_paused} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4266 | # Cancel any existing timer before creating new one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4267 | 0 | ▶ | if ($self->{_accept_pause_timer}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4268 | 0 | $self->loop->unwatch_time($self->{_accept_pause_timer}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4269 | 0 | delete $self->{_accept_pause_timer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4272 | # Temporarily disable all listeners | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4273 | 0 | ▶ | for my $entry (@{$self->{_listen_entries} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"_listen_entries"} // []
MC/DC0%0/1
$self->{"_listen_entries"} // []
$self->{"_listen_entries"}
Truth table0%0/2
A
A= $self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4274 | 0 | my $listener = $entry->{listener}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4275 | 0 | ▶ | if ($listener && $listener->read_handle) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$listener and $listener->read_handle
MC/DC0%0/2
$listener and $listener->read_handle
$listener
$listener->read_handle
Truth table0%0/3
A and B
A= $listenerB= $listener->read_handle
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4276 | 0 | $listener->want_readready(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4278 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4279 | # Backward compat: also pause $self->{listener} if not in entries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4280 | 0 | ▶ | if ($self->{listener} && !$self->{_listen_entries}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"listener"} and not $self->{"_listen_entries"}
MC/DC0%0/2
$self->{"listener"} and not $self->{"_listen_entries"}
$self->{"listener"}
not $self->{"_listen_entries"}
Truth table0%0/3
A and not(B)
A= $self->{"listener"}B= not $self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4281 | 0 | ▶ | $self->{listener}->want_readready(0) if $self->{listener}->read_handle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4282 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4284 | # Re-enable after duration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4285 | 0 | weaken(my $weak_self = $self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4286 | 0 | ▶ | my $timer_id = $self->loop->watch_time(after => $duration, code => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4287 | 0 | ▶ | return unless $weak_self && $weak_self->{running}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$weak_self and $weak_self->{"running"}
MC/DC0%0/2
$weak_self and $weak_self->{"running"}
$weak_self
$weak_self->{"running"}
Truth table0%0/3
A and B
A= $weak_selfB= $weak_self->{"running"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4288 | 0 | $weak_self->{_accept_paused} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4289 | 0 | delete $weak_self->{_accept_pause_timer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4291 | # Resume all listeners | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4292 | 0 | ▶ | for my $entry (@{$weak_self->{_listen_entries} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$weak_self->{"_listen_entries"} // []
MC/DC0%0/1
$weak_self->{"_listen_entries"} // []
$weak_self->{"_listen_entries"}
Truth table0%0/2
A
A= $weak_self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4293 | 0 | my $listener = $entry->{listener}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4294 | 0 | ▶ | if ($listener && $listener->read_handle) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$listener and $listener->read_handle
MC/DC0%0/2
$listener and $listener->read_handle
$listener
$listener->read_handle
Truth table0%0/3
A and B
A= $listenerB= $listener->read_handle
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4295 | 0 | $listener->want_readready(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4297 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4298 | # Backward compat | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4299 | 0 | ▶ | if ($weak_self->{listener} && !$weak_self->{_listen_entries}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$weak_self->{"listener"} and not $weak_self->{"_listen_entries"}
MC/DC0%0/2
$weak_self->{"listener"} and not $weak_self->{"_listen_entries"}
$weak_self->{"listener"}
not $weak_self->{"_listen_entries"}
Truth table0%0/3
A and not(B)
A= $weak_self->{"listener"}B= not $weak_self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4300 | 0 | ▶ | $weak_self->{listener}->want_readready(1) if $weak_self->{listener}->read_handle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4301 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4303 | 0 | $weak_self->_log(debug => "Accept resumed after FD exhaustion pause"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4304 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4306 | 0 | $self->{_accept_pause_timer} = $timer_id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4307 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4309 | # Called when a request completes (for max_requests tracking) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4310 | 527 | ▶ | sub _on_request_complete { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_on_request_complete: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4311 | 527 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4313 | 527 | ▶ | return unless $self->{is_worker}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4314 | 10 | ▶ | return unless $self->{max_requests} && $self->{max_requests} > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"max_requests"} and $self->{"max_requests"} > 0
MC/DC50%1/2
$self->{"max_requests"} and $self->{"max_requests"} > 0
$self->{"max_requests"}
$self->{"max_requests"} > 0
Truth table66%2/3
A and B
A= $self->{"max_requests"}B= $self->{"max_requests"} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4316 | 5 | $self->{_request_count}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4318 | 5 | ▶ | if ($self->{_request_count} >= $self->{max_requests}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4319 | 2 | ▶ | return if $self->{_max_requests_shutdown_triggered}; # Prevent duplicate shutdowns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4320 | 2 | $self->{_max_requests_shutdown_triggered} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4321 | 2 | $self->_log(info => "reached max_requests ($self->{max_requests}), shutting down"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4322 | # Initiate graceful shutdown (finish current connections, then exit) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4323 | $self->adopt_future( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4324 | 2 | ▶ | $self->shutdown->on_done(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4325 | 2 | $self->loop->stop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4326 | 0 | ▶ | })->on_fail(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4327 | 0 | my ($error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4328 | 0 | $self->_log(error => "max_requests shutdown error: $error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4329 | 0 | $self->loop->stop; # Still stop even on error | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4330 | }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4331 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4332 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4333 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4335 | # Lifespan Protocol Implementation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4337 | 300 | ▶ | async sub _run_lifespan_startup { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_lifespan_startup: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4338 | 300 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4340 | # Create lifespan scope | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4341 | my $scope = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4342 | type => 'lifespan', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4343 | pagi => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4344 | version => '0.5', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4345 | spec_version => '0.3', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4346 | is_worker => $self->{is_worker} // 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4347 | worker_num => $self->{worker_num}, # undef for single-worker, 1-N for multi-worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4348 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4349 | state => $self->{state}, # App can populate this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4350 | 300 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"is_worker"} // 0
MC/DC0%0/1
$self->{"is_worker"} // 0
$self->{"is_worker"}
Truth table50%1/2
A
A= $self->{"is_worker"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4352 | # Create receive/send for lifespan protocol | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4353 | 300 | my @send_queue; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4354 | my $receive_pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4355 | 300 | my $startup_complete = Future->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4357 | 300 | $self->{lifespan_phase} = 'startup_pending'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4359 | # $receive for the app - returns events from the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4360 | 369 | ▶ | my $receive = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4361 | 369 | ▶ | if (@send_queue) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4362 | 197 | return Future->done(shift @send_queue); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4363 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4364 | 172 | $receive_pending = Future->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4365 | 172 | return $receive_pending; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4366 | 300 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4368 | # $send for the app - handles app responses | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4369 | 383 | ▶ | my $send = async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=28.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4370 | 383 | my ($event) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4371 | 383 | ▶ | my $type = $event->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"type"} // ""
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4373 | 383 | PAGI::Server::EventValidator::validate_lifespan_send($event); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4374 | $self->{lifespan_phase} = PAGI::Server::EventValidator::advance_lifespan( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4375 | 344 | $self->{lifespan_phase}, $event); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4377 | 342 | ▶ | if ($type eq 'lifespan.startup.complete') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4378 | 173 | $startup_complete->done({ success => 1 }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4380 | elsif ($type eq 'lifespan.startup.failed') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4381 | 4 | ▶ | my $message = $event->{message} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"message"} // ""
MC/DC0%0/1
$event->{"message"} // ""
$event->{"message"}
Truth table50%1/2
A
A= $event->{"message"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4382 | 4 | $startup_complete->done({ success => 0, message => $message }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4383 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4384 | elsif ($type eq 'lifespan.shutdown.complete') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4385 | # Store for shutdown handling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4386 | 165 | $self->{shutdown_complete} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4387 | 165 | ▶ | if ($self->{shutdown_pending}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4388 | 165 | $self->{shutdown_pending}->done({ success => 1 }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4389 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4390 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4391 | elsif ($type eq 'lifespan.shutdown.failed') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4392 | 0 | ▶ | my $message = $event->{message} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$event->{"message"} // ""
MC/DC0%0/1
$event->{"message"} // ""
$event->{"message"}
Truth table0%0/2
A
A= $event->{"message"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4393 | 0 | $self->{shutdown_complete} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4394 | 0 | ▶ | if ($self->{shutdown_pending}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4395 | 0 | $self->{shutdown_pending}->done({ success => 0, message => $message }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4396 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4397 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4399 | 342 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4400 | 300 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4402 | # Queue the startup event | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4403 | 300 | push @send_queue, { type => 'lifespan.startup' }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4404 | 300 | ▶ | if ($receive_pending && !$receive_pending->is_ready) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$receive_pending and not $receive_pending->is_ready
MC/DC0%0/2
$receive_pending and not $receive_pending->is_ready
$receive_pending
not $receive_pending->is_ready
Truth table33%1/3
A and not(B)
A= $receive_pendingB= not $receive_pending->is_ready
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4405 | 0 | my $f = $receive_pending; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4406 | 0 | $receive_pending = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4407 | 0 | $f->done(shift @send_queue); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4408 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4410 | # Store lifespan handlers for shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4411 | 300 | $self->{lifespan_receive} = $receive; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4412 | 300 | $self->{lifespan_send} = $send; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4413 | 300 | $self->{lifespan_send_queue} = \@send_queue; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4414 | 300 | $self->{lifespan_receive_pending} = \$receive_pending; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4416 | # Start the lifespan app handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4417 | # We run it in the background and wait for startup.complete | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4418 | 300 | ▶ | my $app_future = (async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4419 | 300 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4420 | 300 | await $self->{app}->($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4421 | 173 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4422 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4423 | 293 | ▶ | my $err = $ok ? undef : $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4425 | 293 | ▶ | if (!$startup_complete->is_ready) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4426 | # The app finished -- by returning cleanly OR by throwing -- without | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4427 | # ever signalling startup. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4428 | 122 | my $detail; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4429 | 122 | ▶ | if (defined $err) { ($detail = $err) =~ s/\s+\z//; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4430 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4431 | 122 | ▶ | if (($self->{lifespan_mode} // 'auto') eq 'on') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"lifespan_mode"} // "auto"
MC/DC0%0/1
$self->{"lifespan_mode"} // "auto"
$self->{"lifespan_mode"}
Truth table50%1/2
A
A= $self->{"lifespan_mode"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4432 | # 'on' requires lifespan: a decline (a raise, or a clean return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4433 | # that never signalled startup) is a fatal startup failure rather | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4434 | # than "continue without it". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4435 | 3 | ▶ | my $why = defined $detail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4436 | ? "the application raised: $detail" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4437 | : "the application returned without signalling startup"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4438 | 3 | $startup_complete->done({ success => 0, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4439 | message => "lifespan_mode is 'on' but $why" }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4440 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4441 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4442 | # auto: continue without lifespan. This matches Uvicorn/Hypercorn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4443 | # "auto"; an app may decline by returning on the lifespan scope or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4444 | # by dying on it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4445 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4446 | # Report it either way: an application whose lifespan handler is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4447 | # silently not being reached has nothing else to tell it apart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4448 | # from one that never had a handler. The raised message is kept | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4449 | # because a genuine startup failure can wear a decline as a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4450 | # disguise -- that is the blind spot strict mode exists to close. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4451 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4452 | # The file and line Perl appends to a bare die are dropped: an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4453 | # assertion on the scope type is the idiomatic way to decline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4454 | # (the spec resolves a raise and a clean return identically), so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4455 | # a location here points at ordinary, correct code. Strict mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4456 | # keeps them, because there the same raise is a fatal failure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4457 | 119 | my $shown = $detail; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4458 | 119 | ▶ | $shown =~ s/ at \S+ line \d+\.?\z// if defined $shown; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4459 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4460 | 119 | my $msg = 'not supported, continuing without it'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4461 | 119 | ▶ | $msg .= " (application raised: $shown)" if defined $shown; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4462 | 119 | $self->_startup_note(lifespan => $msg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4463 | 119 | $startup_complete->done({ success => 1, lifespan_supported => 0 }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4464 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4465 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4466 | elsif (defined $err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4467 | # The app's long-lived lifespan task failed AFTER startup completed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4468 | # Surface it at error level rather than swallowing it silently, so a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4469 | # crashed background task is visible to operators. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4470 | 2 | $self->_log(error => "Lifespan app failed after startup: $err"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4471 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4472 | # else: the app returned cleanly after startup completed -- the handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4473 | # simply exited early; nothing to do. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4474 | 300 | })->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4475 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4476 | # Keep the app future so we can trigger shutdown later | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4477 | 300 | $self->{lifespan_app_future} = $app_future; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4478 | # Use adopt_future instead of retain for proper error handling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4479 | 300 | $self->adopt_future($app_future); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4480 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4481 | # Wait for startup complete, bounded by lifespan_startup_timeout so a hung | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4482 | # lifespan handler (one that neither signals startup nor returns) cannot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4483 | # block the server forever. The constructor and configure() both reject a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4484 | # value of 0, so this bound always applies -- the PAGI Lifespan spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4485 | # requires that a server must not block startup indefinitely. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4486 | # without_cancel keeps $startup_complete pending on timeout, so a late | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4487 | # signal cannot croak. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4488 | 300 | ▶ | my $startup_timeout = $self->{lifespan_startup_timeout} // 30; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"lifespan_startup_timeout"} // 30
MC/DC0%0/1
$self->{"lifespan_startup_timeout"} // 30
$self->{"lifespan_startup_timeout"}
Truth table50%1/2
A
A= $self->{"lifespan_startup_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4489 | my $timeout_f = $self->loop->delay_future(after => $startup_timeout) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4490 | 300 | ▶ | ->then(sub { Future->done(undef) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4491 | 300 | my $result = await Future->wait_any($startup_complete->without_cancel, $timeout_f); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4492 | 300 | ▶ | if (!defined $result) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4493 | 1 | my $message = "Lifespan startup timed out after ${startup_timeout}s"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4494 | 1 | $self->_log(error => $message); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4495 | 1 | return { success => 0, message => $message }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4496 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4497 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4498 | # Track if lifespan is supported | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4499 | 299 | ▶ | $self->{lifespan_supported} = $result->{lifespan_supported} // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$result->{"lifespan_supported"} // 1
MC/DC100%1/1
$result->{"lifespan_supported"} // 1
$result->{"lifespan_supported"}
Truth table100%2/2
A
A= $result->{"lifespan_supported"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4500 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4501 | 299 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4502 | 153 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4503 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4504 | 285 | ▶ | async sub _run_lifespan_shutdown { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_lifespan_shutdown: called CC=9 SCAR=23.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4505 | 285 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4506 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4507 | # If lifespan is not supported or no lifespan was started, just return success | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4508 | 285 | ▶ | return { success => 1 } unless $self->{lifespan_supported}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4509 | 166 | ▶ | return { success => 1 } unless $self->{lifespan_send_queue}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4510 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4511 | 166 | $self->{shutdown_pending} = $self->loop->new_future; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4513 | # Queue the shutdown event | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4514 | 166 | my $send_queue = $self->{lifespan_send_queue}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4515 | 166 | my $receive_pending_ref = $self->{lifespan_receive_pending}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4516 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4517 | 166 | $self->{lifespan_phase} = 'shutdown_pending'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4518 | 166 | push @$send_queue, { type => 'lifespan.shutdown' }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4519 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4520 | # Trigger pending receive if waiting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4521 | 166 | ▶ | if ($$receive_pending_ref && !$$receive_pending_ref->is_ready) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$$receive_pending_ref and not $$receive_pending_ref->is_ready
MC/DC0%0/2
$$receive_pending_ref and not $$receive_pending_ref->is_ready
$$receive_pending_ref
not $$receive_pending_ref->is_ready
Truth table33%1/3
A and not(B)
A= $$receive_pending_refB= not $$receive_pending_ref->is_ready
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4522 | 166 | my $f = $$receive_pending_ref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4523 | 166 | $$receive_pending_ref = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4524 | 166 | $f->done(shift @$send_queue); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4525 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4526 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4527 | # Wait for shutdown complete (with timeout to prevent hanging) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4528 | 166 | ▶ | my $timeout = $self->{shutdown_timeout} // 30; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"shutdown_timeout"} // 30
MC/DC0%0/1
$self->{"shutdown_timeout"} // 30
$self->{"shutdown_timeout"}
Truth table50%1/2
A
A= $self->{"shutdown_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4529 | 166 | my $timeout_f = $self->loop->delay_future(after => $timeout); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4530 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4531 | 166 | my $result = await Future->wait_any($self->{shutdown_pending}, $timeout_f); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4532 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4533 | # If timeout won, return failure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4534 | 166 | ▶ | if ($timeout_f->is_ready && !$self->{shutdown_pending}->is_ready) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$timeout_f->is_ready and not $self->{"shutdown_pending"}->is_ready
MC/DC0%0/2
$timeout_f->is_ready and not $self->{"shutdown_pending"}->is_ready
$timeout_f->is_ready
not $self->{"shutdown_pending"}->is_ready
Truth table33%1/3
A and not(B)
A= $timeout_f->is_readyB= not $self->{"shutdown_pending"}->is_ready
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4535 | 0 | return { success => 0, message => "Lifespan shutdown timed out after ${timeout}s" }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4536 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4537 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4538 | 166 | ▶ | return $result // { success => 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$result // {"success", 1}
MC/DC100%1/1
$result // {"success", 1}
$result
Truth table100%2/2
A
A= $result
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4539 | 153 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4540 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4541 | 288 | ▶ | async sub shutdown { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shutdown: called CC=19 SCAR=31.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4542 | 288 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4544 | 288 | ▶ | return unless $self->{running}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4545 | 286 | $self->{running} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4546 | 286 | $self->{shutting_down} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4547 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4548 | # Cancel accept pause timer if active | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4549 | 286 | ▶ | if ($self->{_accept_pause_timer}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4550 | 0 | $self->loop->unwatch_time($self->{_accept_pause_timer}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4551 | 0 | delete $self->{_accept_pause_timer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4552 | 0 | $self->{_accept_paused} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4553 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4554 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4555 | # Stop accepting new connections on all listeners | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4556 | 286 | ▶ | for my $entry (@{$self->{_listen_entries} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_listen_entries"} // []
MC/DC100%1/1
$self->{"_listen_entries"} // []
$self->{"_listen_entries"}
Truth table100%2/2
A
A= $self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4557 | 275 | eval { $self->remove_child($entry->{listener}) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4558 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4559 | 286 | $self->{listener} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4560 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4561 | # Clean up PAGI_REUSE entries for sockets we created (not inherited) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4562 | 286 | ▶ | for my $entry (@{$self->{_listen_entries} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_listen_entries"} // []
MC/DC100%1/1
$self->{"_listen_entries"} // []
$self->{"_listen_entries"}
Truth table100%2/2
A
A= $self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4563 | 275 | my $key = $entry->{spec}{_reuse_key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4564 | 275 | ▶ | if ($key && !$self->{_hot_restart_in_progress} && defined $ENV{PAGI_REUSE}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$key and not $self->{"_hot_restart_in_progress"} and defined $ENV{"PAGI_REUSE"}
$key and not $self->{"_hot_restart_in_progress"}
MC/DC33%1/3
$key and not $self->{"_hot_restart_in_progress"} and defined $ENV{"PAGI_REUSE"}
$key
not $self->{"_hot_restart_in_progress"}
defined $ENV{"PAGI_REUSE"}
Truth table50%2/4
A and not(B) and C
A= $keyB= not $self->{"_hot_restart_in_progress"}C= defined $ENV{"PAGI_REUSE"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4565 | 272 | $ENV{PAGI_REUSE} =~ s/(?:^|,)\Q$key\E//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4566 | delete $OWNED_REUSE_FDS{ $entry->{spec}{_reuse_fd} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4567 | 272 | ▶ | if defined $entry->{spec}{_reuse_fd}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4568 | 272 | ▶ | $ENV{PAGI_REUSE} =~ s/^,// if defined $ENV{PAGI_REUSE}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4569 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4570 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4571 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4572 | # Clean up Unix socket files (only those we created, not inherited) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4573 | 286 | ▶ | for my $entry (@{$self->{_listen_entries} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_listen_entries"} // []
MC/DC100%1/1
$self->{"_listen_entries"} // []
$self->{"_listen_entries"}
Truth table100%2/2
A
A= $self->{"_listen_entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4574 | 275 | ▶ | if ($entry->{spec}{type} eq 'unix' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$entry->{"spec"}{"type"} eq "unix" and not $entry->{"spec"}{"_inherited"} and -e $entry->{"spec"}{"path"}
$entry->{"spec"}{"type"} eq "unix" and not $entry->{"spec"}{"_inherited"}
MC/DC66%2/3
$entry->{"spec"}{"type"} eq "unix" and not $entry->{"spec"}{"_inherited"} and -e $entry->{"spec"}{"path"}
$entry->{"spec"}{"type"} eq "unix"
not $entry->{"spec"}{"_inherited"}
-e $entry->{"spec"}{"path"}
Truth table75%3/4
A and not(B) and C
A= $entry->{"spec"}{"type"} eq "unix"B= not $entry->{"spec"}{"_inherited"}C= -e $entry->{"spec"}{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4575 | && !$entry->{spec}{_inherited} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4576 | && -e $entry->{spec}{path}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4577 | 7 | unlink $entry->{spec}{path}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4578 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4579 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4580 | 286 | $self->{_listen_entries} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4581 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4582 | # Wait for active connections to drain (graceful shutdown) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4583 | 286 | await $self->_drain_connections; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4585 | # Run lifespan shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4586 | 285 | my $shutdown_result = await $self->_run_lifespan_shutdown; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4587 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4588 | 285 | ▶ | if (!$shutdown_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4589 | 0 | ▶ | my $message = $shutdown_result->{message} // 'Lifespan shutdown failed'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$shutdown_result->{"message"} // "Lifespan shutdown failed"
MC/DC0%0/1
$shutdown_result->{"message"} // "Lifespan shutdown failed"
$shutdown_result->{"message"}
Truth table0%0/2
A
A= $shutdown_result->{"message"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4590 | 0 | $self->_log(warn => "PAGI Server shutdown warning: $message"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4591 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4592 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4593 | 285 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4594 | 153 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4595 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4596 | # Wait for active connections to complete, with timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4597 | # Uses event-driven approach: Connection._close() signals when last one closes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4598 | 286 | ▶ | async sub _drain_connections { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_drain_connections: called CC=11 SCAR=29.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4599 | 286 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4601 | 286 | ▶ | my $timeout = $self->{shutdown_timeout} // 30; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"shutdown_timeout"} // 30
MC/DC0%0/1
$self->{"shutdown_timeout"} // 30
$self->{"shutdown_timeout"}
Truth table50%1/2
A
A= $self->{"shutdown_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4602 | 286 | my $loop = $self->loop; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4603 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4604 | # First, close all idle connections immediately (not processing a request) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4605 | # Keep-alive connections waiting for next request should be closed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4606 | 286 | my @idle = grep { !$_->{handling_request} } values %{$self->{connections}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4607 | 286 | for my $conn (@idle) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4608 | 165 | $conn->_handle_disconnect_and_close('server_shutdown'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4609 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4610 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4611 | # Also close long-lived connections (SSE, WebSocket) immediately | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4612 | # These never become "idle" so would wait for full timeout otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4613 | 286 | ▶ | my @longlived = grep { $_->{sse_mode} || $_->{websocket_mode} } values %{$self->{connections}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$_->{'sse_mode'} || $_->{'websocket_mode'}
MC/DC100%2/2
$_->{'sse_mode'} || $_->{'websocket_mode'}
$_->{'sse_mode'}
$_->{'websocket_mode'}
Truth table100%3/3
A || B
A= $_->{'sse_mode'}B= $_->{'websocket_mode'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4614 | 286 | for my $conn (@longlived) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4615 | 12 | $conn->_handle_disconnect_and_close('server_shutdown'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4616 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4617 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4618 | # If all connections are now closed, we're done | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4619 | 286 | ▶ | return if keys %{$self->{connections}} == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4620 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4621 | # Create a Future that Connection._close() will resolve when last one closes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4622 | 1 | $self->{drain_complete} = $loop->new_future; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4623 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4624 | # Wait for either: all connections close OR timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4625 | 1 | my $timeout_f = $loop->delay_future(after => $timeout); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4626 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4627 | 1 | await Future->wait_any($self->{drain_complete}, $timeout_f); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4628 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4629 | # Brief pause to let any final socket writes flush | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4630 | # (stream->write is async; data may still be in kernel buffer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4631 | 1 | ▶ | await $loop->delay_future(after => 0.05) if keys %{$self->{connections}} == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4632 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4633 | # If timeout won (connections still remain), force close them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4634 | 0 | ▶ | if (keys %{$self->{connections}} > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4635 | 0 | my $remaining = scalar keys %{$self->{connections}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4636 | 0 | $self->_log(warn => "Shutdown timeout: force-closing $remaining active connections"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4637 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4638 | 0 | for my $conn (values %{$self->{connections}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4639 | 0 | ▶ | $conn->_close if $conn && $conn->can('_close'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$conn and $conn->can("_close")
MC/DC0%0/2
$conn and $conn->can("_close")
$conn
$conn->can("_close")
Truth table0%0/3
A and B
A= $connB= $conn->can("_close")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4640 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4641 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4642 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4643 | 0 | delete $self->{drain_complete}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4644 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4645 | 153 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4646 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4647 | # --- Access log format compiler --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4648 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4649 | 153 | my %ACCESS_LOG_PRESETS = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4650 | clf => '%h - - [%t] "%m %U%q" %s %ds', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4651 | combined => '%h - - [%t] "%r" %s %b "%{Referer}i" "%{User-Agent}i"', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4652 | common => '%h - - [%t] "%r" %s %b', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4653 | tiny => '%m %U%q %s %Dms', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4654 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4655 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4656 | 688 | ▶ | sub _compile_access_log_format { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compile_access_log_format: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4657 | 688 | my ($class_or_self, $format) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4658 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4659 | # Resolve preset names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4660 | 688 | ▶ | if (exists $ACCESS_LOG_PRESETS{$format}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4661 | 654 | $format = $ACCESS_LOG_PRESETS{$format}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4662 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4663 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4664 | # Parse format string into a list of fragments (closures or literal strings) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4665 | 688 | my @fragments; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4666 | 688 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4667 | 688 | my $len = length($format); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4668 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4669 | 688 | ▶ | while ($pos < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4670 | 8542 | my $ch = substr($format, $pos, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4671 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4672 | 8542 | ▶ | if ($ch eq '%') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4673 | 4611 | $pos++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4674 | 4611 | ▶ | last if $pos >= $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4675 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4676 | 4611 | my $next = substr($format, $pos, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4677 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4678 | 4611 | ▶ | if ($next eq '%') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4679 | # Literal percent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4680 | 1 | push @fragments, '%'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4681 | 1 | $pos++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4682 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4683 | elsif ($next eq '{') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4684 | # Header extraction: %{Name}i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4685 | 13 | my $end = index($format, '}', $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4686 | 13 | ▶ | die "Unterminated %{...} in access log format\n" if $end < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4687 | 13 | my $header_name = substr($format, $pos + 1, $end - $pos - 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4688 | 13 | $pos = $end + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4689 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4690 | # Must be followed by 'i' (request header) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4691 | 13 | ▶ | die "Expected 'i' after %{$header_name} in access log format\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$pos >= $len or substr($format, $pos, 1) ne "i"
MC/DC0%0/2
$pos >= $len or substr($format, $pos, 1) ne "i"
$pos >= $len
substr($format, $pos, 1) ne "i"
Truth table33%1/3
A or B
A= $pos >= $lenB= substr($format, $pos, 1) ne "i"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4692 | if $pos >= $len || substr($format, $pos, 1) ne 'i'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4693 | 13 | $pos++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4694 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4695 | 13 | my $lc_name = lc($header_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4696 | 11 | ▶ | push @fragments, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4697 | 11 | my ($info) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4698 | 11 | for my $h (@{$info->{request_headers}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4699 | 26 | ▶ | return $h->[1] if lc($h->[0]) eq $lc_name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4700 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4701 | 2 | return '-'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4702 | 13 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4703 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4704 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4705 | # Simple atom | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4706 | 4597 | my $atom = $next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4707 | 4597 | $pos++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4708 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4709 | 4597 | my $frag = _access_log_atom($atom); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4710 | 4596 | push @fragments, $frag; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4711 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4712 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4713 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4714 | # Literal text: collect until next % | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4715 | 3931 | my $next_pct = index($format, '%', $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4716 | 3931 | ▶ | if ($next_pct < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4717 | 654 | push @fragments, substr($format, $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4718 | 654 | $pos = $len; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4719 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4720 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4721 | 3277 | push @fragments, substr($format, $pos, $next_pct - $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4722 | 3277 | $pos = $next_pct; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4723 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4724 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4725 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4726 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4727 | # Build a single closure from fragments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4728 | 344 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4729 | 344 | my ($info) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4730 | 344 | ▶ | return join('', map { ref $_ ? $_->($info) : $_ } @fragments); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4731 | 687 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4732 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4733 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4734 | 4597 | ▶ | sub _access_log_atom { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_access_log_atom: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4735 | 4597 | my ($atom) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4736 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4737 | my %atoms = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4738 | 312 | ▶ | h => sub { $_[0]->{client_ip} // '-' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"client_ip"} // "-"
MC/DC0%0/1
$_[0]{"client_ip"} // "-"
$_[0]{"client_ip"}
Truth table50%1/2
A
A= $_[0]{"client_ip"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4739 | 1 | ▶ | l => sub { '-' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4740 | 1 | ▶ | u => sub { '-' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4741 | 310 | ▶ | t => sub { $_[0]->{timestamp} // '-' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"timestamp"} // "-"
MC/DC0%0/1
$_[0]{"timestamp"} // "-"
$_[0]{"timestamp"}
Truth table50%1/2
A
A= $_[0]{"timestamp"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4742 | 5 | ▶ | r => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4743 | 5 | my $i = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4744 | 5 | ▶ | my $uri = $i->{path} // '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$i->{"path"} // "/"
MC/DC0%0/1
$i->{"path"} // "/"
$i->{"path"}
Truth table50%1/2
A
A= $i->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4745 | 5 | my $qs = $i->{query}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4746 | 5 | ▶ | $uri .= "?$qs" if defined $qs && length $qs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $qs and length $qs
MC/DC50%1/2
defined $qs and length $qs
defined $qs
length $qs
Truth table66%2/3
A and B
A= defined $qsB= length $qs
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4747 | 5 | ▶ | sprintf('%s %s HTTP/%s', $i->{method} // '-', $uri, $i->{http_version} // '1.1'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$i->{"method"} // "-"
$i->{"http_version"} // "1.1"
MC/DC0%0/1
$i->{"method"} // "-"
$i->{"method"}
MC/DC0%0/1
$i->{"http_version"} // "1.1"
$i->{"http_version"}
Truth table50%1/2
A
A= $i->{"method"}
Truth table50%1/2
A
A= $i->{"http_version"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4748 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4749 | 307 | ▶ | m => sub { $_[0]->{method} // '-' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"method"} // "-"
MC/DC0%0/1
$_[0]{"method"} // "-"
$_[0]{"method"}
Truth table50%1/2
A
A= $_[0]{"method"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4750 | 307 | ▶ | U => sub { $_[0]->{path} // '/' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"path"} // "/"
MC/DC0%0/1
$_[0]{"path"} // "/"
$_[0]{"path"}
Truth table50%1/2
A
A= $_[0]{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4751 | 309 | ▶ | q => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4752 | 309 | my $qs = $_[0]->{query}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4753 | 309 | ▶ | (defined $qs && length $qs) ? "?$qs" : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $qs and length $qs
MC/DC100%2/2
defined $qs and length $qs
defined $qs
length $qs
Truth table100%3/3
A and B
A= defined $qsB= length $qs
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4754 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4755 | 1 | ▶ | H => sub { 'HTTP/' . ($_[0]->{http_version} // '1.1') }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"http_version"} // "1.1"
MC/DC0%0/1
$_[0]{"http_version"} // "1.1"
$_[0]{"http_version"}
Truth table50%1/2
A
A= $_[0]{"http_version"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4756 | 317 | ▶ | s => sub { $_[0]->{status} // '-' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"status"} // "-"
MC/DC0%0/1
$_[0]{"status"} // "-"
$_[0]{"status"}
Truth table50%1/2
A
A= $_[0]{"status"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4757 | 10 | ▶ | b => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4758 | 10 | ▶ | my $size = $_[0]->{size} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_[0]{"size"} // 0
MC/DC0%0/1
$_[0]{"size"} // 0
$_[0]{"size"}
Truth table50%1/2
A
A= $_[0]{"size"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4759 | 10 | ▶ | $size ? $size : '-'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4760 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4761 | 2 | ▶ | B => sub { $_[0]->{size} // 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"size"} // 0
MC/DC0%0/1
$_[0]{"size"} // 0
$_[0]{"size"}
Truth table50%1/2
A
A= $_[0]{"size"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4762 | 306 | ▶ | d => sub { sprintf('%.3f', $_[0]->{duration} // 0) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"duration"} // 0
MC/DC0%0/1
$_[0]{"duration"} // 0
$_[0]{"duration"}
Truth table50%1/2
A
A= $_[0]{"duration"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4763 | 4 | ▶ | D => sub { int(($_[0]->{duration} // 0) * 1_000_000) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"duration"} // 0
MC/DC0%0/1
$_[0]{"duration"} // 0
$_[0]{"duration"}
Truth table50%1/2
A
A= $_[0]{"duration"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4764 | 2 | ▶ | T => sub { int($_[0]->{duration} // 0) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"duration"} // 0
MC/DC0%0/1
$_[0]{"duration"} // 0
$_[0]{"duration"}
Truth table50%1/2
A
A= $_[0]{"duration"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4765 | 4597 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4766 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4767 | 4597 | ▶ | if (my $frag = $atoms{$atom}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4768 | 4596 | return $frag; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4769 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4770 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4771 | 1 | die "Unknown access log format atom '%$atom'\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4772 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4773 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4774 | 265 | ▶ | sub port { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
port: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4775 | 265 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4776 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4777 | 265 | ▶ | return $self->{bound_port} // $self->{port}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"bound_port"} // $self->{"port"}
MC/DC50%1/2
$self->{"bound_port"} // $self->{"port"}
$self->{"bound_port"}
$self->{"port"}
Truth table66%2/3
A // B
A= $self->{"bound_port"}B= $self->{"port"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4778 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4779 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4780 | 4 | ▶ | sub socket_path { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
socket_path: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4781 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4782 | 4 | ▶ | for my $listener (@{$self->{listeners} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"listeners"} // []
MC/DC0%0/1
$self->{"listeners"} // []
$self->{"listeners"}
Truth table50%1/2
A
A= $self->{"listeners"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4783 | 4 | ▶ | return $listener->{path} if $listener->{type} eq 'unix'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4784 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4785 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4786 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4787 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4788 | 5 | ▶ | sub listeners { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
listeners: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4789 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4790 | 5 | ▶ | return $self->{listeners} // []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"listeners"} // []
MC/DC0%0/1
$self->{"listeners"} // []
$self->{"listeners"}
Truth table50%1/2
A
A= $self->{"listeners"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4791 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4792 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4793 | 23 | ▶ | sub is_running { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_running: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4794 | 23 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4795 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4796 | 23 | ▶ | return $self->{running} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4797 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4798 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4799 | 314 | ▶ | sub connection_count { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connection_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4800 | 314 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4801 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4802 | 314 | return scalar keys %{$self->{connections}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4803 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4804 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4805 | 626 | ▶ | sub effective_max_connections { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
effective_max_connections: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4806 | 626 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4807 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4808 | # If explicitly set, use that; otherwise default to 1000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4809 | # (Same default as Mojolicious - simple, predictable, no platform-specific hacks) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4810 | return $self->{max_connections} && $self->{max_connections} > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4811 | ? $self->{max_connections} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4812 | 626 | ▶ | : 1000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"max_connections"} and $self->{"max_connections"} > 0
MC/DC50%1/2
$self->{"max_connections"} and $self->{"max_connections"} > 0
$self->{"max_connections"}
$self->{"max_connections"} > 0
Truth table66%2/3
A and B
A= $self->{"max_connections"}B= $self->{"max_connections"} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4813 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4814 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4815 | 153 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4816 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4817 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||