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.10';
13             }
14             ## use critic
15              
16 2     2   52215 use POSIX qw(ETIMEDOUT ECONNRESET);
  2         4  
  2         16  
17 2     2   1649 use Time::Out qw(timeout);
  2         1917  
  2         124  
18 2     2   15 use Time::HiRes;
  2         5  
  2         10  
19 2     2   680 use Riak::Light::Util qw(is_windows);
  2         6  
  2         96  
20 2     2   11 use Carp;
  2         4  
  2         131  
21 2     2   13 use Moo;
  2         5  
  2         20  
22 2     2   885 use Types::Standard -types;
  2         5  
  2         29  
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 210 croak "Time::Out alarm(2) doesn't interrupt blocking I/O on MSWin32!"
39             if is_windows();
40              
41 2         120 carp "Not Safe: can clobber previous alarm";
42             }
43              
44             sub clean {
45 1     1 0 65 $_[0]->socket->close;
46 1         228 $_[0]->is_valid(0);
47             }
48              
49             sub sysread {
50 3     3 0 7 my $self = shift;
51 3 50       77 $self->is_valid
52             or $! = ECONNRESET,
53             return; ## no critic (RequireLocalizedPunctuationVars)
54              
55 3         23 my $buffer;
56 3         16 my $seconds = $self->in_timeout;
57             my $result = timeout $seconds, @_ => sub {
58 3     3   159 my $readed = $self->socket->sysread(@_);
59 3         500498 $buffer = $_[0]; # NECESSARY, timeout does not map the alias @_ !!
60 2         6 $readed;
61 3         36 };
62 3 100       737 if ($@) {
63 1         8 $self->clean();
64 1         41 $! = ETIMEDOUT; ## no critic (RequireLocalizedPunctuationVars)
65             }
66             else {
67 2         882 $_[0] = $buffer;
68             }
69              
70 3         15 $result;
71             }
72              
73             sub syswrite {
74 3     3 0 7 my $self = shift;
75 3 100       70 $self->is_valid
76             or $! = ECONNRESET,
77             return; ## no critic (RequireLocalizedPunctuationVars)
78              
79 2         765 my $seconds = $self->out_timeout;
80             my $result = timeout $seconds, @_ => sub {
81 2     2   536 $self->socket->syswrite(@_);
82 2         95 };
83 2 50       355 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             =encoding UTF-8
97              
98             =head1 NAME
99              
100             Riak::Light::Timeout::TimeOut - proxy to read/write using Time::Out as a timeout provider
101              
102             =head1 VERSION
103              
104             version 0.10
105              
106             =head1 DESCRIPTION
107              
108             Internal class
109              
110             =head1 AUTHORS
111              
112             =over 4
113              
114             =item *
115              
116             Tiago Peczenyj
117              
118             =item *
119              
120             Damien Krotkine
121              
122             =back
123              
124             =head1 COPYRIGHT AND LICENSE
125              
126             This software is copyright (c) 2013 by Weborama.
127              
128             This is free software; you can redistribute it and/or modify it under
129             the same terms as the Perl 5 programming language system itself.
130              
131             =cut
132              
133              
134             __END__