File Coverage

blib/lib/Riak/Light/Timeout/TimeOut.pm
Criterion Covered Total %
statement 45 47 95.7
branch 8 10 80.0
condition n/a
subroutine 13 13 100.0
pod 0 4 0.0
total 66 74 89.1


line stmt bran cond sub pod time code
1             #
2             # This file is part of Riak-Light
3             #
4             # This software is copyright (c) 2013 by Weborama.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             ## no critic (RequireUseStrict, RequireUseWarnings)
10             package Riak::Light::Timeout::TimeOut;
11             {
12             $Riak::Light::Timeout::TimeOut::VERSION = '0.12';
13             }
14             ## use critic
15              
16 2     2   68272 use POSIX qw(ETIMEDOUT ECONNRESET);
  2         4  
  2         13  
17 2     2   1486 use Time::Out qw(timeout);
  2         1446  
  2         93  
18 2     2   11 use Time::HiRes;
  2         3  
  2         8  
19 2     2   588 use Riak::Light::Util qw(is_windows);
  2         5  
  2         81  
20 2     2   9 use Carp;
  2         3  
  2         85  
21 2     2   10 use Moo;
  2         4  
  2         17  
22 2     2   651 use Types::Standard -types;
  2         4  
  2         49  
23              
24             with 'Riak::Light::Timeout';
25              
26             # ABSTRACT: proxy to read/write using Time::Out as a timeout provider
27              
28             has socket => ( is => 'ro', required => 1 );
29             has in_timeout => ( is => 'ro', isa => Num, default => sub {0.5} );
30             has out_timeout => ( is => 'ro', isa => Num, default => sub {0.5} );
31             has is_valid => ( is => 'rw', isa => Bool, default => sub {1} );
32              
33             sub BUILD {
34              
35             # from Time::Out documentation
36             # alarm(2) doesn't interrupt blocking I/O on MSWin32, so 'timeout' won't do that either.
37              
38 3 100   3 0 329 croak "Time::Out alarm(2) doesn't interrupt blocking I/O on MSWin32!"
39             if is_windows();
40              
41 2         649 carp "Not Safe: can clobber previous alarm";
42             }
43              
44             sub clean {
45 1     1 0 33 $_[0]->socket->close;
46 1         174 $_[0]->is_valid(0);
47             }
48              
49             sub sysread {
50 3     3 0 6 my $self = shift;
51 3 50       76 $self->is_valid
52             or $! = ECONNRESET,
53             return; ## no critic (RequireLocalizedPunctuationVars)
54              
55 3         21 my $buffer;
56 3         23 my $seconds = $self->in_timeout;
57             my $result = timeout $seconds, @_ => sub {
58 3     3   128 my $readed = $self->socket->sysread(@_);
59 3         500098 $buffer = $_[0]; # NECESSARY, timeout does not map the alias @_ !!
60 2         14 $readed;
61 3         32 };
62 3 100       158 if ($@) {
63 1         8 $self->clean();
64 1         38 $! = ETIMEDOUT; ## no critic (RequireLocalizedPunctuationVars)
65             }
66             else {
67 2         19 $_[0] = $buffer;
68             }
69              
70 3         14 $result;
71             }
72              
73             sub syswrite {
74 3     3 0 5 my $self = shift;
75 3 100       58 $self->is_valid
76             or $! = ECONNRESET,
77             return; ## no critic (RequireLocalizedPunctuationVars)
78              
79 2         677 my $seconds = $self->out_timeout;
80             my $result = timeout $seconds, @_ => sub {
81 2     2   195 $self->socket->syswrite(@_);
82 2         58 };
83 2 50       353 if ($@) {
84 0         0 $self->clean();
85 0         0 $! = ETIMEDOUT; ## no critic (RequireLocalizedPunctuationVars)
86             }
87              
88 2         7 $result;
89             }
90              
91             1;
92              
93              
94             =pod
95              
96             =head1 NAME
97              
98             Riak::Light::Timeout::TimeOut - proxy to read/write using Time::Out as a timeout provider
99              
100             =head1 VERSION
101              
102             version 0.12
103              
104             =head1 DESCRIPTION
105              
106             Internal class
107              
108             =head1 AUTHORS
109              
110             =over 4
111              
112             =item *
113              
114             Tiago Peczenyj
115              
116             =item *
117              
118             Damien Krotkine
119              
120             =back
121              
122             =head1 COPYRIGHT AND LICENSE
123              
124             This software is copyright (c) 2013 by Weborama.
125              
126             This is free software; you can redistribute it and/or modify it under
127             the same terms as the Perl 5 programming language system itself.
128              
129             =cut
130              
131              
132             __END__