Mojolicious
view release on metacpan or search on metacpan
lib/Mojo/Server/Daemon.pm view on Meta::CPAN
}
sub _remove {
my ($self, $id) = @_;
$self->ioloop->remove($id);
$self->_close($id);
}
sub _url { shift->req->url->to_abs }
sub _write {
my ($self, $id) = @_;
# Protect from resume event recursion
my $c = $self->{connections}{$id};
return if !(my $tx = $c->{tx}) || $c->{writing};
local $c->{writing} = 1;
my $chunk = $tx->server_write;
warn term_escape "-- Server >>> Client (@{[_url($tx)]})\n$chunk\n" if DEBUG;
my $next = $tx->is_finished ? '_finish' : length $chunk ? '_write' : undef;
return $self->ioloop->stream($id)->write($chunk) unless $next;
weaken $self;
$self->ioloop->stream($id)->write($chunk => sub { $self->$next($id) });
}
1;
=encoding utf8
=head1 NAME
Mojo::Server::Daemon - Non-blocking I/O HTTP and WebSocket server
=head1 SYNOPSIS
use 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;
=head1 DESCRIPTION
L<Mojo::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 L<EV> (4.32+), L<Net::DNS::Native> (0.15+), L<IO::Socket::Socks> (0.64+) and L<IO::Socket::SSL>
(2.009+) will be used automatically if possible. Individual features can also be disabled with the C<MOJO_NO_NNR>,
C<MOJO_NO_SOCKS> and C<MOJO_NO_TLS> environment variables.
See L<Mojolicious::Guides::Cookbook/"DEPLOYMENT"> for more.
=head1 SIGNALS
The L<Mojo::Server::Daemon> process can be controlled at runtime with the following signals.
=head2 INT, TERM
Shut down server immediately.
=head1 EVENTS
L<Mojo::Server::Daemon> inherits all events from L<Mojo::Server>.
=head1 ATTRIBUTES
L<Mojo::Server::Daemon> inherits all attributes from L<Mojo::Server> and implements the following new ones.
=head2 acceptors
my $acceptors = $daemon->acceptors;
$daemon = $daemon->acceptors(['6be0c140ef00a389c5d039536b56d139']);
Active acceptor ids.
# Check port
mu $port = $daemon->ioloop->acceptor($daemon->acceptors->[0])->port;
=head2 backlog
my $backlog = $daemon->backlog;
$daemon = $daemon->backlog(128);
Listen backlog size, defaults to C<SOMAXCONN>.
=head2 inactivity_timeout
my $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 C<MOJO_INACTIVITY_TIMEOUT> environment variable or C<30>. Setting the value to C<0> will allow
connections to be inactive indefinitely.
=head2 ioloop
my $loop = $daemon->ioloop;
$daemon = $daemon->ioloop(Mojo::IOLoop->new);
Event loop object to use for I/O operations, defaults to the global L<Mojo::IOLoop> singleton.
=head2 keep_alive_timeout
my $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,
( run in 0.530 second using v1.01-cache-2.11-cpan-39bf76dae61 )