lib/Dancer/Plugin/TimeoutManager.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 80.0% 16 / 20 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 1 totaltotal 92.7% 115 / 124 SCAR 27.1
CC
15
Cov
93%
timeout
8.1
__ANON__
8.0
BEGIN
1.0
CRAP 15.1
« blib/lib/Dancer/Plugin/TimeoutManager.pm ↑ Summary
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
BranchTrueFalseTotal
if (scalar @_ == 4) { } 5 2 7
elsif (scalar @_ == 3) { } 2 0 2
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
BranchTrueFalseTotal
if ref $e eq "CODE" 7 0 7
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
BranchTrueFalseTotal
if (&none(sub { $_ eq lc $method; } , @authorized_methods)) 1 6 7
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
BranchTrueFalseTotal
if defined $timeout 6 7 13
77 13 ▶ $request_timeout = vars->{header_timeout} if (!defined $timeout && defined vars->{header_timeout});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $timeout and defined vars()->{"header_timeout"} 4 9 13
Condition100%3/3
not defined $timeout and defined vars()->{"header_timeout"}
!ll&&!rl&&r
634
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (not $request_timeout) { } 5 8 13
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
BranchTrueFalseTotal
if ($timeout_exception and $timeout_exception =~ /$exception_message/) 6 7 13
Condition66%2/3
$timeout_exception and $timeout_exception =~ /$exception_message/
!ll&&!rl&&r
706
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/
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $@ 0 7 7
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
BranchTrueFalseTotal
if (ref $e eq 'CODE') { } 6 0 6
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__
« blib/lib/Dancer/Plugin/TimeoutManager.pm ↑ Summary