File Coverage

blib/lib/Riak/Light/Timeout/Select.pm
Criterion Covered Total %
statement 32 35 91.4
branch 6 8 75.0
condition n/a
subroutine 12 12 100.0
pod 0 6 0.0
total 50 61 81.9


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::Select;
11             {
12             $Riak::Light::Timeout::Select::VERSION = '0.12';
13             }
14             ## use critic
15              
16 2     2   1575 use POSIX qw(ETIMEDOUT ECONNRESET);
  2         3  
  2         48  
17 2     2   926 use IO::Select;
  2         1669  
  2         113  
18 2     2   123 use Time::HiRes;
  2         4  
  2         23  
19 2     2   390 use Config;
  2         5  
  2         154  
20 2     2   11 use Moo;
  2         4  
  2         20  
21 2     2   850 use Types::Standard -types;
  2         2  
  2         83  
22              
23             with 'Riak::Light::Timeout';
24              
25             # ABSTRACT: proxy to read/write using IO::Select as a timeout provider
26              
27             has socket => ( is => 'ro', required => 1 );
28             has in_timeout => ( is => 'ro', isa => Num, default => sub {0.5} );
29             has out_timeout => ( is => 'ro', isa => Num, default => sub {0.5} );
30             has select => ( is => 'ro', default => sub { IO::Select->new } );
31              
32             sub BUILD {
33 2     2 0 148 $_[0]->select->add( $_[0]->socket );
34             }
35              
36             sub DEMOLISH {
37 2     2 0 16259 $_[0]->clean();
38             }
39              
40             sub clean {
41 3     3 0 82 $_[0]->select->remove( $_[0]->socket );
42 3         305 $_[0]->socket->close;
43             }
44              
45             sub is_valid {
46 6     6 0 49 scalar $_[0]->select->handles;
47             }
48              
49             sub sysread {
50 3     3 0 6 my $self = shift;
51              
52 3 50       13 $self->is_valid
53             or $! = ECONNRESET,
54             return; ## no critic (RequireLocalizedPunctuationVars)
55              
56 3 100       96 return $self->socket->sysread(@_)
57             if $self->select->can_read( $self->in_timeout );
58              
59 1         500680 $self->clean();
60 1         162 $! = ETIMEDOUT; ## no critic (RequireLocalizedPunctuationVars)
61              
62 1         8 undef;
63             }
64              
65             sub syswrite {
66 3     3 0 9 my $self = shift;
67              
68 3 100       23 $self->is_valid
69             or $! = ECONNRESET,
70             return; ## no critic (RequireLocalizedPunctuationVars)
71              
72 2 50       72 return $self->socket->syswrite(@_)
73             if $self->select->can_write( $self->out_timeout );
74              
75 0           $self->clean();
76 0           $! = ETIMEDOUT; ## no critic (RequireLocalizedPunctuationVars)
77              
78 0           undef;
79             }
80              
81             1;
82              
83              
84             =pod
85              
86             =head1 NAME
87              
88             Riak::Light::Timeout::Select - proxy to read/write using IO::Select as a timeout provider
89              
90             =head1 VERSION
91              
92             version 0.12
93              
94             =head1 DESCRIPTION
95              
96             Internal class
97              
98             =head1 AUTHORS
99              
100             =over 4
101              
102             =item *
103              
104             Tiago Peczenyj
105              
106             =item *
107              
108             Damien Krotkine
109              
110             =back
111              
112             =head1 COPYRIGHT AND LICENSE
113              
114             This software is copyright (c) 2013 by Weborama.
115              
116             This is free software; you can redistribute it and/or modify it under
117             the same terms as the Perl 5 programming language system itself.
118              
119             =cut
120              
121              
122             __END__