blib/lib/XAS/Lib/Mixins/Keepalive.pm

filter: Statementstmt 31.8% 14 / 44 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 3 / 7 Podpod 100% 2 / 2 totaltotal 28.3% 19 / 67 CC 8 SCAR 28.3
CC
8
Cov
24%
init_keepalive
30.0
__ANON__
20.0
enable_keepalive
2.0
CRAP 16.9
« blib/lib/XAS/Lib/Mixins/JSON/Server.pm ↑ Summary blib/lib/XAS/Exception.pm »
1 package XAS::Lib::Mixins::Keepalive;
2  
3 1 our $VERSION = '0.01';
4  
5 1 our $TCP_KEEPCNT = 0;
6 1 our $TCP_KEEPIDLE = 0;
7 1 our $TCP_KEEPINTVL = 0;
8  
9 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Socket ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use XAS::Class
13 1 debug => 0,
14 version => $VERSION,
15 base => 'XAS::Base',
16 accessors => 'tcp_keepidle tcp_keepcnt tcp_keepintvl',
17 mixins => 'init_keepalive enable_keepalive tcp_keepidle tcp_keepcnt tcp_keepintvl',
18 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # ----------------------------------------------------------------------
21 # Public Methods
22 # ----------------------------------------------------------------------
23  
24 sub enable_keepalive {
25 0 ▶ my $self = shift;
Subroutine enable_keepalive: not called CC=1 SCAR=6.9
Pod documented
26 0 my ($socket) = $self->validate_params(\@_, [1]);
27  
28 # turn keepalive on, this should send a keepalive
29 # packet once every 2 hours according to the RFC.
30  
31 0 setsockopt($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
32  
33 # adjust the system defaults, all values are in seconds.
34 # so this does the following:
35 # every 15 minutes send up to 3 packets at 5 second intervals
36 # if no reply, the connection is down.
37  
38 0 setsockopt($socket, IPPROTO_TCP, $TCP_KEEPINTVL, $self->tcp_keepintvl);
39 0 setsockopt($socket, IPPROTO_TCP, $TCP_KEEPIDLE, $self->tcp_keepidle);
40 0 setsockopt($socket, IPPROTO_TCP, $TCP_KEEPCNT, $self->tcp_keepcnt);
41  
42 }
43  
44 # ----------------------------------------------------------------------
45 # Private Methods
46 # ----------------------------------------------------------------------
47  
48 sub init_keepalive {
49 0 ▶ my $self = shift;
Subroutine init_keepalive: not called CC=5 SCAR=34.0
Pod documented
50 0 my $p = $self->validate_params(\@_, {
51 -tcp_keepcnt => { optional => 1, default => 3 }, # number of packets
52 -tcp_keepidle => { optional => 1, default => 900 }, # 15 minutes
53 -tcp_keepintvl => { optional => 1, default => 5 }, # interval seconds
54 });
55
56 0 $self->{tcp_keepcnt} = $p->{tcp_keepcnt};
57 0 $self->{tcp_keepidle} = $p->{tcp_keepidle};
58 0 $self->{tcp_keepintvl} = $p->{tcp_keepintvl};
59
60 # implement socket level keepalive, what a mess...
61  
62 0 ▶ if ( $] < 5.014 ) { # check perl's version
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.014) { } 0 0 0
63  
64 # at this point we can only support the below. if you have
65 # access to your systems header files, you could provide
66 # the following values. I would be happy to include them.
67  
68 0 ▶ if ($^O eq "aix") { # from /usr/include/netinet/tcp.h
Branch: 3 branches
BranchTrueFalseTotal
if ($^O eq 'aix') { } 0 0 0
elsif ($^O eq 'linux') { } 0 0 0
elsif ($^O eq 'vms') { } 0 0 0
69  
70 0 $TCP_KEEPIDLE = 0x11;
71 0 $TCP_KEEPINTVL = 0x12;
72 0 $TCP_KEEPCNT = 0x13;
73  
74 } elsif ($^O eq "linux") { # from /usr/include/netinet/tcp.h
75  
76 0 $TCP_KEEPIDLE = 4;
77 0 $TCP_KEEPINTVL = 5;
78 0 $TCP_KEEPCNT = 6;
79  
80 } elsif ($^O eq 'vms') { # from TCP in sys$library:decc$rtldef.tlb
81  
82 0 $TCP_KEEPIDLE = 0x04;
83 0 $TCP_KEEPINTVL = 0x05;
84 0 $TCP_KEEPCNT = 0x06;
85  
86 }
87  
88 } else {
89  
90 try {
91  
92 # hmmm, maybe perl will do it for us. checking to see if the
93 # platform implements these macros.
94  
95 0 ▶ $TCP_KEEPCNT = Socket::TCP_KEEPCNT() if (UNIVERSAL::can('Socket', 'TCP_KEEPCNT'));
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can("Socket", "TCP_KEEPCNT") 0 0 0
96 0 ▶ $TCP_KEEPIDLE = Socket::TCP_KEEPIDLE() if (UNIVERSAL::can('Socket', 'TCP_KEEPIDLE'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can("Socket", "TCP_KEEPIDLE") 0 0 0
97 0 ▶ $TCP_KEEPINTVL = Socket::TCP_KEEPINTVL() if (UNIVERSAL::can('Socket', 'TCP_KEEPINTVL'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can("Socket", "TCP_KEEPINTVL") 0 0 0
98  
99 } catch {
100  
101 # nope, guess not...
102  
103 0 ▶ my $ex = $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 my ($err) = m/(.*,)/;
105 0 chop($err);
106  
107 0 $self->log->warn(lcfirst($err));
108  
109 0 };
110  
111 }
112  
113 }
114  
115 1 1;
116  
117 __END__
118  
119 =head1 NAME
120  
121 XAS::Lib::Mixin::Keepalive - A mixin to implement TCP keepalive
122  
123 =head1 SYNOPSIS
124  
125 use XAS::Class
126 version => '0.01',
127 base => 'XAS::Base',
128 mixin => 'XAS::Lib::Mixin::Keepalive'
129 ;
130  
131 =head1 DESCRIPTION
132  
133 This module is a mixin class to share code for initializing TCP level
134 keepalives.
135  
136 =head1 METHODS
137  
138 =head2 init_keepalive
139  
140 This will attempt to define the necessary variables to allow TCP keepalive
141 to function. Not all Perl's and OS's define the necessary values.
142  
143 =head2 enable_keepalive($socket)
144  
145 This will enable keepalive on the given socket. By default this will
146 initialize keepalive to the RFC minimal, i.e. send a keepalive packet
147 once every 2 hours. If the OS supports it, this will be modified to
148 send up to 3 keepalive packets once every 15 minutes.
149  
150 This should fix those pesky firewalls...
151  
152 =over 4
153  
154 =item B<$socket>
155  
156 The socket to enable keepalive on.
157  
158 =back
159  
160 =head1 SEE ALSO
161  
162 =over 4
163  
164 =item L<XAS|XAS>
165  
166 =back
167  
168 =head1 AUTHOR
169  
170 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
171  
172 =head1 COPYRIGHT AND LICENSE
173  
174 Copyright (C) 2014 Kevin L. Esteb
175  
176 This is free software; you can redistribute it and/or modify it under
177 the terms of the Artistic License 2.0. For details, see the full text
178 of the license at http://www.perlfoundation.org/artistic_license_2_0.
179  
180 =cut
« blib/lib/XAS/Lib/Mixins/JSON/Server.pm ↑ Summary blib/lib/XAS/Exception.pm »