|
NAMEMojo::Server::Daemon - Non-blocking I/O HTTP and WebSocket serverSYNOPSISuse Mojo::Server::Daemon; my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:8080']); $daemon->unsubscribe('request')->on(request => sub ($daemon, $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; }); $daemon->run; DESCRIPTIONMojo::Server::Daemon is a full featured, highly portable non-blocking I/O HTTP and WebSocket server, with IPv6, TLS, SNI, Comet (long polling), keep-alive and multiple event loop support.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 (2.009+) 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. SIGNALSThe Mojo::Server::Daemon process can be controlled at runtime with the following signals.INT, TERMShut down server immediately.EVENTSMojo::Server::Daemon inherits all events from Mojo::Server.ATTRIBUTESMojo::Server::Daemon inherits all attributes from Mojo::Server and implements the following new ones.acceptorsmy $acceptors = $daemon->acceptors; $daemon = $daemon->acceptors(['6be0c140ef00a389c5d039536b56d139']); Active acceptor ids. # Check port mu $port = $daemon->ioloop->acceptor($daemon->acceptors->[0])->port; backlogmy $backlog = $daemon->backlog; $daemon = $daemon->backlog(128); Listen backlog size, defaults to "SOMAXCONN". inactivity_timeoutmy $timeout = $daemon->inactivity_timeout; $daemon = $daemon->inactivity_timeout(5); Maximum amount of time in seconds a connection with an active request can be inactive before getting closed, defaults to the value of the "MOJO_INACTIVITY_TIMEOUT" environment variable or 30. Setting the value to 0 will allow connections to be inactive indefinitely. ioloopmy $loop = $daemon->ioloop; $daemon = $daemon->ioloop(Mojo::IOLoop->new); Event loop object to use for I/O operations, defaults to the global Mojo::IOLoop singleton. keep_alive_timeoutmy $timeout = $daemon->keep_alive_timeout; $daemon = $daemon->keep_alive_timeout(10); Maximum amount of time in seconds a connection without an active request can be inactive before getting closed, defaults to the value of the "MOJO_KEEP_ALIVE_TIMEOUT" environment variable or 5. Setting the value to 0 will allow connections to be inactive indefinitely. listenmy $listen = $daemon->listen; $daemon = $daemon->listen(['https://127.0.0.1:8080']); Array reference with one or more locations to listen on, defaults to the value of the "MOJO_LISTEN" environment variable or "http://*:3000" (shortcut for "http://0.0.0.0:3000"). # Listen on all IPv4 interfaces $daemon->listen(['http://*:3000']); # Listen on all IPv4 and IPv6 interfaces $daemon->listen(['http://[::]:3000']); # Listen on IPv6 interface $daemon->listen(['http://[::1]:4000']); # Listen on IPv4 and IPv6 interfaces $daemon->listen(['http://127.0.0.1:3000', 'http://[::1]:3000']); # Listen on UNIX domain socket "/tmp/myapp.sock" (percent encoded slash) $daemon->listen(['http+unix://%2Ftmp%2Fmyapp.sock']); # File descriptor, as used by systemd $daemon->listen(['http://127.0.0.1?fd=3']); # Allow multiple servers to use the same port (SO_REUSEPORT) $daemon->listen(['http://*:8080?reuse=1']); # Listen on two ports with HTTP and HTTPS at the same time $daemon->listen(['http://*:3000', 'https://*:4000']); # Use a custom certificate and key $daemon->listen(['https://*:3000?cert=/x/server.crt&key=/y/server.key']); # Domain specific certificates and keys (SNI) $daemon->listen( ['https://*:3000?example.com_cert=/x/my.crt&example.com_key=/y/my.key']); # Or even a custom certificate authority $daemon->listen( ['https://*:3000?cert=/x/server.crt&key=/y/server.key&ca=/z/ca.crt']); These parameters are currently available:
max_clientsmy $max = $daemon->max_clients; $daemon = $daemon->max_clients(100); Maximum number of accepted connections this server is allowed to handle concurrently, before stopping to accept new incoming connections, passed along to "max_connections" in Mojo::IOLoop. max_requestsmy $max = $daemon->max_requests; $daemon = $daemon->max_requests(250); Maximum number of keep-alive requests per connection, defaults to 100. silentmy $bool = $daemon->silent; $daemon = $daemon->silent($bool); Disable console messages. METHODSMojo::Server::Daemon inherits all methods from Mojo::Server and implements the following new ones.portsmy $ports = $daemon->ports; Get all ports this server is currently listening on. # All ports say for @{$daemon->ports}; run$daemon->run; Run server and wait for "SIGNALS". start$daemon = $daemon->start; Start or resume accepting connections through "ioloop". # Listen on random port my $port = $daemon->listen(['http://127.0.0.1'])->start->ports->[0]; # Run multiple web servers concurrently my $daemon1 = Mojo::Server::Daemon->new(listen => ['http://*:3000'])->start; my $daemon2 = Mojo::Server::Daemon->new(listen => ['http://*:4000'])->start; Mojo::IOLoop->start unless Mojo::IOLoop->is_running; stop$daemon = $daemon->stop; Stop accepting connections through "ioloop". DEBUGGINGYou can set the "MOJO_SERVER_DEBUG" environment variable to get some advanced diagnostics information printed to "STDERR".MOJO_SERVER_DEBUG=1 SEE ALSOMojolicious, Mojolicious::Guides, <https://mojolicious.org>.
Visit the GSP FreeBSD Man Page Interface. |