| 1 | package Dancer::Plugin::TimeoutManager; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '0.10'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Dancer::Exception ':all'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use List::MoreUtils qw( none); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | if ( exists $ENV{DISABLE_DANCER_TIMEOUT} && $ENV{DISABLE_DANCER_TIMEOUT} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $ENV{"DISABLE_DANCER_TIMEOUT"} and $ENV{"DISABLE_DANCER_TIMEOUT"}
MC/DC0%0/2
exists $ENV{"DISABLE_DANCER_TIMEOUT"} and $ENV{"DISABLE_DANCER_TIMEOUT"}
exists $ENV{"DISABLE_DANCER_TIMEOUT"}
$ENV{"DISABLE_DANCER_TIMEOUT"}
Truth table33%1/3
A and B
A= exists $ENV{"DISABLE_DANCER_TIMEOUT"}B= $ENV{"DISABLE_DANCER_TIMEOUT"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | set disable_timeout => 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | #get the timeout from headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 19 | ▶ | hook(before => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 19 | var header_timeout => request->header('X-Dancer-Timeout'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | register 'timeout' => \&timeout; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | register_exception ('InvalidArgumentNumber', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | message_pattern => "the number of arguments must 3 or 4, you've got %s", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | register_exception ('InvalidMethod', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | message_pattern => "method must be one in get, put, post, delete and %s is used as a method", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | my @authorized_methods = ('get', 'post', 'put', 'delete'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | =method exception_message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | return the exception message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | This method can be used to catch the exception if the code used already contained a try catch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | ▶ | sub exception_message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
exception_message: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 7 | return 'Route Timeout Detected'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | =method timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | Method that manage the timeout on a dancer request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 8 | ▶ | sub timeout { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
timeout: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 8 | my ($timeout,$method, $pattern, @rest); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 8 | ▶ | if (scalar(@_) == 4) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | ($timeout,$method, $pattern, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | elsif(scalar(@_) == 3) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | ($method, $pattern, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | raise InvalidMethod => scalar(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 8 | my $code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 8 | ▶ | for my $e (@rest) { $code = $e if (ref($e) eq 'CODE') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | my $request; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | #if method is not valid an exception is done | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | if ( none { $_ eq lc($method) } @authorized_methods ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | raise InvalidMethod => $method; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 7 | my $exception_message = exception_message(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 19 | ▶ | my $timeout_route = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 19 | my $response; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # maximum possible timeout, set in the plugin config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 19 | my $conf = plugin_setting(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | #if timeout is not defined but a value is set in the headers for timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 19 | my $request_timeout = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 19 | ▶ | $request_timeout = $timeout if (defined $timeout); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 19 | ▶ | $request_timeout = vars->{header_timeout} if (!defined $timeout && defined vars->{header_timeout}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $timeout and defined vars()->{"header_timeout"}
MC/DC100%2/2
not defined $timeout and defined vars()->{"header_timeout"}
not defined $timeout
defined vars()->{"header_timeout"}
Truth table100%3/3
not(A) and B
A= not defined $timeoutB= defined vars()->{"header_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | $request_timeout = $conf->{max_timeout} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 19 | ▶ | if (defined $conf->{max_timeout} && defined $request_timeout && $request_timeout && $conf->{max_timeout} < $request_timeout); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
defined $conf->{"max_timeout"} and defined $request_timeout and $request_timeout and $conf->{"max_timeout"} < $request_timeout
defined $conf->{"max_timeout"} and defined $request_timeout and $request_timeout
defined $conf->{"max_timeout"} and defined $request_timeout
MC/DC50%2/4
defined $conf->{"max_timeout"} and defined $request_timeout and $request_timeout and $conf->{"max_timeout"} < $request_timeout
defined $conf->{"max_timeout"}
defined $request_timeout
$request_timeout
$conf->{"max_timeout"} < $request_timeout
Truth table60%3/5
A and B and C and D
A= defined $conf->{"max_timeout"}B= defined $request_timeoutC= $request_timeoutD= $conf->{"max_timeout"} < $request_timeout
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # if timeout is not defined or equal 0 the timeout manager is not used | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 19 | my $timeout_exception; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 19 | ▶ | if (!$request_timeout || (defined setting('disable_timeout') && setting('disable_timeout'))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $request_timeout or defined setting("disable_timeout") and setting("disable_timeout")
defined setting("disable_timeout") and setting("disable_timeout")
MC/DC100%3/3
not $request_timeout or defined setting("disable_timeout") and setting("disable_timeout")
not $request_timeout
defined setting("disable_timeout")
setting("disable_timeout")
Truth table100%4/4
not(A) or B and C
A= not $request_timeoutB= defined setting("disable_timeout")C= setting("disable_timeout")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 9 | $response = $code->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 10 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 10 | ▶ | local $SIG{ALRM} = sub { croak ($exception_message); }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 10 | alarm($request_timeout); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 10 | $response = $code->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 2 | alarm(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | ▶ | catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | $timeout_exception = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 10 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 10 | alarm(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # Timeout detected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 19 | ▶ | if ($timeout_exception && $timeout_exception =~ /$exception_message/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$timeout_exception and $timeout_exception =~ /$exception_message/
MC/DC50%1/2
$timeout_exception and $timeout_exception =~ /$exception_message/
$timeout_exception
$timeout_exception =~ /$exception_message/
Truth table66%2/3
A and B
A= $timeout_exceptionB= $timeout_exception =~ /$exception_message/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 8 | my $response_with_timeout = Dancer::Response->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | status => 408, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | content => "Request Timeout : more than $request_timeout seconds elapsed." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 8 | return $response_with_timeout; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # Preserve exceptions caught during route call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 11 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # else everything is allright | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 11 | return $response; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 7 | my @compiled_rest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 7 | for my $e (@rest) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 7 | ▶ | if (ref($e) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 7 | push @compiled_rest, $timeout_route; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | push @compiled_rest, $e; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # declare the route in Dancer's registry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 7 | any [$method] => $pattern, @compiled_rest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | register_plugin; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||