++ed by:

2 non-PAUSE users.

Thomas Kratz
and 1 contributors

NAME

Mojo::Server::Threaded - Multithreaded non-blocking I/O HTTP and WebSocket server

SYNOPSIS

  use Mojo::Server::Threaded;

  my $threaded = Mojo::Server::Threaded->new(listen => ['http://*:8080']);
  $threaded->unsubscribe('request')->on(request => sub {
    my ($threaded, $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;
  });
  $threaded->run;

DESCRIPTION

Mojo::Server::Threaded is a multithreaded alternative for Mojo::Server::Prefork which is not available under Win32. It takes the same parameters and works analoguous by just using threads instead of forked processes.

The main difference besides using threads is that signals are only used for the termination of the server. Starting, stopping and de- or increasing the amount of workers is done via "send_command".

MANAGER SIGNALS

Signals under Windows are not really useful for communicating with running background processes. Mojo::Server::Threaded can be controlled using a TCP management socket. See "MANAGER COMMANDS".

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

INT, TERM

Shut down server immediately.

QUIT

Shut down server gracefully.

EVENTS

Mojo::Server::Threaded emits the same events as Mojo::Server::Prefork and implements the following new ones.

command

  $threaded->on(command => sub {
    my($threaded, $command, @params) = @_;
    say "got command $command on management port";
    ...
  });

Emitted when a command is received on the manager listening port.

ATTRIBUTES

Mojo::Server::Threaded recognizes the same attributes as Mojo::Server::Prefork and implements the following new ones.

manage_interval

  my $interval = $threaded->manage_interval;
  $threaded    = $threaded->manage_interval(0.5);

Check interval for the management port in seconds, defaults to 0.1.

METHODS

Mojo::Server::Threaded inherits all methods from Mojo::Server::Daemon, implements the same ones as Mojo::Server::Prefork and the following new ones.

check_mport

  my $port = $threaded->check_mport;

Get the TCP port for the management socket from "pid_file" in Mojo::Server::Prefork.

register_command

  $threaded->register_command( HELLO => sub {
      my($self, $cmd, @params) = @_;
      warn "handling $cmd on management port\n";
  });

Register a management command and handler subroutine. The command will be upcased.

send_command

  $threaded->send_command("hello DO SOMETHING");

Send a command to the management socket. The string will be split on whitespace and the first element will be upcased on the receiver's side.

MANAGER COMMANDS

Mojo::Server::Threaded can be controlled by the following commands:

KILL

Shut down server immediately.

QUIT

Shut down server gracefully.

WORKER <QUIT|KILL> <ID>

Send QUIT or KILL to an existing worker thread

WORKERS <NUMBER>

Increase or decrease (negative values) the number of workers by NUMBER.

CAVEATS

Since Perl ithreads are relatively heavy in comparison the spawning of threads is much slower than forking on UNIX. The hot deploy also will be much slower, as a new interpreter is started.

Once running this should not affect performance too much.

SEE ALSO

Mojolicious, Mojolicious::Guides, https://mojolicious.org.