Mojo::Server::Prefork - Preforking non-blocking I/O HTTP and WebSocket server
use Mojo::Server::Prefork; my $prefork = Mojo::Server::Prefork->new(listen => ['http://*:8080']); $prefork->unsubscribe('request'); $prefork->on(request => sub { my ($prefork, $tx) = @_; # Request my $method = $tx->req->method; my $path = $tx->req->url->path; # Response $tx->res->code(200); $tx->res->headers->content_type('text/plain'); $tx->res->body("$method request for $path!"); # Resume transaction $tx->resume; }); $prefork->run;
Mojo::Server::Prefork is a full featured, UNIX optimized, preforking non-blocking I/O HTTP and WebSocket server, built around the very well tested and reliable Mojo::Server::Daemon, with IPv6, TLS, 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.
For better scalability (epoll, kqueue) and to provide IPv6, SOCKS5 as well as TLS support, the optional modules EV (4.0+), IO::Socket::IP (0.20+), IO::Socket::Socks (0.64+) and IO::Socket::SSL (1.84+) will be used automatically if they are installed. Individual features can also be disabled with the MOJO_NO_IPV6, MOJO_NO_SOCKS and MOJO_NO_TLS environment variables.
MOJO_NO_IPV6
MOJO_NO_SOCKS
MOJO_NO_TLS
See "DEPLOYMENT" in Mojolicious::Guides::Cookbook for more.
The Mojo::Server::Prefork manager process can be controlled at runtime with the following signals.
Shutdown server immediately.
Shutdown server gracefully.
Increase worker pool by one.
Decrease worker pool by one.
Mojo::Server::Prefork worker processes can be controlled at runtime with the following signals.
Stop worker immediately.
Stop worker gracefully.
Mojo::Server::Prefork inherits all events from Mojo::Server::Daemon and can emit the following new ones.
$prefork->on(finish => sub { my ($prefork, $graceful) = @_; ... });
Emitted when the server shuts down.
$prefork->on(finish => sub { my ($prefork, $graceful) = @_; say $graceful ? 'Graceful server shutdown' : 'Server shutdown'; });
$prefork->on(heartbeat => sub { my ($prefork, $pid) = @_; ... });
Emitted when a heartbeat message has been received from a worker.
$prefork->on(heartbeat => sub { my ($prefork, $pid) = @_; say "Worker $pid has a heartbeat"; });
$prefork->on(reap => sub { my ($prefork, $pid) = @_; ... });
Emitted when a child process dies.
$prefork->on(reap => sub { my ($prefork, $pid) = @_; say "Worker $pid stopped"; });
$prefork->on(spawn => sub { my ($prefork, $pid) = @_; ... });
Emitted when a worker process is spawned.
$prefork->on(spawn => sub { my ($prefork, $pid) = @_; say "Worker $pid started"; });
$prefork->on(wait => sub { my $prefork = shift; ... });
Emitted when the manager starts waiting for new heartbeat messages.
$prefork->on(wait => sub { my $prefork = shift; my $workers = $prefork->workers; say "Waiting for heartbeat messages from $workers workers"; });
Mojo::Server::Prefork inherits all attributes from Mojo::Server::Daemon and implements the following new ones.
my $interval = $prefork->accept_interval; $prefork = $prefork->accept_interval(0.5);
Interval in seconds for trying to reacquire the accept mutex, passed along to "accept_interval" in Mojo::IOLoop. Note that changing this value can affect performance and idle CPU usage.
my $accepts = $prefork->accepts; $prefork = $prefork->accepts(100);
Maximum number of connections a worker is allowed to accept before stopping gracefully, passed along to "max_accepts" in Mojo::IOLoop, defaults to 1000. Setting the value to 0 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 that worker processes will stop sending heartbeat messages once the limit has been reached.
1000
0
my $bool = $prefork->cleanup; $prefork = $prefork->cleanup($bool);
Delete "lock_file" and "pid_file" automatically once they are not needed anymore, defaults to a true value.
my $timeout = $prefork->graceful_timeout; $prefork = $prefork->graceful_timeout(15);
Maximum amount of time in seconds stopping a worker gracefully may take before being forced, defaults to 20.
20
my $interval = $prefork->heartbeat_intrval; $prefork = $prefork->heartbeat_interval(3);
Heartbeat interval in seconds, defaults to 5.
5
my $timeout = $prefork->heartbeat_timeout; $prefork = $prefork->heartbeat_timeout(2);
Maximum amount of time in seconds before a worker without a heartbeat will be stopped gracefully, defaults to 20.
my $file = $prefork->lock_file; $prefork = $prefork->lock_file('/tmp/prefork.lock');
Full path of accept mutex lock file prefix, to which the process id will be appended, defaults to a random temporary path.
my $timeout = $prefork->lock_timeout; $prefork = $prefork->lock_timeout(0.5);
Maximum amount of time in seconds a worker may block when waiting for the accept mutex, defaults to 1. Note that changing this value can affect performance and idle CPU usage.
1
my $multi = $prefork->multi_accept; $prefork = $prefork->multi_accept(100);
Number of connections to accept at once, passed along to "multi_accept" in Mojo::IOLoop.
my $file = $prefork->pid_file; $prefork = $prefork->pid_file('/tmp/prefork.pid');
Full path of process id file, defaults to a random temporary path.
my $workers = $prefork->workers; $prefork = $prefork->workers(10);
Number of worker processes, defaults to 4. 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 the number of concurrent "clients" in Mojo::Server::Daemon (often as low as 1).
4
Mojo::Server::Prefork inherits all methods from Mojo::Server::Daemon and implements the following new ones.
my $pid = $prefork->check_pid;
Get process id for running server from "pid_file" or delete it if server is not running.
say 'Server is not running' unless $prefork->check_pid;
$prefork->ensure_pid_file;
Ensure "pid_file" exists.
$prefork->run;
Run server.
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
To install Mojolicious, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious
CPAN shell
perl -MCPAN -e shell install Mojolicious
For more information on module installation, please visit the detailed CPAN module installation guide.