GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Mojo::Server::Prefork(3) User Contributed Perl Documentation Mojo::Server::Prefork(3)

Mojo::Server::Prefork - Pre-forking non-blocking I/O HTTP and WebSocket server

  use Mojo::Server::Prefork;

  my $prefork = Mojo::Server::Prefork->new(listen => ['http://*:8080']);
  $prefork->unsubscribe('request')->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('text/plain');
    $tx->res->body("$method request for $path!");

    # Resume transaction
    $tx->resume;
  });
  $prefork->run;

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.

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 "MOJO_NO_NNR", "MOJO_NO_SOCKS" and "MOJO_NO_TLS" environment variables.

See "DEPLOYMENT" in Mojolicious::Guides::Cookbook for more.

The Mojo::Server::Prefork manager process can be controlled at runtime with the following signals.

Shut down server immediately.

Shut down 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 gracefully.

Mojo::Server::Prefork inherits all events from Mojo::Server::Daemon and can emit the following new ones.

  $prefork->on(finish => sub ($prefork, $graceful) {...});

Emitted when the server shuts down.

  $prefork->on(finish => sub ($prefork, $graceful) {
    say $graceful ? 'Graceful server shutdown' : 'Server shutdown';
  });

  $prefork->on(heartbeat => sub ($prefork, $pid) {...});

Emitted when a heartbeat message has been received from a worker.

  $prefork->on(heartbeat => sub ($prefork, $pid) { say "Worker $pid has a heartbeat" });

  $prefork->on(reap => sub ($prefork, $pid) {...});

Emitted when a child process exited.

  $prefork->on(reap => sub ($prefork, $pid) { say "Worker $pid stopped" });

  $prefork->on(spawn => sub ($prefork, $pid) {...});

Emitted when a worker process is spawned.

  $prefork->on(spawn => sub ($prefork, $pid) { say "Worker $pid started" });

  $prefork->on(wait => sub ($prefork) {...});

Emitted when the manager starts waiting for new heartbeat messages.

  $prefork->on(wait => sub ($prefork) {
    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 $accepts = $prefork->accepts;
  $prefork    = $prefork->accepts(100);

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 10000. 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 to make sure that not all workers restart at the same time.

  my $bool = $prefork->cleanup;
  $prefork = $prefork->cleanup($bool);

Delete "pid_file" automatically once it is 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 120. Note that this value should usually be a little larger than the maximum amount of time you expect any one request to take.

  my $interval = $prefork->heartbeat_interval;
  $prefork     = $prefork->heartbeat_interval(3);

Heartbeat interval in seconds, defaults to 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 50. 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.

  my $file = $prefork->pid_file;
  $prefork = $prefork->pid_file('/tmp/prefork.pid');

Full path of process id file, defaults to "prefork.pid" in a temporary directory.

  my $spare = $prefork->spare;
  $prefork  = $prefork->spare(4);

Temporarily spawn up to this number of additional workers if there is a need, defaults to 2. This allows for new workers to be started while old ones are still shutting down gracefully, drastically reducing the performance cost of worker restarts.

  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 concurrency with "max_clients" in Mojo::Server::Daemon (often as low as 1).

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($pid);

Ensure "pid_file" exists.

  my $healthy = $prefork->healthy;

Number of currently active worker processes with a heartbeat.

  $prefork->run;

Run server and wait for "MANAGER SIGNALS".

Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
2021-12-08 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.