Skip to content

Commit

Permalink
added SO_REUSEPORT support
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 24, 2013
1 parent 171198d commit 2ab7ac0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

4.27 2013-08-20
4.27 2013-08-25
- Added reuse option to Mojo::IOLoop::Server::listen.
- Added reuse parameter to Mojo::Server::Daemon::listen.

4.26 2013-08-18
- Fixed support for Netscape cookies in Mojo::Cookie::Response.
Expand Down
18 changes: 18 additions & 0 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -208,39 +208,57 @@ These options are currently available:
=item address
address => 'mojolicio.us'
Address or host name of the peer to connect to, defaults to C<localhost>.
=item handle
handle => $handle
Use an already prepared handle.
=item local_address
local_address => '127.0.0.1'
Local address to bind to.
=item port
port => 80
Port to connect to.
=item timeout
timeout => 15
Maximum amount of time in seconds establishing connection may take before
getting canceled, defaults to C<10>.
=item tls
tls => 1
Enable TLS.
=item tls_ca
tls_ca => '/etc/tls/ca.crt'
Path to TLS certificate authority file. Also activates hostname verification.
=item tls_cert
tls_cert => '/etc/tls/client.crt'
Path to the TLS certificate file.
=item tls_key
tls_key => '/etc/tls/client.key'
Path to the TLS key file.
=back
Expand Down
24 changes: 24 additions & 0 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -68,6 +68,7 @@ sub listen {
LocalPort => $port,
Proto => 'tcp',
ReuseAddr => 1,
ReusePort => $args->{reuse},
Type => SOCK_STREAM
);
$options{LocalAddr} =~ s/[\[\]]//g;
Expand Down Expand Up @@ -230,34 +231,57 @@ These options are currently available:
=item address
address => '127.0.0.1'
Local address to listen on, defaults to all.
=item backlog
backlog => 128
Maximum backlog size, defaults to C<SOMAXCONN>.
=item port
port => 80
Port to listen on.
=item reuse
reuse => 1
Allow multiple servers to use the same port with the C<SO_REUSEPORT> socket
option.
=item tls
tls => 1
Enable TLS.
=item tls_ca
tls_ca => '/etc/tls/ca.crt'
Path to TLS certificate authority file.
=item tls_cert
tls_cert => '/etc/tls/server.crt'
Path to the TLS cert file, defaults to a built-in test certificate.
=item tls_key
tls_key => '/etc/tls/server.key'
Path to the TLS key file, defaults to a built-in test key.
=item tls_verify
tls_verify => 0x00
TLS verification mode, defaults to C<0x03>.
=back
Expand Down
29 changes: 24 additions & 5 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -165,6 +165,7 @@ sub _listen {
address => $url->host,
backlog => $self->backlog,
port => $url->port,
reuse => scalar $query->param('reuse'),
tls_ca => scalar $query->param('ca'),
tls_cert => scalar $query->param('cert'),
tls_key => scalar $query->param('key')
Expand All @@ -175,7 +176,7 @@ sub _listen {
my $tls = $options->{tls} = $url->protocol eq 'https' ? 1 : undef;

weaken $self;
my $id = $self->ioloop->server(
push @{$self->{acceptors} ||= []}, $self->ioloop->server(
$options => sub {
my ($loop, $stream, $id) = @_;

Expand All @@ -196,12 +197,12 @@ sub _listen {
sub { $self->app->log->debug('Inactivity timeout.') if $c->{tx} });
}
);
push @{$self->{acceptors} ||= []}, $id;

return if $self->silent;
$self->app->log->info(qq{Listening at "$listen".});
$listen =~ s!//\*!//127.0.0.1!i;
say "Server available at $listen.";
$self->app->log->info(qq{Listening at "$url".});
$query->params([]);
$url->host('127.0.0.1') if $url->host eq '*';
say "Server available at $url.";
}

sub _read {
Expand Down Expand Up @@ -353,6 +354,9 @@ L<Mojo::IOLoop> singleton.
List of one or more locations to listen on, defaults to the value of the
MOJO_LISTEN environment variable or C<http://*:3000>.
# Allow multiple servers to use the same port (SO_REUSEPORT)
$daemon->listen(['http://*:8080?reuse=1']);
# Listen on IPv6 interface
$daemon->listen(['http://[::1]:4000']);
Expand All @@ -372,18 +376,33 @@ These parameters are currently available:
=item ca
ca=/etc/tls/ca.crt
Path to TLS certificate authority file.
=item cert
cert=/etc/tls/server.crt
Path to the TLS cert file, defaults to a built-in test certificate.
=item key
key=/etc/tls/server.key
Path to the TLS key file, defaults to a built-in test key.
=item reuse
reuse=1
Allow multiple servers to use the same port with the C<SO_REUSEPORT> socket
option.
=item verify
verify=0x00
TLS verification mode, defaults to C<0x03>.
=back
Expand Down

0 comments on commit 2ab7ac0

Please sign in to comment.