Skip to content

Commit

Permalink
added support for MOJO_CONNECT_TIMEOUT, MOJO_INACTIVITY_TIMEOUT, MOJO…
Browse files Browse the repository at this point in the history
…_REQUEST_TIMEOUT and MOJO_WEBSOCKET_TIMEOUT environment variables
  • Loading branch information
kraih committed Feb 25, 2012
1 parent 28e4acc commit c737802
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,6 +1,9 @@
This file documents the revision history for Perl extension Mojolicious.

2.54 2012-02-25 00:00:00
- Added support for MOJO_CONNECT_TIMEOUT, MOJO_INACTIVITY_TIMEOUT,
MOJO_REQUEST_TIMEOUT and MOJO_WEBSOCKET_TIMEOUT environment
variables.
- Increased default connect timeout from 3 to 10 seconds in
Mojo::UserAgent and Mojo::IOLoop::Client.
- Improved test command to colorize by default.
Expand Down
14 changes: 8 additions & 6 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -15,11 +15,11 @@ use constant BONJOUR => $ENV{MOJO_NO_BONJOUR}
use constant DEBUG => $ENV{MOJO_DAEMON_DEBUG} || 0;

has [qw/backlog group listen silent user/];
has inactivity_timeout => 15;
has inactivity_timeout => sub { $ENV{MOJO_INACTIVITY_TIMEOUT} // 15 };
has ioloop => sub { Mojo::IOLoop->singleton };
has max_clients => 1000;
has max_requests => 25;
has websocket_timeout => 300;
has websocket_timeout => sub { $ENV{MOJO_WEBSOCKET_TIMEOUT} // 300 };

my $LISTEN_RE = qr|
^
Expand Down Expand Up @@ -402,8 +402,9 @@ Group for server process.
$daemon = $daemon->inactivity_timeout(5);
Maximum amount of time in seconds a connection can be inactive before getting
dropped, defaults to C<15>. Setting the value to C<0> will allow connections
to be inactive indefinitely.
dropped, defaults to the value of the C<MOJO_INACTIVITY_TIMEOUT> environment
variable or C<15>. Setting the value to C<0> will allow connections to be
inactive indefinitely.
=head2 C<ioloop>
Expand Down Expand Up @@ -463,8 +464,9 @@ User for the server process.
$server = $server->websocket_timeout(300);
Maximum amount of time in seconds a WebSocket connection can be inactive
before getting dropped, defaults to C<300>. Setting the value to C<0> will
allow WebSocket connections to be inactive indefinitely.
before getting dropped, defaults to the value of the
C<MOJO_WEBSOCKET_TIMEOUT> environment variable or C<300>. Setting the value
to C<0> will allow WebSocket connections to be inactive indefinitely.
=head1 METHODS
Expand Down
36 changes: 20 additions & 16 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -13,20 +13,20 @@ use Scalar::Util 'weaken';
use constant DEBUG => $ENV{MOJO_USERAGENT_DEBUG} || 0;

# "You can't let a single bad experience scare you away from drugs."
has ca => sub { $ENV{MOJO_CA_FILE} };
has cert => sub { $ENV{MOJO_CERT_FILE} };
has connect_timeout => 10;
has cookie_jar => sub { Mojo::CookieJar->new };
has ca => sub { $ENV{MOJO_CA_FILE} };
has cert => sub { $ENV{MOJO_CERT_FILE} };
has connect_timeout => sub { $ENV{MOJO_CONNECT_TIMEOUT} // 10 };
has cookie_jar => sub { Mojo::CookieJar->new };
has [qw/http_proxy https_proxy local_address no_proxy/];
has inactivity_timeout => 20;
has inactivity_timeout => sub { $ENV{MOJO_INACTIVITY_TIMEOUT} // 20 };
has ioloop => sub { Mojo::IOLoop->new };
has key => sub { $ENV{MOJO_KEY_FILE} };
has max_connections => 5;
has max_redirects => sub { $ENV{MOJO_MAX_REDIRECTS} || 0 };
has name => 'Mojolicious (Perl)';
has request_timeout => 0;
has transactor => sub { Mojo::UserAgent::Transactor->new };
has websocket_timeout => 300;
has max_redirects => sub { $ENV{MOJO_MAX_REDIRECTS} || 0 };
has name => 'Mojolicious (Perl)';
has request_timeout => sub { $ENV{MOJO_REQUEST_TIMEOUT} // 0 };
has transactor => sub { Mojo::UserAgent::Transactor->new };
has websocket_timeout => sub { $ENV{MOJO_WEBSOCKET_TIMEOUT} // 300 };

# Common HTTP methods
{
Expand Down Expand Up @@ -698,7 +698,8 @@ environment variable.
$ua = $ua->connect_timeout(5);
Maximum amount of time in seconds establishing a connection may take before
getting canceled, defaults to C<10>.
getting canceled, defaults to the value of the C<MOJO_CONNECT_TIMEOUT>
environment variable or C<10>.
=head2 C<cookie_jar>
Expand Down Expand Up @@ -728,8 +729,9 @@ Proxy server to use for HTTPS and WebSocket requests.
$ua = $ua->inactivity_timeout(15);
Maximum amount of time in seconds a connection can be inactive before getting
dropped, defaults to C<20>. Setting the value to C<0> will allow connections
to be inactive indefinitely.
dropped, defaults to the value of the C<MOJO_INACTIVITY_TIMEOUT> environment
variable or C<20>. Setting the value to C<0> will allow connections to be
inactive indefinitely.
=head2 C<ioloop>
Expand Down Expand Up @@ -793,7 +795,8 @@ Domains that don't require a proxy server to be used.
Maximum amount of time in seconds establishing a connection, sending the
request and receiving a whole response may take before getting canceled,
defaults to C<0>. Setting the value to C<0> will allow the user agent to wait
defaults to the value of the C<MOJO_REQUEST_TIMEOUT> environment variable or
C<0>. Setting the value to C<0> will allow the user agent to wait
indefinitely. The timeout will reset for every followed redirect. Note that
this attribute is EXPERIMENTAL and might change without warning!
Expand All @@ -814,8 +817,9 @@ Note that this attribute is EXPERIMENTAL and might change without warning!
$ua = $ua->websocket_timeout(300);
Maximum amount of time in seconds a WebSocket connection can be inactive
before getting dropped, defaults to C<300>. Setting the value to C<0> will
allow WebSocket connections to be inactive indefinitely.
before getting dropped, defaults to the value of the
C<MOJO_WEBSOCKET_TIMEOUT> environment variable or C<300>. Setting the value
to C<0> will allow WebSocket connections to be inactive indefinitely.
=head1 METHODS
Expand Down
6 changes: 4 additions & 2 deletions lib/Mojolicious/Command/daemon.pm
Expand Up @@ -14,15 +14,17 @@ These options are available:
-c, --clients <number> Set maximum number of concurrent clients,
defaults to 1000.
-g, --group <name> Set group name for process.
-i, --inactivity <seconds> Set inactivity timeout, defaults to 15.
-i, --inactivity <seconds> Set inactivity timeout, defaults to the value
of MOJO_INACTIVITY_TIMEOUT or 15.
-l, --listen <location> Set one or more locations you want to listen
on, defaults to "http://*:3000".
-p, --proxy Activate reverse proxy support, defaults to
the value of MOJO_REVERSE_PROXY.
-r, --requests <number> Set maximum number of requests per keep-alive
connection, defaults to 25.
-u, --user <name> Set username for process.
-w, --websocket <seconds> Set WebSocket timeout, defaults to 300.
-w, --websocket <seconds> Set WebSocket timeout, defaults to the value
of MOJO_WEBSOCKET_TIMEOUT or 300.
EOF

# "It's an albino humping worm!
Expand Down
33 changes: 33 additions & 0 deletions lib/Mojolicious/Guides/Cheatsheet.pod
Expand Up @@ -169,13 +169,29 @@ L<Mojolicious::Plugin::JSONConfig>, quite useful for testing.

MOJO_CONFIG=myapp.conf

=head2 C<MOJO_CONNECT_TIMEOUT>

Maximum amount of time in seconds establishing a connection may take before
getting canceled, defaults to C<10>.

MOJO_CONNECT_TIMEOUT=30

=head2 C<MOJO_HOME>

Home directory for the L<Mojolicious> application, should always contain a
path like C</home/sri/myapp>.

MOJO_HOME=/home/sri/myapp

=head2 C<MOJO_INACTIVITY_TIMEOUT>

Maximum amount of time in seconds a connection can be inactive before getting
dropped, defaults to C<20> for L<Mojo::UserAgent> and C<15> for
L<Mojo::Server::Daemon>. Setting the value to C<0> will allow connections to
be inactive indefinitely.

MOJO_INACTIVITY_TIMEOUT=0

=head2 C<MOJO_IOWATCHER>

Alternative L<Mojo::IOWatcher> implementation to try.
Expand Down Expand Up @@ -280,6 +296,15 @@ security reasons this is disabled by default.

MOJO_PROXY=1

=head2 C<MOJO_REQUEST_TIMEOUT>

Maximum amount of time in seconds establishing a connection, sending the
request and receiving a whole response may take before getting canceled,
defaults to C<0>. Setting the value to C<0> will allow the user agent to wait
indefinitely. The timeout will reset for every followed redirect.

MOJO_REQUEST_TIMEOUT=30

=head2 C<MOJO_REVERSE_PROXY>

Enable reverse proxy support for L<Mojolicious> application. This allows
Expand All @@ -295,6 +320,14 @@ platform specific temporary directory.

MOJO_TMPDIR=/tmp/mojo

=head2 C<MOJO_WEBSOCKET_TIMEOUT>

Maximum amount of time in seconds a WebSocket connection can be inactive
before getting dropped, defaults to C<300>. Setting the value to C<0> will
allow connections to be inactive indefinitely.

MOJO_WEBSOCKET_TIMEOUT=0

=head1 MORE

You can continue with L<Mojolicious::Guides> now or take a look at the
Expand Down
17 changes: 16 additions & 1 deletion t/mojo/app.t
Expand Up @@ -6,20 +6,35 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 50;
use Test::More tests => 56;

# "I was so bored I cut the pony tail off the guy in front of us.
# Look at me, I'm a grad student.
# I'm 30 years old and I made $600 last year.
# Bart, don't make fun of grad students.
# They've just made a terrible life choice."
use Mojo::IOLoop;
use Mojo::Server::Daemon;
use Mojo::Transaction::HTTP;
use Mojo::UserAgent;

use_ok 'Mojo';
use_ok 'Mojolicious';

# Timeouts
{
is(Mojo::Server::Daemon->new->inactivity_timeout, 15, 'right value');
local $ENV{MOJO_INACTIVITY_TIMEOUT} = 25;
is(Mojo::Server::Daemon->new->inactivity_timeout, 25, 'right value');
$ENV{MOJO_INACTIVITY_TIMEOUT} = 0;
is(Mojo::Server::Daemon->new->inactivity_timeout, 0, 'right value');
is(Mojo::Server::Daemon->new->websocket_timeout, 300, 'right value');
local $ENV{MOJO_WEBSOCKET_TIMEOUT} = 25;
is(Mojo::Server::Daemon->new->websocket_timeout, 25, 'right value');
$ENV{MOJO_WEBSOCKET_TIMEOUT} = 0;
is(Mojo::Server::Daemon->new->websocket_timeout, 0, 'right value');
}

# Logger
my $logger = Mojo::Log->new;
my $app = Mojo->new({log => $logger});
Expand Down
28 changes: 27 additions & 1 deletion t/mojo/user_agent.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 73;
use Test::More tests => 86;

# "The strong must protect the sweet."
use Mojo::IOLoop;
Expand Down Expand Up @@ -80,6 +80,32 @@ get '/echo' => sub {
{
local $ENV{MOJO_MAX_REDIRECTS} = 25;
is(Mojo::UserAgent->new->max_redirects, 25, 'right value');
$ENV{MOJO_MAX_REDIRECTS} = 0;
is(Mojo::UserAgent->new->max_redirects, 0, 'right value');
}

# Timeouts
{
is(Mojo::UserAgent->new->connect_timeout, 10, 'right value');
local $ENV{MOJO_CONNECT_TIMEOUT} = 25;
is(Mojo::UserAgent->new->connect_timeout, 25, 'right value');
$ENV{MOJO_CONNECT_TIMEOUT} = 0;
is(Mojo::UserAgent->new->connect_timeout, 0, 'right value');
is(Mojo::UserAgent->new->inactivity_timeout, 20, 'right value');
local $ENV{MOJO_INACTIVITY_TIMEOUT} = 25;
is(Mojo::UserAgent->new->inactivity_timeout, 25, 'right value');
$ENV{MOJO_INACTIVITY_TIMEOUT} = 0;
is(Mojo::UserAgent->new->inactivity_timeout, 0, 'right value');
is(Mojo::UserAgent->new->request_timeout, 0, 'right value');
local $ENV{MOJO_REQUEST_TIMEOUT} = 25;
is(Mojo::UserAgent->new->request_timeout, 25, 'right value');
$ENV{MOJO_REQUEST_TIMEOUT} = 0;
is(Mojo::UserAgent->new->request_timeout, 0, 'right value');
is(Mojo::UserAgent->new->websocket_timeout, 300, 'right value');
local $ENV{MOJO_WEBSOCKET_TIMEOUT} = 25;
is(Mojo::UserAgent->new->websocket_timeout, 25, 'right value');
$ENV{MOJO_WEBSOCKET_TIMEOUT} = 0;
is(Mojo::UserAgent->new->websocket_timeout, 0, 'right value');
}

# GET / (non-blocking)
Expand Down

0 comments on commit c737802

Please sign in to comment.