IProto/Connection/Sync.pm

filter: Statementstmt 14.3% 22 / 153 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 35.7% 5 / 14 Podpod 60.0% 3 / 5 totaltotal 10.9% 30 / 274 CC 47 SCAR 52.3
CC
47
Cov
9%
recv_all
380.0
send
210.0
_build__socket
72.0
CRAP 186.1
« Tarantool/Box/Singleton.pm ↑ Summary IProto.pm »
1 package MR::IProto::Connection::Sync;
2  
3 =head1 NAME
4  
5 MR::IProto::Connection::Sync - sync communication
6  
7 =head1 DESCRIPTION
8  
9 Used to perform synchronous communication.
10  
11 =cut
12  
13 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 extends 'MR::IProto::Connection';
15  
16 1 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Socket qw( TCP_NODELAY SO_KEEPALIVE SO_SNDTIMEO SO_RCVTIMEO );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 has _socket => (
21 is => 'ro',
22 isa => 'IO::Socket::INET',
23 predicate => '_has_socket',
24 clearer => '_clear_socket',
25 lazy_build => 1,
26 );
27  
28 has _sent => (
29 is => 'ro',
30 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 1 );
32  
33 1 has last_sync => (
34 is => 'rw',
35 isa => 'Int',
36 );
37  
38 1 has last_error => (
39 is => 'rw',
40 isa => 'Str',
41 default => '',
42 );
43  
44 =head1 PUBLIC METHODS
45  
46 =over
47  
48 =item fh
49  
50 Returns socket.
51  
52 =item send
53  
54 See L<MR::IProto::Connection/send> for more information.
55  
56 =cut
57  
58 0 ▶sub fh { return $_[0]->_has_socket && $_[0]->_socket }
Subroutine fh: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$_[0]->_has_socket && $_[0]->_socket
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]->_has_socket && $_[0]->_socket
$_[0]->_has_socket $_[0]->_socket
Truth table0%0/3
A && B
A= $_[0]->_has_socketB= $_[0]->_socket
ABresult
0X0
100
111
59  
60 0 ▶sub Close {
Subroutine Close: not called CC=1 SCAR=6.9
Pod undocumented
61 0 my ($self, $reason) = @_;
62 0 $self->_handle_error(undef, undef, $reason);
63 }
64  
65 0 ▶sub send {
Subroutine send: not called CC=14 SCAR=53.5
Pod documented
66 0 my ($self, $msg, $payload, $callback, $no_reply, $sync) = @_;
67 0 my $server = $self->server;
68 0 my $sent = $self->_sent;
69 0 my $ok = eval {
70 0 ▶ if(defined $sync) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sync) { } 0 0 0
71 0 ▶ die "Sync $sync already sent" if exists $sent->{$sync};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sent->{$sync} 0 0 0
72 } else {
73 0 ▶ 1 while exists $sent->{$sync = $self->_choose_sync()};
Branch: 1 branch
BranchTrueFalseTotal
exists $$sent{$sync = $self->_choose_sync} and do { '???' } 0 0 0
74 }
75 0 $server->_send_started($sync, $msg, $payload);
76  
77 0 my $socket = $self->_socket;
78 0 ▶ unless (%$sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$sent) 0 0 0
79 0 vec((my $rin = ''), fileno($socket), 1) = 1;
80 0 ▶ if (select((my $rout = $rin), undef, undef, 0) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (select(my $rout = $rin, undef, undef, 0) > 0) 0 0 0
81 0 ▶ if (sysread($socket, my $buf, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $socket, my $buf, 1) { } 0 0 0
82 0 die "More then 0 bytes was received when nothing was waited for";
83 } else {
84 # Connection was closed by other side, socket is in CLOSE_WAIT state, reconnecting
85 0 $self->_clear_socket();
86 0 $socket = $self->_socket;
87 }
88 }
89 }
90  
91 0 my $header = $self->_pack_header($msg, length $payload, $sync);
92 0 ▶ if( $server->debug >= 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server->debug >= 5) 0 0 0
93 0 $server->_debug_dump('send header: ', $header);
94 0 $server->_debug_dump('send payload: ', $payload);
95 }
96  
97 0 my $write = $header . $payload;
98 0 ▶ while( length $write ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $write) 0 0 0
99 0 my $written = syswrite($socket, $write);
100 0 ▶ if (!defined $written) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $written) { } 0 0 0
101 0 ▶ if ($! != Errno::EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 4) 0 0 0
102 0 ▶ $! = Errno::ETIMEDOUT if $! == Errno::EAGAIN; # Hack over SO_SNDTIMEO behaviour
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 0 0 0
103 0 die "send: $!";
104 }
105 } else {
106 0 substr $write, 0, $written, '';
107 }
108 }
109 0 1;
110 };
111 0 my $err = $@;
112 0 $self->last_sync($sync);
113 0 ▶ if($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 0 0
114 0 ▶ if ($no_reply) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_reply) { } 0 0 0
115 0 $callback->(undef, undef);
116 0 $server->_recv_finished($sync, undef, undef);
117 } else {
118 0 $sent->{$sync} = $callback;
119 }
120 }
121 else {
122 0 $self->_handle_error($sync, $callback, $err);
123 }
124 0 return $ok;
125 }
126  
127 0 ▶sub recv_all {
Subroutine recv_all: not called CC=19 SCAR=59.4
Pod undocumented
128 0 my ($self, %opts) = @_;
129 0 my $server = $self->server;
130 0 my $sent = $self->_sent;
131 0 my $dump_resp = $server->debug >= 6;
132 0 my @sync = keys %$sent;
133 0 ▶ my $n = $opts{max} || @sync;
Condition0%0/3
$opts{'max'} || @sync
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'max'} || @sync
$opts{'max'} @sync
Truth table0%0/3
A || B
A= $opts{'max'}B= @sync
ABresult
1X1
011
000
134 0 ▶ while ($n-- and %$sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n-- and %$sent) 0 0 0
Condition0%0/3
$n-- and %$sent
!ll&&!rl&&r
000
MC/DC0%0/2
$n-- and %$sent
$n-- %$sent
Truth table0%0/3
A and B
A= $n--B= %$sent
ABresult
0X0
100
111
135 0 my ($resp_msg, $resp_payload);
136 0 my ($sync,$callback);
137 0 my $ok = eval {
138 0 my $socket = $self->_socket;
139 0 my $resp_header;
140 0 my $to_read = 12;
141 0 ▶ while( $to_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_read) 0 0 0
142 0 my $read = sysread($socket, my $buf, $to_read);
143 0 ▶ if (!defined $read) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $read) { } 0 0 0
elsif ($read == 0) { } 0 0 0
144 0 ▶ if ($! != Errno::EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 4) 0 0 0
145 0 ▶ $! = Errno::ETIMEDOUT if $! == Errno::EAGAIN; # Hack over SO_RCVTIMEO behaviour
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 0 0 0
146 0 die "recv: $!";
147 }
148 } elsif ($read == 0) {
149 0 die "recv: Unexpected end-of-file";
150 } else {
151 0 $resp_header .= $buf;
152 0 $to_read -= $read;
153 }
154 }
155 0 ▶ $server->_debug_dump('recv header: ', $resp_header) if $dump_resp;
Branch: 1 branch
BranchTrueFalseTotal
if $dump_resp 0 0 0
156 0 ($resp_msg, my $resp_length, $sync) = $self->_unpack_header($resp_header);
157 0 ▶ $callback = delete $sent->{$sync} or die "Reply sync $sync not found";
Branch: 1 branch
BranchTrueFalseTotal
$callback = delete $sent->{$sync} or die "Reply sync $sync not found" 0 0 0
158 #die "Request and reply sync is different: $resp_sync != $sync" unless $resp_sync == $sync;
159  
160 0 $to_read = $resp_length;
161 0 ▶ while( $to_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_read) 0 0 0
162 0 my $read = sysread($socket, my $buf, $to_read);
163 0 ▶ if (!defined $read) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $read) { } 0 0 0
elsif ($read == 0) { } 0 0 0
164 0 ▶ if ($! != Errno::EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 4) 0 0 0
165 0 ▶ $! = Errno::ETIMEDOUT if $! == Errno::EAGAIN; # Hack over SO_RCVTIMEO behaviour
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 0 0 0
166 0 die "recv: $!";
167 }
168 } elsif ($read == 0) {
169 0 die "recv: Unexpected end-of-file";
170 } else {
171 0 $resp_payload .= $buf;
172 0 $to_read -= $read;
173 }
174 }
175 0 ▶ $server->_debug_dump('recv payload: ', $resp_payload) if $dump_resp;
Branch: 1 branch
BranchTrueFalseTotal
if $dump_resp 0 0 0
176 0 1;
177 };
178 0 ▶ if($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 0 0
179 0 $server->_recv_finished($sync, $resp_msg, $resp_payload);
180 0 ▶ die "No Callback" unless $callback;
Branch: 1 branch
BranchTrueFalseTotal
unless $callback 0 0 0
181 0 $callback->($resp_msg, $resp_payload);
182 }
183 else {
184 0 $self->_handle_error(undef, undef, $@);
185 }
186 }
187 0 return;
188 }
189  
190 =item set_timeout( $timeout )
191  
192 Set timeout value for existing connection.
193  
194 =cut
195  
196 0 ▶sub set_timeout {
Subroutine set_timeout: not called CC=2 SCAR=17.9
Pod documented
197 0 my ($self, $timeout) = @_;
198 0 ▶ $self->_set_timeout($self->_socket, $timeout) if $self->_has_socket();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_socket 0 0 0
199 0 return;
200 }
201  
202 =back
203  
204 =cut
205  
206 0 ▶sub _build__socket {
Subroutine _build__socket: not called CC=8 SCAR=42.8
207 0 my ($self) = @_;
208 0 my $server = $self->server;
209 0 ▶ $server->_debug("connecting") if $server->debug >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $server->debug >= 4 0 0 0
210 my $socket = IO::Socket::INET->new(
211 PeerHost => $self->host,
212 PeerPort => $self->port,
213 Proto => 'tcp',
214 Timeout => $self->connect_timeout,
215 0 ▶ ) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("PeerHost", $self->host, "PeerPort", $self->port, "Proto", "tcp", "Timeout", $self->connect_timeout) or do { $@ =~ s/^IO::Socket::INET: (?:connect: )?//; if ($@ eq "timeout") { $! = 110; $@ = "$!"; } ; die "connect: $@" } 0 0 0
216 0 $@ =~ s/^IO::Socket::INET: (?:connect: )?//;
217 0 ▶ if ($@ eq 'timeout') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ eq "timeout") 0 0 0
218 # Hack over IO::Socket behaviour
219 0 $! = Errno::ETIMEDOUT;
220 0 $@ = "$!";
221 }
222 0 die "connect: $@";
223 };
224 0 ▶ $socket->sockopt(SO_KEEPALIVE, 1) if $self->tcp_keepalive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tcp_keepalive 0 0 0
225 0 ▶ $socket->setsockopt((getprotobyname('tcp'))[2], TCP_NODELAY, 1) if $self->tcp_nodelay;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tcp_nodelay 0 0 0
226 0 ▶ $self->_set_timeout($socket, $self->timeout) if $self->timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $self->timeout 0 0 0
227 0 ▶ $server->_debug("connected") if $server->debug >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $server->debug >= 4 0 0 0
228 0 return $socket;
229 }
230  
231 0 ▶sub _set_timeout {
Subroutine _set_timeout: not called CC=1 SCAR=6.9
232 0 my ($self, $socket, $timeout) = @_;
233 0 my $sec = int $timeout; # seconds
234 0 my $usec = int( ($timeout - $sec) * 1_000_000 ); # micro-seconds
235 0 my $timeval = pack "L!L!", $sec, $usec; # struct timeval;
236 0 $socket->sockopt(SO_SNDTIMEO, $timeval);
237 0 $socket->sockopt(SO_RCVTIMEO, $timeval);
238 0 return;
239 }
240  
241 0 ▶sub _handle_error {
Subroutine _handle_error: not called CC=7 SCAR=40.3
242 0 my ($self, $sync, $callback, $error) = @_;
243 0 my $errno = $!;
244 0 ▶ if (!$error) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $error) { } 0 0 0
elsif ($error =~ /^(.+?) at \S+ line \d+/s) { } 0 0 0
245 0 $error = 'Unknown error';
246 } elsif ($error =~ /^(.+?) at \S+ line \d+/s) {
247 0 $error = $1;
248 }
249 0 $self->last_error($error);
250 0 my $server = $self->server;
251 0 $server->_debug("error: $error");
252 0 ▶ if($self->_has_socket()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_socket) 0 0 0
253 0 close($self->_socket);
254 0 $self->_clear_socket();
255 }
256 0 $server->active(0);
257 0 my $sent = $self->_sent;
258 0 ▶ if($sync && $callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sync and $callback) 0 0 0
Condition0%0/3
$sync and $callback
!ll&&!rl&&r
000
MC/DC0%0/2
$sync and $callback
$sync $callback
Truth table0%0/3
A and B
A= $syncB= $callback
ABresult
0X0
100
111
259 0 $server->_recv_finished($sync, undef, undef, $error, $errno);
260 0 $callback->(undef, undef, $error, $errno);
261 0 delete $sent->{$sync};
262 }
263 0 foreach my $sync (keys %$sent) {
264 0 $server->_recv_finished($sync, undef, undef, $error, $errno);
265 0 $sent->{$sync}->(undef, undef, $error, $errno);
266 }
267 0 undef %$sent;
268 0 return;
269 }
270  
271 =head1 SEE ALSO
272  
273 L<MR::IProto::Connection>, L<MR::IProto::Cluster::Server>.
274  
275 =cut
276  
277 1 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
278 1 __PACKAGE__->meta->make_immutable();
279  
280 1 1;
« Tarantool/Box/Singleton.pm ↑ Summary IProto.pm »