YUID/Client.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 237 CC 23 SCAR 49.5
CC
23
Cov
0%
_oneline
306.0
new
20.0
get_sock
12.0
CRAP 140.9
1 # $Id$
2  
3 package Data::YUID::Client;
4 use strict;
5  
6 use fields qw( servers select_timeout hosts connect_timeout );
7 use Carp;
8 use Errno qw( EINPROGRESS EWOULDBLOCK EISCONN );
9 use IO::Socket::INET;
10 use Socket qw( MSG_NOSIGNAL );
11 use URI::Escape ();
12  
13 use constant DEFAULT_PORT => 9001;
14 use constant DEFAULT_TIMEOUT => {
15 connect => 1.0, # seconds
16 select => 1.0, # seconds
17 };
18  
19 our $FLAG_NOSIGNAL = 0;
20 eval { $FLAG_NOSIGNAL = MSG_NOSIGNAL };
21  
22 my $Active_Sock;
23 my $Socket_Used_Count = 0; #yuids served per socket before we refresh it, to force rebalancing
24 my $Max_Socket_Reused = 1000 + rand(1000);
25  
26 sub new {
27 my Data::YUID::Client $client = shift;
28 my %args = @_;
29 $client = fields::new($client) unless ref $client;
30  
31 croak "servers must be an arrayref if specified"
32 unless !exists $args{servers} || ref $args{servers} eq 'ARRAY';
33 $client->{servers} = $args{servers} || [];
34 for (qw(select_timeout connect_timeout)) {
35 my $value = exists $args{$_} ? $args{$_} : DEFAULT_TIMEOUT->{$_};
36 $client->{$_} = $value;
37 }
38 $client->{hosts} = $args{servers};
39  
40 $client;
41 }
42  
43 sub _oneline {
44 my Data::YUID::Client $client = shift;
45 my ($sock, $line) = @_;
46 my $res;
47 my ($ret, $offset) = (undef, 0);
48  
49 # state: 0 - writing, 1 - reading, 2 - done
50 my $state = defined $line ? 0 : 1;
51  
52 # the bitsets for select
53 my ($rin, $rout, $win, $wout);
54 my $nfound;
55  
56 my $copy_state = -1;
57 local $SIG{'PIPE'} = "IGNORE" unless $FLAG_NOSIGNAL;
58  
59 # the select loop
60 while(1) {
61 if ($copy_state!=$state) {
62 last if $state==2;
63 ($rin, $win) = ('', '');
64 vec($rin, fileno($sock), 1) = 1 if $state==1;
65 vec($win, fileno($sock), 1) = 1 if $state==0;
66 $copy_state = $state;
67 }
68 $nfound = select($rout=$rin, $wout=$win, undef,
69 $client->{select_timeout});
70 last unless $nfound;
71  
72 if (vec($wout, fileno($sock), 1)) {
73 $res = send($sock, $line, $FLAG_NOSIGNAL);
74 next
75 if not defined $res and $!==EWOULDBLOCK;
76 unless (($res || 0) > 0) {
77 _close_sock($sock);
78 return undef;
79 }
80 if ($res == length($line)) { # all sent
81 $state = 1;
82 } else { # we only succeeded in sending some of it
83 substr($line, 0, $res, ''); # delete the part we sent
84 }
85 }
86  
87 if (vec($rout, fileno($sock), 1)) {
88 $res = sysread($sock, $ret, 255, $offset);
89 next
90 if !defined($res) and $!==EWOULDBLOCK;
91 if ($res == 0) { # catches 0=conn closed or undef=error
92 _close_sock($sock);
93 return undef;
94 }
95 $offset += $res;
96 if (rindex($ret, "\r\n") + 2 == length($ret)) {
97 $state = 2;
98 }
99 }
100 }
101  
102 unless ($state == 2) {
103 _close_sock($sock);
104 return undef;
105 }
106  
107 return $ret;
108 }
109  
110 sub get_id {
111 my Data::YUID::Client $client = shift;
112 my($ns) = @_;
113 my $id;
114 while (!$id && (my $sock = $client->get_sock)) {
115 my $cmd = sprintf "getid ns=%s\r\n", URI::Escape::uri_escape($ns || '');
116 my $res = $client->_oneline($sock, $cmd) or next;
117 ($id) = $res =~ /^ok\s+id=(\d+)/i;
118 }
119 $id;
120 }
121  
122 sub _close_sock {
123 my($sock) = @_;
124 undef $Active_Sock
125 if ($Active_Sock && fileno($sock) == fileno($Active_Sock));
126 close $sock;
127 }
128  
129 #class method, for request cleanup
130 sub close_active_sock {
131 close $Active_Sock;
132 }
133  
134 sub connect_to_server {
135 my Data::YUID::Client $client = shift;
136 my($host) = @_;
137 my($ip, $port) = split /:/, $host;
138 $port ||= DEFAULT_PORT;
139 my $sock = IO::Socket::INET->new(
140 PeerAddr => $ip,
141 PeerPort => $port,
142 Proto => 'tcp',
143 Type => SOCK_STREAM,
144 ReuseAddr => 1,
145 Blocking => 0,
146 Timeout => $client->{connect_timeout},
147 ) or return;
148 $sock;
149 }
150  
151 sub get_sock {
152 my Data::YUID::Client $client = shift;
153  
154 ++$Socket_Used_Count;
155  
156 if ($Active_Sock && $Active_Sock->connected && ($Socket_Used_Count % $Max_Socket_Reused != 0)) {
157 return $Active_Sock;
158 }else{
159 my $hosts = $client->{hosts};
160 my @try_hosts = @$hosts;
161 my $sock;
162 while (@try_hosts){
163 my $host = splice(@try_hosts, int(rand(@try_hosts)), 1);
164 $sock = $client->connect_to_server($host);
165 last if $sock;
166 }
167 return $Active_Sock = $sock;
168 }
169 }
170  
171 1;
172 __END__