File Coverage

blib/lib/Riak/Light/Timeout/SelectOnRead.pm
Criterion Covered Total %
statement 35 35 100.0
branch 5 6 83.3
condition n/a
subroutine 13 13 100.0
pod 0 6 0.0
total 53 60 88.3


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::SelectOnRead;
11             {
12             $Riak::Light::Timeout::SelectOnRead::VERSION = '0.12';
13             }
14             ## use critic
15              
16 2     2   1537 use POSIX qw(ETIMEDOUT ECONNRESET);
  2         6  
  2         40  
17 2     2   373 use IO::Select;
  2         11  
  2         87  
18 2     2   176 use Time::HiRes;
  2         10  
  2         34  
19 2     2   371 use Config;
  2         3  
  2         116  
20 2     2   9 use Carp;
  2         2  
  2         126  
21 2     2   8 use Moo;
  2         3  
  2         33  
22 2     2   814 use Types::Standard -types;
  2         3  
  2         62  
23              
24             with 'Riak::Light::Timeout';
25              
26             # ABSTRACT: proxy to read/write using IO::Select as a timeout provider only for READ operations
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 select => ( is => 'ro', default => sub { IO::Select->new } );
32              
33             sub BUILD {
34              
35             #carp "Should block in Write Operations, be careful";
36              
37 2     2 0 109 $_[0]->select->add( $_[0]->socket );
38             }
39              
40             sub DEMOLISH {
41 2     2 0 15419 $_[0]->clean();
42             }
43              
44             sub clean {
45 3     3 0 51 $_[0]->select->remove( $_[0]->socket );
46 3         272 $_[0]->socket->close;
47 3         339 $! = ETIMEDOUT; ## no critic (RequireLocalizedPunctuationVars)
48             }
49              
50             sub is_valid {
51 6     6 0 33 scalar $_[0]->select->handles;
52             }
53              
54             sub sysread {
55 3     3 0 4 my $self = shift;
56 3 50       8 $self->is_valid
57             or $! = ECONNRESET,
58             return; ## no critic (RequireLocalizedPunctuationVars)
59              
60 3 100       67 return $self->socket->sysread(@_)
61             if $self->select->can_read( $self->in_timeout );
62              
63 1         500646 $self->clean();
64              
65 1         7 undef;
66             }
67              
68             sub syswrite {
69 3     3 0 6 my $self = shift;
70 3 100       20 $self->is_valid
71             or $! = ECONNRESET,
72             return; ## no critic (RequireLocalizedPunctuationVars)
73 2         85 $self->socket->syswrite(@_);
74             }
75              
76             1;
77              
78              
79             __END__