| 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.09'; # 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 Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use List::MoreUtils qw( none); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | #get the timeout from headers | ||||||||||||||||||||||||||||
| 16 | hook(before => sub { | ||||||||||||||||||||||||||||
| 17 | 13 | ▶ | var header_timeout => request->header('X-Dancer-Timeout'); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | }); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | register 'timeout' => \&timeout; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | register_exception ('InvalidArgumentNumber', | |||||||||||||||||||||||||||
| 23 | message_pattern => "the number of arguments must 3 or 4, you've got %s", | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | 1 | register_exception ('InvalidMethod', | |||||||||||||||||||||||||||
| 26 | message_pattern => "method must be one in get, put, post, delete and %s is used as a method", | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | my @authorized_methods = ('get', 'post', 'put', 'delete'); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =method exception_message | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | return the exception message | ||||||||||||||||||||||||||||
| 35 | This method can be used to catch the exception if the code used already contained a try catch | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub exception_message{ | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 6 | ▶ | return 'Route Timeout Detected'; | ||||||||||||||||||||||||||
|
Subroutine
exception_message: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =method timeout | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Method that manage the timeout on a dancer request | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub timeout { | ||||||||||||||||||||||||||||
| 51 | 7 | ▶ | my ($timeout,$method, $pattern, @rest); | ||||||||||||||||||||||||||
|
Subroutine
timeout: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 52 | 7 | ▶ | if (scalar(@_) == 4){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | 5 | ($timeout,$method, $pattern, @rest) = @_; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | elsif(scalar(@_) == 3){ | ||||||||||||||||||||||||||||
| 56 | 2 | ($method, $pattern, @rest) = @_; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else{ | ||||||||||||||||||||||||||||
| 59 | 0 | raise InvalidMethod => scalar(@_); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 7 | my $code; | |||||||||||||||||||||||||||
| 62 | 7 | ▶ | for my $e (@rest) { $code = $e if (ref($e) eq 'CODE') } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 7 | my $request; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | #if method is not valid an exception is done | ||||||||||||||||||||||||||||
| 66 | 7 | ▶ | if ( none { $_ eq lc($method) } @authorized_methods ){ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | raise InvalidMethod => $method; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | my $exception_message = exception_message(); | |||||||||||||||||||||||||||
| 71 | my $timeout_route = sub { | ||||||||||||||||||||||||||||
| 72 | 13 | ▶ | my $response; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | #if timeout is not defined but a value is set in the headers for timeout | ||||||||||||||||||||||||||||
| 75 | 13 | my $request_timeout = 0; | |||||||||||||||||||||||||||
| 76 | 13 | ▶ | $request_timeout = $timeout if (defined $timeout); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 13 | ▶ | $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"}
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # if timeout is not defined or equal 0 the timeout manager is not used | ||||||||||||||||||||||||||||
| 80 | 13 | my $timeout_exception; | |||||||||||||||||||||||||||
| 81 | 13 | ▶ | if (!$request_timeout){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 5 | $response = $code->(); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else{ | ||||||||||||||||||||||||||||
| 85 | try { | ||||||||||||||||||||||||||||
| 86 | 6 | ▶ | local $SIG{ALRM} = sub { croak ($exception_message); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 8 | alarm($request_timeout); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 8 | $response = $code->(); | |||||||||||||||||||||||||||
| 90 | 2 | alarm(0); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | catch{ | ||||||||||||||||||||||||||||
| 93 | 6 | ▶ | $timeout_exception = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | 8 | }; | |||||||||||||||||||||||||||
| 95 | 8 | alarm(0); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | # Timeout detected | ||||||||||||||||||||||||||||
| 98 | 13 | ▶ | 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/
| |||||||||||||||||||||||||||||
| 99 | 6 | my $response_with_timeout = Dancer::Response->new( | |||||||||||||||||||||||||||
| 100 | status => 408, | ||||||||||||||||||||||||||||
| 101 | content => "Request Timeout : more than $request_timeout seconds elapsed." | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | 6 | return $response_with_timeout; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | # Preserve exceptions caught during route call | ||||||||||||||||||||||||||||
| 106 | 7 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # else everything is allright | ||||||||||||||||||||||||||||
| 109 | 7 | return $response; | |||||||||||||||||||||||||||
| 110 | 6 | }; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | my @compiled_rest; | |||||||||||||||||||||||||||
| 114 | 6 | for my $e (@rest) { | |||||||||||||||||||||||||||
| 115 | 6 | ▶ | if (ref($e) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 6 | push @compiled_rest, $timeout_route; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||
| 119 | 0 | push @compiled_rest, $e; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # declare the route in Dancer's registry | ||||||||||||||||||||||||||||
| 124 | 6 | any [$method] => $pattern, @compiled_rest; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | register_plugin; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | 1; | |||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||