.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::Server::Prefork 3pm" .TH Mojo::Server::Prefork 3pm 2024-05-15 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Mojo::Server::Prefork \- Pre\-forking non\-blocking I/O HTTP and WebSocket server .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::Server::Prefork; \& \& my $prefork = Mojo::Server::Prefork\->new(listen => [\*(Aqhttp://*:8080\*(Aq]); \& $prefork\->unsubscribe(\*(Aqrequest\*(Aq)\->on(request => sub ($prefork, $tx) { \& \& # Request \& my $method = $tx\->req\->method; \& my $path = $tx\->req\->url\->path; \& \& # Response \& $tx\->res\->code(200); \& $tx\->res\->headers\->content_type(\*(Aqtext/plain\*(Aq); \& $tx\->res\->body("$method request for $path!"); \& \& # Resume transaction \& $tx\->resume; \& }); \& $prefork\->run; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mojo::Server::Prefork is a full featured, UNIX optimized, pre-forking non-blocking I/O HTTP and WebSocket server, built around the very well tested and reliable Mojo::Server::Daemon, with IPv6, TLS, SNI, UNIX domain socket, Comet (long polling), keep-alive and multiple event loop support. Note that the server uses signals for process management, so you should avoid modifying signal handlers in your applications. .PP For better scalability (epoll, kqueue) and to provide non-blocking name resolution, SOCKS5 as well as TLS support, the optional modules EV (4.32+), Net::DNS::Native (0.15+), IO::Socket::Socks (0.64+) and IO::Socket::SSL (1.84+) will be used automatically if possible. Individual features can also be disabled with the \f(CW\*(C`MOJO_NO_NNR\*(C'\fR, \&\f(CW\*(C`MOJO_NO_SOCKS\*(C'\fR and \f(CW\*(C`MOJO_NO_TLS\*(C'\fR environment variables. .PP See "DEPLOYMENT" in Mojolicious::Guides::Cookbook for more. .SH "MANAGER SIGNALS" .IX Header "MANAGER SIGNALS" The Mojo::Server::Prefork manager process can be controlled at runtime with the following signals. .SS "INT, TERM" .IX Subsection "INT, TERM" Shut down server immediately. .SS QUIT .IX Subsection "QUIT" Shut down server gracefully. .SS TTIN .IX Subsection "TTIN" Increase worker pool by one. .SS TTOU .IX Subsection "TTOU" Decrease worker pool by one. .SH "WORKER SIGNALS" .IX Header "WORKER SIGNALS" Mojo::Server::Prefork worker processes can be controlled at runtime with the following signals. .SS QUIT .IX Subsection "QUIT" Stop worker gracefully. .SH EVENTS .IX Header "EVENTS" Mojo::Server::Prefork inherits all events from Mojo::Server::Daemon and can emit the following new ones. .SS finish .IX Subsection "finish" .Vb 1 \& $prefork\->on(finish => sub ($prefork, $graceful) {...}); .Ve .PP Emitted when the server shuts down. .PP .Vb 3 \& $prefork\->on(finish => sub ($prefork, $graceful) { \& say $graceful ? \*(AqGraceful server shutdown\*(Aq : \*(AqServer shutdown\*(Aq; \& }); .Ve .SS heartbeat .IX Subsection "heartbeat" .Vb 1 \& $prefork\->on(heartbeat => sub ($prefork, $pid) {...}); .Ve .PP Emitted when a heartbeat message has been received from a worker. .PP .Vb 1 \& $prefork\->on(heartbeat => sub ($prefork, $pid) { say "Worker $pid has a heartbeat" }); .Ve .SS reap .IX Subsection "reap" .Vb 1 \& $prefork\->on(reap => sub ($prefork, $pid) {...}); .Ve .PP Emitted when a child process exited. .PP .Vb 1 \& $prefork\->on(reap => sub ($prefork, $pid) { say "Worker $pid stopped" }); .Ve .SS spawn .IX Subsection "spawn" .Vb 1 \& $prefork\->on(spawn => sub ($prefork, $pid) {...}); .Ve .PP Emitted when a worker process is spawned. .PP .Vb 1 \& $prefork\->on(spawn => sub ($prefork, $pid) { say "Worker $pid started" }); .Ve .SS wait .IX Subsection "wait" .Vb 1 \& $prefork\->on(wait => sub ($prefork) {...}); .Ve .PP Emitted when the manager starts waiting for new heartbeat messages. .PP .Vb 4 \& $prefork\->on(wait => sub ($prefork) { \& my $workers = $prefork\->workers; \& say "Waiting for heartbeat messages from $workers workers"; \& }); .Ve .SH ATTRIBUTES .IX Header "ATTRIBUTES" Mojo::Server::Prefork inherits all attributes from Mojo::Server::Daemon and implements the following new ones. .SS accepts .IX Subsection "accepts" .Vb 2 \& my $accepts = $prefork\->accepts; \& $prefork = $prefork\->accepts(100); .Ve .PP Maximum number of connections a worker is allowed to accept, before stopping gracefully and then getting replaced with a newly started worker, passed along to "max_accepts" in Mojo::IOLoop, defaults to \f(CW10000\fR. Setting the value to \f(CW0\fR will allow workers to accept new connections indefinitely. Note that up to half of this value can be subtracted randomly to improve load balancing, and to make sure that not all workers restart at the same time. .SS cleanup .IX Subsection "cleanup" .Vb 2 \& my $bool = $prefork\->cleanup; \& $prefork = $prefork\->cleanup($bool); .Ve .PP Delete "pid_file" automatically once it is not needed anymore, defaults to a true value. .SS graceful_timeout .IX Subsection "graceful_timeout" .Vb 2 \& my $timeout = $prefork\->graceful_timeout; \& $prefork = $prefork\->graceful_timeout(15); .Ve .PP Maximum amount of time in seconds stopping a worker gracefully may take before being forced, defaults to \f(CW120\fR. Note that this value should usually be a little larger than the maximum amount of time you expect any one request to take. .SS heartbeat_interval .IX Subsection "heartbeat_interval" .Vb 2 \& my $interval = $prefork\->heartbeat_interval; \& $prefork = $prefork\->heartbeat_interval(3); .Ve .PP Heartbeat interval in seconds, defaults to \f(CW5\fR. .SS heartbeat_timeout .IX Subsection "heartbeat_timeout" .Vb 2 \& my $timeout = $prefork\->heartbeat_timeout; \& $prefork = $prefork\->heartbeat_timeout(2); .Ve .PP Maximum amount of time in seconds before a worker without a heartbeat will be stopped gracefully, defaults to \f(CW50\fR. Note that this value should usually be a little larger than the maximum amount of time you expect any one operation to block the event loop. .SS pid_file .IX Subsection "pid_file" .Vb 2 \& my $file = $prefork\->pid_file; \& $prefork = $prefork\->pid_file(\*(Aq/tmp/prefork.pid\*(Aq); .Ve .PP Full path of process id file, defaults to \f(CW\*(C`prefork.pid\*(C'\fR in a temporary directory. .SS spare .IX Subsection "spare" .Vb 2 \& my $spare = $prefork\->spare; \& $prefork = $prefork\->spare(4); .Ve .PP Temporarily spawn up to this number of additional workers if there is a need, defaults to \f(CW2\fR. This allows for new workers to be started while old ones are still shutting down gracefully, drastically reducing the performance cost of worker restarts. .SS workers .IX Subsection "workers" .Vb 2 \& my $workers = $prefork\->workers; \& $prefork = $prefork\->workers(10); .Ve .PP Number of worker processes, defaults to \f(CW4\fR. A good rule of thumb is two worker processes per CPU core for applications that perform mostly non-blocking operations, blocking operations often require more and benefit from decreasing concurrency with "max_clients" in Mojo::Server::Daemon (often as low as \f(CW1\fR). .SH METHODS .IX Header "METHODS" Mojo::Server::Prefork inherits all methods from Mojo::Server::Daemon and implements the following new ones. .SS check_pid .IX Subsection "check_pid" .Vb 1 \& my $pid = $prefork\->check_pid; .Ve .PP Get process id for running server from "pid_file" or delete it if server is not running. .PP .Vb 1 \& say \*(AqServer is not running\*(Aq unless $prefork\->check_pid; .Ve .SS ensure_pid_file .IX Subsection "ensure_pid_file" .Vb 1 \& $prefork\->ensure_pid_file($pid); .Ve .PP Ensure "pid_file" exists. .SS healthy .IX Subsection "healthy" .Vb 1 \& my $healthy = $prefork\->healthy; .Ve .PP Number of currently active worker processes with a heartbeat. .SS run .IX Subsection "run" .Vb 1 \& $prefork\->run; .Ve .PP Run server and wait for "MANAGER SIGNALS". .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .