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