Clacks/Client.pm

filter: Statementstmt 7.8% 59 / 756 Branchbran 0.0% 0 / 402 Conditioncond 0.0% 0 / 123 MC/DCmcdc 0.0% 0 / 80 Subroutinesub 35.1% 19 / 54 Podpod 100% 30 / 30 totaltotal 7.4% 108 / 1445 CC 253 SCAR 55.0
CC
253
Cov
5%
doNetwork
992.0
getNext
420.0
clientlist
380.0
CRAP 245.5
« Clacks/Server.pm ↑ Summary Clacks/PostgreSQL2Clacks.pm »
1 package Net::Clacks::Client;
2 #---AUTOPRAGMASTART---
3 4 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Carp qw[carp croak confess cluck longmess shortmess];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 our $VERSION = 38;
10 4 ▶use autodie qw( close );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Array::Contains;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Encode qw(is_utf8 encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use builtin qw[true false is_bool];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶no warnings qw(experimental::builtin); ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #---AUTOPRAGMAEND---
18  
19 4 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 #use IO::Socket::UNIX;
21 4 ▶use Time::HiRes qw[sleep usleep time];
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 ▶sub new($class, $server, $port, $username, $password, $clientname, $iscaching = 0) {
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
28 0 my $self = bless {}, $class;
29  
30 0 ▶ if(!defined($server) || !length($server)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $server and length $server) 0 0 0
Condition0%0/3
defined $server and length $server
!ll&&!rl&&r
000
MC/DC0%0/2
defined $server and length $server
defined $server length $server
Truth table0%0/3
A and B
A= defined $serverB= length $server
ABresult
0X0
100
111
31 0 croak("server not defined!");
32 }
33 0 ▶ if(!defined($port) || !length($port)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port and length $port) 0 0 0
Condition0%0/3
defined $port and length $port
!ll&&!rl&&r
000
MC/DC0%0/2
defined $port and length $port
defined $port length $port
Truth table0%0/3
A and B
A= defined $portB= length $port
ABresult
0X0
100
111
34 0 croak("port not defined!");
35 }
36 0 ▶ if(!defined($username) || !length($username)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username and length $username) 0 0 0
Condition0%0/3
defined $username and length $username
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and length $username
defined $username length $username
Truth table0%0/3
A and B
A= defined $usernameB= length $username
ABresult
0X0
100
111
37 0 croak("username not defined!");
38 }
39 0 ▶ if(!defined($password) || !length($password)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password and length $password) 0 0 0
Condition0%0/3
defined $password and length $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $password and length $password
defined $password length $password
Truth table0%0/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
40 0 croak("password not defined!");
41 }
42 0 ▶ if(!defined($clientname) || !length($clientname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clientname and length $clientname) 0 0 0
Condition0%0/3
defined $clientname and length $clientname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $clientname and length $clientname
defined $clientname length $clientname
Truth table0%0/3
A and B
A= defined $clientnameB= length $clientname
ABresult
0X0
100
111
43 0 croak("clientname not defined!");
44 }
45  
46 0 $self->{server} = $server;
47 0 $self->{port} = $port;
48  
49 0 $self->init($username, $password, $clientname, $iscaching);
50  
51 0 return $self;
52 }
53  
54 0 ▶sub newSocket($class, $socketpath, $username, $password, $clientname, $iscaching = 0) {
Subroutine newSocket: not called CC=11 SCAR=48.8
Pod documented
55 0 my $self = bless {}, $class;
56  
57 0 ▶ if(!defined($socketpath) || !length($socketpath)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socketpath and length $socketpath) 0 0 0
Condition0%0/3
defined $socketpath and length $socketpath
!ll&&!rl&&r
000
MC/DC0%0/2
defined $socketpath and length $socketpath
defined $socketpath length $socketpath
Truth table0%0/3
A and B
A= defined $socketpathB= length $socketpath
ABresult
0X0
100
111
58 0 croak("socketpath not defined!");
59 }
60 0 ▶ if(!defined($username) || !length($username)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username and length $username) 0 0 0
Condition0%0/3
defined $username and length $username
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and length $username
defined $username length $username
Truth table0%0/3
A and B
A= defined $usernameB= length $username
ABresult
0X0
100
111
61 0 croak("username not defined!");
62 }
63 0 ▶ if(!defined($password) || !length($password)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password and length $password) 0 0 0
Condition0%0/3
defined $password and length $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $password and length $password
defined $password length $password
Truth table0%0/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
64 0 croak("password not defined!");
65 }
66 0 ▶ if(!defined($clientname) || !length($clientname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clientname and length $clientname) 0 0 0
Condition0%0/3
defined $clientname and length $clientname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $clientname and length $clientname
defined $clientname length $clientname
Truth table0%0/3
A and B
A= defined $clientnameB= length $clientname
ABresult
0X0
100
111
67 0 croak("clientname not defined!");
68 }
69  
70 0 my $udsloaded = 0;
71 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
72 0 require IO::Socket::UNIX;
73 0 $udsloaded = 1;
74 };
75 0 ▶ if(!$udsloaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($udsloaded) 0 0 0
76 0 croak("Specified a unix domain socket, but i couldn't load IO::Socket::UNIX!");
77 }
78  
79 0 $self->{socketpath} = $socketpath;
80  
81 0 $self->init($username, $password, $clientname, $iscaching);
82  
83 0 return $self;
84 }
85  
86 0 ▶sub init($self, $username, $password, $clientname, $iscaching) {
Subroutine init: not called CC=9 SCAR=45.0
Pod documented
87 0 ▶ if(!defined($username) || $username eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $username or $username eq "") 0 0 0
Condition0%0/3
not defined $username or $username eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $username or $username eq ""
not defined $username $username eq ""
Truth table0%0/3
not(A) or B
A= not defined $usernameB= $username eq ""
ABresult
1X1
011
000
88 0 croak("Username not defined!");
89 }
90 0 ▶ if(!defined($password) || $password eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $password or $password eq "") 0 0 0
Condition0%0/3
not defined $password or $password eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $password or $password eq ""
not defined $password $password eq ""
Truth table0%0/3
not(A) or B
A= not defined $passwordB= $password eq ""
ABresult
1X1
011
000
91 0 croak("Password not defined!");
92 }
93  
94 0 ▶ if(!defined($clientname || $clientname eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($clientname || $clientname eq "")) 0 0 0
Condition0%0/3
$clientname || $clientname eq ''
l!l&&r!l&&!r
000
MC/DC0%0/2
$clientname || $clientname eq ''
$clientname $clientname eq ''
Truth table0%0/3
A || B
A= $clientnameB= $clientname eq ''
ABresult
1X1
011
000
95 0 croak("Clientname not defined!");
96 }
97 0 $self->{clientname} = $clientname;
98  
99 0 $self->{authtoken} = encode_base64($username, '') . ':' . encode_base64($password, '');
100  
101 0 ▶ if(!defined($iscaching)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $iscaching) 0 0 0
102 0 $iscaching = 0;
103 }
104 0 $self->{iscaching} = $iscaching;
105  
106 0 ▶ if($self->{iscaching}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"iscaching"}) 0 0 0
107 0 $self->{cache} = {};
108 }
109  
110 0 $self->{needreconnect} = 1;
111 0 $self->{inlines} = [];
112 0 $self->{firstconnect} = 1;
113  
114 # Maximum number of seconds any synchronous request (retrieve, flush, keylist,
115 # clientlist) will wait for a server response before giving up. Without this
116 # cap a hung or silently-dropped server connection turned into an indefinite
117 # client-side hang. Callers can override this after construction.
118 0 $self->{requesttimeout} = 30;
119  
120 # Whether this client has told the server to stop expecting pings.
121 #
122 # NOPING is CONNECTION STATE, not a one-off command, so it has to be
123 # remembered and replayed. reconnect() rebuilds the outbuffer from
124 # nothing, so before this flag existed the NOPING was silently dropped by
125 # the first reconnect and never sent again: a client that had been
126 # deliberately exempted from the server's ping timeout quietly became
127 # subject to it, and was hung up on after pingtimeout seconds of the
128 # idleness it was exempted for in the first place. Worse, the reconnects
129 # that lost it are triggered by exactly the errors that make a client go
130 # quiet. Found on a PageCamel web backend 2026-09-03, whose ClacksCache
131 # calls disablePing() once at connect and never again.
132 0 $self->{noping} = 0;
133  
134 # Maximum time to spend trying to drain the outbuffer when the server isn't
135 # reading (kernel send buffer full -> syswrite returns EAGAIN repeatedly).
136 # Tracked in doNetwork() via writefailtime; on expiry the connection is
137 # marked for reconnect. This is the symmetric counterpart of the server's
138 # stalledwritetimeout and is what stops the many "send loop" sites that
139 # otherwise spin forever when the server is alive but not consuming.
140 0 $self->{stalledwritetimeout} = 30;
141  
142 # TCP connect timeout. Without this, a TCP connect to an unreachable host
143 # waits for the kernel SYN-retransmit timeout (~75 s on Linux) before
144 # giving up. Defaulted generously so a transiently-busy server (full
145 # listen backlog, scheduler delays under load) or a slow link doesn't
146 # produce false-positive failures. Unix-domain sockets are not affected —
147 # connect there fails or succeeds immediately.
148 0 $self->{connecttimeout} = 60;
149  
150 # TLS handshake timeout. Implemented via deferred handshake + IO::Select
151 # polling so we don't depend on SIGALRM, which is unsafe in applications
152 # that already use signals or have their own event loop. Defaulted
153 # generously to tolerate slow servers (RSA signing on a small CPU is not
154 # instant) and high-RTT links.
155 0 $self->{ssltimeout} = 60;
156  
157 0 $self->{memcached_compatibility} = 0;
158  
159 $self->{remembrancenames} = [
160 0 'Ivy Bdubs',
161 'Terry Pratchett',
162 'Sven Guckes',
163 'Sheila', # faithful four-legged family member of @NightStorm_KPC
164 'Dolly Parton',
165 ];
166 0 $self->{remembranceinterval} = 3600; # One hour
167 0 $self->{nextremembrance} = time + $self->{remembranceinterval};
168  
169 0 $self->reconnect();
170  
171 0 return;
172 }
173  
174 0 ▶sub _safeCloseSocket($self, $socket) {
Subroutine _safeCloseSocket: not called CC=3 SCAR=24.8
175 0 ▶ return if(!defined($socket));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $socket 0 0 0
176 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
177 0 ▶ if(ref($socket) =~ /^IO::Socket::SSL/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($socket) =~ /^IO::Socket::SSL/u) { } 0 0 0
178 0 $socket->close(SSL_no_shutdown => 1, SSL_fast_shutdown => 1);
179 } else {
180 0 $socket->close;
181 }
182 };
183 0 return;
184 }
185  
186 # Perform a TLS handshake with our own deadline, without using SIGALRM.
187 # Strategy: put the socket into non-blocking mode up front, defer the handshake
188 # at start_SSL (SSL_startHandshake => 0), then drive connect_SSL() ourselves and
189 # poll with IO::Select::can_read / can_write between attempts. This is safe in
190 # applications that already use signals or have their own event loop.
191 #
192 # Returns the wrapped socket on success, or undef on timeout / handshake error.
193 # On failure, the caller is responsible for closing the original $socket.
194 0 ▶sub _sslHandshakeWithTimeout($self, $socket, $timeout) {
Subroutine _sslHandshakeWithTimeout: not called CC=6 SCAR=37.4
195 0 $socket->blocking(0);
196 0 my $wrapped = IO::Socket::SSL->start_SSL($socket,
197 SSL_verify_mode => SSL_VERIFY_NONE,
198 SSL_startHandshake => 0,
199 );
200 0 ▶ if(!$wrapped) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wrapped) 0 0 0
201 0 return;
202 }
203  
204 0 my $deadline = time + $timeout;
205 0 my $select = IO::Select->new($wrapped);
206 0 while(1) {
207 0 ▶ if($wrapped->connect_SSL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrapped->connect_SSL) 0 0 0
208 0 return $wrapped;
209 }
210 # connect_SSL returned false; figure out why and (maybe) wait for the
211 # right kind of socket readiness.
212 0 my $remaining = $deadline - time;
213 0 ▶ if($remaining <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining <= 0) 0 0 0
214 0 return;
215 }
216 0 ▶ if($SSL_ERROR == SSL_WANT_READ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Net::Clacks::Client::SSL_ERROR == 96273243778680) { } 0 0 0
elsif ($Net::Clacks::Client::SSL_ERROR == 96273243779016) { } 0 0 0
217 0 $select->can_read($remaining);
218 } elsif($SSL_ERROR == SSL_WANT_WRITE) {
219 0 $select->can_write($remaining);
220 } else {
221 # Real handshake error (cert mismatch, protocol error, etc.).
222 0 return;
223 }
224 }
225 }
226  
227 0 ▶sub reconnect($self) {
Subroutine reconnect: not called CC=14 SCAR=53.5
Pod documented
228 # Tear the old connection down: remove from the selector first (otherwise the
229 # selector keeps the handle alive and the OS FD stays open), then explicitly
230 # close. Without the explicit close, server-side FDs accumulate until the
231 # ping timeout — under reconnect churn the server can run out of FDs.
232 0 ▶ if(defined($self->{selector}) && defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"selector"} and defined $self->{"socket"}) 0 0 0
Condition0%0/3
defined $self->{"selector"} and defined $self->{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"selector"} and defined $self->{"socket"}
defined $self->{"selector"} defined $self->{"socket"}
Truth table0%0/3
A and B
A= defined $self->{"selector"}B= defined $self->{"socket"}
ABresult
0X0
100
111
233 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
234 0 $self->{selector}->remove($self->{socket});
235 };
236 }
237 0 undef $self->{selector};
238 0 ▶ if(defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket"}) 0 0 0
239 0 $self->_safeCloseSocket($self->{socket});
240 0 delete $self->{socket};
241 }
242  
243 0 ▶ if(!$self->{firstconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"firstconnect"}) 0 0 0
244 # Not our first connection (=real reconnect).
245 # wait a short random time before reconnecting. In case all
246 # clients got disconnected, we want to avoid having all clients reconnect
247 # at the exact same time
248 0 my $waittime = rand(4000)/1000;
249 0 sleep($waittime);
250 }
251  
252 0 my $socket;
253 0 ▶ if(defined($self->{server}) && defined($self->{port})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'server'} and defined $self->{'port'}) { } 0 0 0
elsif (defined $self->{'socketpath'}) { } 0 0 0
Condition0%0/3
defined $self->{"server"} and defined $self->{"port"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"server"} and defined $self->{"port"}
defined $self->{"server"} defined $self->{"port"}
Truth table0%0/3
A and B
A= defined $self->{"server"}B= defined $self->{"port"}
ABresult
0X0
100
111
254 # Cap TCP connect at $self->{connecttimeout} so an unreachable host
255 # doesn't make us wait for the kernel SYN-retransmit timeout (~75s).
256 $socket = IO::Socket::IP->new(
257 PeerHost => $self->{server},
258 PeerPort => $self->{port},
259 Type => SOCK_STREAM,
260 Timeout => $self->{connecttimeout},
261 0 ▶ ) or croak("Failed to connect to Clacks TCP message service: $ERRNO");
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::IP"->new("PeerHost", $self->{"server"}, "PeerPort", $self->{"port"}, "Type", 1, "Timeout", $self->{"connecttimeout"}) or croak("Failed to connect to Clacks TCP message service: $Net::Clacks::Client::ERRNO") 0 0 0
262 } elsif(defined($self->{socketpath})) {
263 $socket = IO::Socket::UNIX->new(
264 Peer => $self->{socketpath},
265 0 ▶ Type => SOCK_STREAM,
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::UNIX"->new("Peer", $self->{"socketpath"}, "Type", 1) or croak("Failed to connect to Clacks Unix Domain Socket message service: $Net::Clacks::Client::ERRNO") 0 0 0
266 ) or croak("Failed to connect to Clacks Unix Domain Socket message service: $ERRNO");
267 } else {
268 0 croak("Neither TCP nor Unix domain socket specified. Don't know where to connect to.");
269 }
270  
271 0 ▶ if(ref $socket ne 'IO::Socket::UNIX') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $socket ne "IO::Socket::UNIX") 0 0 0
272 # ONLY USE SSL WHEN RUNNING OVER THE NETWORK
273 # There is simply no point in running it over a local socket.
274 # Use the non-SIGALRM-based handshake helper so we get a hard deadline
275 # without interfering with the embedding application's signal handling.
276 0 my $sslsocket = $self->_sslHandshakeWithTimeout($socket, $self->{ssltimeout});
277 0 ▶ if(!defined($sslsocket)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sslsocket) 0 0 0
278 # Release the underlying TCP FD — otherwise a flapping server /
279 # cert mismatch / handshake hang leaks one FD per reconnect attempt.
280 0 $self->_safeCloseSocket($socket);
281 0 croak("Can't use SSL (handshake failed or timed out after $self->{ssltimeout}s): " . $SSL_ERROR);
282 }
283 0 $socket = $sslsocket;
284 }
285  
286 # _sslHandshakeWithTimeout already left the TCP+SSL socket non-blocking.
287 # For Unix-domain sockets we still need to set non-blocking explicitly.
288 0 $socket->blocking(0);
289  
290 0 $self->{socket} = $socket;
291 0 $self->{selector} = IO::Select->new($self->{socket});
292 0 $self->{failcount} = 0;
293 0 $self->{writefailtime} = 0; # Stalled-write streak start; 0 = no streak in progress
294 0 $self->{lastping} = time;
295 0 $self->{inbuffer} = '';
296 0 $self->{incharbuffer} = [];
297 0 $self->{outbuffer} = '';
298 0 $self->{serverinfo} = 'UNKNOWN';
299 0 $self->{needreconnect} = 0;
300 0 $self->{firstline} = 1;
301 0 $self->{headertimeout} = time + 15;
302  
303 # Do *not* nuke "inlines" array, since it may hold "QUIT" messages that the client wants to handle, for example, to re-issue
304 # "LISTEN" commands.
305 # $self->{inlines} = ();
306  
307 0 ▶ if($self->{firstconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'firstconnect'}) { } 0 0 0
308 0 $self->{firstconnect} = 0;
309 } else {
310 0 push @{$self->{inlines}}, "RECONNECTED";
311 }
312  
313 # Startup "handshake". As everything else, this is asyncronous, both server and
314 # client send their respective version strings and then wait to recieve their counterparts
315 # Also, this part is REQUIRED, just to make sure we actually speek to CLACKS protocol
316 0 $self->{outbuffer} .= 'CLACKS ' . $self->{clientname} . "\r\n";
317 0 $self->{outbuffer} .= 'OVERHEAD A ' . $self->{authtoken} . "\r\n";
318  
319 # AFTER the auth line: the server ignores every command from a client it
320 # has not authenticated yet, so a NOPING sent ahead of it is thrown away.
321 0 ▶ if($self->{noping}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"noping"}) 0 0 0
322 0 $self->{outbuffer} .= "NOPING\r\n";
323 }
324  
325 0 $self->doNetwork();
326  
327 0 return;
328 }
329  
330 0 ▶sub activate_memcached_compat($self) {
Subroutine activate_memcached_compat: not called CC=1 SCAR=6.9
Pod documented
331 0 $self->{memcached_compatibility} = 1;
332 0 return;
333 }
334  
335 0 ▶sub getRawSocket($self) {
Subroutine getRawSocket: not called CC=2 SCAR=17.9
Pod documented
336 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
337 0 $self->reconnect();
338 }
339  
340 0 return $self->{socket};
341 }
342  
343 0 ▶sub doNetwork($self, $readtimeout = 0) {
Subroutine doNetwork: not called CC=31 SCAR=69.0
Pod documented
344 0 ▶ if(!defined($readtimeout)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $readtimeout) 0 0 0
345 # Don't wait
346 0 $readtimeout = 0;
347 }
348  
349 # Negative read timeout means "send only"
350  
351 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
352 0 $self->reconnect();
353 }
354  
355 0 ▶ if($self->{nextremembrance} && time > $self->{nextremembrance}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nextremembrance"} and &Net::Clacks::Client::time() > $self->{"nextremembrance"}) 0 0 0
Condition0%0/3
$self->{"nextremembrance"} and &Net::Clacks::Client::time() > $self->{"nextremembrance"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"nextremembrance"} and &Net::Clacks::Client::time() > $self->{"nextremembrance"}
$self->{"nextremembrance"} &Net::Clacks::Client::time() > $self->{"nextremembrance"}
Truth table0%0/3
A and B
A= $self->{"nextremembrance"}B= &Net::Clacks::Client::time() > $self->{"nextremembrance"}
ABresult
0X0
100
111
356 # A person is not dead while their name is still spoken.
357 0 $self->{nextremembrance} = time + $self->{remembranceinterval} + int(rand($self->{remembranceinterval} / 10));
358 0 my $neverforget = $self->{remembrancenames}->[rand @{$self->{remembrancenames}}];
359 0 $self->{outbuffer} .= 'OVERHEAD GNU ' . $neverforget . "\r\n";
360 }
361  
362 # doNetwork interleaves handling incoming and outgoing traffic.
363 # This is only relevant on slow links.
364 #
365 # It returns even if the outgoing or incoming buffers are not empty
366 # (meaning that partially buffered data can exists). This way we use the
367 # available bandwidth without blocking unduly the application (we assume it's a realtime
368 # application with multiple things going on at the same time)-
369 #
370 # The downside of this is that doNetwork() needs to be called on a regular basis and sending
371 # and recieving might be delayed until the next cycle. This delay can be minimized by simply
372 # not transfering huge values over clacks, but instead using it the way it was intended to be used:
373 # Small variables can be SET directly by clacks, huge datasets should be stored in the
374 # database and the recievers only NOTIFY'd that a change has taken place.
375 #
376 # The big exception here is the here is the ClacksCache part of the story. These functions
377 # call doNetwork() in a loop until the outbuffer is empty. And depending on requirement, they
378 # KEEP on calling doNetwork() until the answer is recieved. This makes ClacksCache functions
379 # syncronous and causes some delay in the calling function. But doing these asyncronous will
380 # cause more headaches, maybe even leading up to insanity. Believe me, i tried, but those pink
381 # elephants stomping about my rubber-padded room are such a distraction...
382  
383 0 my $workCount = 0;
384 0 my $now = time;
385  
386 0 ▶ if(length($self->{outbuffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"outbuffer"}) 0 0 0
387 0 my $brokenpipe = 0;
388 0 my $writeok = 0;
389 0 my $written;
390 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
391 0 ▶ local $SIG{PIPE} = sub { $brokenpipe = 1; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
392 0 $written = syswrite($self->{socket}, $self->{outbuffer});
393 0 $writeok = 1;
394 };
395  
396 0 ▶ if($brokenpipe || !$writeok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($brokenpipe or not $writeok) 0 0 0
Condition0%0/3
$brokenpipe or not $writeok
l!l&&r!l&&!r
000
MC/DC0%0/2
$brokenpipe or not $writeok
$brokenpipe not $writeok
Truth table0%0/3
A or not(B)
A= $brokenpipeB= not $writeok
ABresult
1X1
011
000
397 0 $self->{needreconnect} = 1;
398 0 push @{$self->{inlines}}, "TIMEOUT";
399 0 return;
400 }
401  
402 0 ▶ if(defined($written) && $written > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $written and $written > 0) { } 0 0 0
Condition0%0/3
defined $written and $written > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $written and $written > 0
defined $written $written > 0
Truth table0%0/3
A and B
A= defined $writtenB= $written > 0
ABresult
0X0
100
111
403 # Forward progress: clear the stalled-write streak.
404 0 $self->{writefailtime} = 0;
405 0 $workCount += $written;
406 0 ▶ if(length($self->{outbuffer}) == $written) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'outbuffer'} == $written) { } 0 0 0
407 0 $self->{outbuffer} = '';
408 } else {
409 0 $self->{outbuffer} = substr($self->{outbuffer}, $written);
410 }
411 } else {
412 # No bytes written. Either undef ($! = EAGAIN/EWOULDBLOCK — kernel
413 # send buffer full because the server isn't reading) or a defined-
414 # but-zero return. Start (or continue) the stalled-write streak;
415 # if we go stalledwritetimeout seconds without a single successful
416 # byte while we have data queued, mark the connection for reconnect.
417 # This is the global counterpart to the server-side stall check
418 # and is what stops the various "send loop" call sites
419 # (notify/set/store/...) from spinning forever when the server is
420 # alive but not consuming.
421 0 ▶ if($self->{writefailtime} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'writefailtime'} == 0) { } 0 0 0
elsif ($now - $self->{'writefailtime'} > $self->{'stalledwritetimeout'}) { } 0 0 0
422 0 $self->{writefailtime} = $now;
423 } elsif(($now - $self->{writefailtime}) > $self->{stalledwritetimeout}) {
424 0 $self->{needreconnect} = 1;
425 0 push @{$self->{inlines}}, "TIMEOUT";
426 0 return;
427 }
428 }
429 }
430  
431 0 ▶ if($readtimeout < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($readtimeout < 0) 0 0 0
432 0 return 1;
433 }
434
435  
436 {
437 0 my @temp;
438 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
439 0 @temp = $self->{selector}->can_read($readtimeout);
440 };
441 0 ▶ if(scalar @temp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @temp == 0) 0 0 0
442 # Timeout
443 0 return $workCount;
444 }
445 }
446  
447 0 my $totalread = 0;
448 0 while(1) {
449 0 my $buf;
450 my $bytes;
451 0 my $readok = 0;
452 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
453 0 $bytes = sysread($self->{socket}, $buf, 10_000); # Read in at most 10kB at once
454 0 $readok = 1;
455 };
456 0 ▶ if(!$readok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($readok) 0 0 0
457 0 $self->{needreconnect} = 1;
458 0 push @{$self->{inlines}}, "TIMEOUT";
459 0 return;
460 }
461 0 ▶ if(defined($bytes) && $bytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes and $bytes == 0) 0 0 0
Condition0%0/3
defined $bytes and $bytes == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes and $bytes == 0
defined $bytes $bytes == 0
Truth table0%0/3
A and B
A= defined $bytesB= $bytes == 0
ABresult
0X0
100
111
462 # sysread returns 0 on EOF — the peer cleanly closed the socket
463 # (FIN received). The previous code ignored the return value and
464 # only relied on the failcount-after-many-empty-reads heuristic
465 # below, which took ~250ms to trigger and was unnecessarily fuzzy.
466 0 $self->{needreconnect} = 1;
467 0 push @{$self->{inlines}}, "TIMEOUT";
468 0 return;
469 }
470 0 ▶ if(defined($bytes) && $bytes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes and $bytes > 0) 0 0 0
Condition0%0/3
defined $bytes and $bytes > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes and $bytes > 0
defined $bytes $bytes > 0
Truth table0%0/3
A and B
A= defined $bytesB= $bytes > 0
ABresult
0X0
100
111
471 0 $totalread += $bytes;
472 0 push @{$self->{incharbuffer}}, split//, $buf;
473 0 next;
474 }
475 # $bytes is undef -> $! = EAGAIN/EWOULDBLOCK or a real error.
476 # Falls through to the totalread / failcount check below.
477 0 last;
478 }
479
480 # Check if we actually got data after checking with can_read() first
481 0 ▶ if($totalread) {
Branch: 1 branch
BranchTrueFalseTotal
if ($totalread) { } 0 0 0
482 0 $self->{failcount} = 0;
483 } else {
484 # This should normally not happen, but thanks to SSL, it sometimes does
485 # We ignore single instances of those but disconnect if many happen in a row
486 0 $self->{failcount}++;
487 0 sleep(0.05);
488
489 0 ▶ if($self->{failcount} > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"failcount"} > 5) 0 0 0
490 0 $self->{needreconnect} = 1;
491 0 return;
492 }
493 }
494 0 ▶ while(@{$self->{incharbuffer}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"incharbuffer"};}) 0 0 0
495 0 my $char = shift @{$self->{incharbuffer}};
496 0 $workCount++;
497 0 ▶ if($char eq "\r") {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq "\r") { } 0 0 0
elsif ($char eq "\n") { } 0 0 0
498 0 next;
499 } elsif($char eq "\n") {
500 0 ▶ if($self->{inbuffer} eq 'NOP') { # Just drop "No OPerations" packets, only used by server to
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inbuffer"} eq "NOP") 0 0 0
501 # verify that the connection is still active
502 #$self->{firstline}
503 0 $self->{inbuffer} = '';
504 0 next;
505 }
506  
507 0 ▶ if($self->{firstline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"firstline"}) 0 0 0
508 0 ▶ if($self->{inbuffer} !~ /^CLACKS\ /) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'inbuffer'} =~ /^CLACKS\ /u) { } 0 0 0
509 # Whoops, not a clacks server or something gone wrong with the protocol
510 0 $self->{needreconnect} = 1;
511 0 return 0;
512 } else {
513 0 $self->{firstline} = 0;
514 }
515 }
516  
517 0 push @{$self->{inlines}}, $self->{inbuffer};
518 0 $self->{inbuffer} = '';
519 } else {
520 0 $self->{inbuffer} .= $char;
521 }
522 }
523  
524 0 ▶ if($self->{firstline} && $self->{headertimeout} < time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"firstline"} and $self->{"headertimeout"} < &Net::Clacks::Client::time()) 0 0 0
Condition0%0/3
$self->{"firstline"} and $self->{"headertimeout"} < &Net::Clacks::Client::time()
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"firstline"} and $self->{"headertimeout"} < &Net::Clacks::Client::time()
$self->{"firstline"} $self->{"headertimeout"} < &Net::Clacks::Client::time()
Truth table0%0/3
A and B
A= $self->{"firstline"}B= $self->{"headertimeout"} < &Net::Clacks::Client::time()
ABresult
0X0
100
111
525 0 $self->{needreconnect} = 1;
526 0 return 0;
527 }
528  
529 0 return $workCount;
530 }
531  
532 4 my %overheadflags = (
533 A => "auth_token", # Authentication token
534 O => "auth_ok", # Authentication OK
535 F => "auth_failed", # Authentication FAILED
536  
537 E => 'error_message', # Server to client error message
538  
539 C => "close_all_connections",
540 D => "discard_message",
541 G => "forward_message",
542 I => "set_interclacks_mode", # value: true/false, disables 'G' and 'U'
543 M => "informal_message", # informal message
544 N => "no_logging",
545 S => "shutdown_service", # value: positive number (number in seconds before shutdown). If interclacks clients are present, should be high
546 # enough to flush all buffers to them
547 U => "return_to_sender",
548 Z => "no_flags", # Only sent when no other flags are set
549 );
550  
551 0 ▶sub getNext($self) {
Subroutine getNext: not called CC=20 SCAR=60.4
Pod documented
552 # Recieve next incoming message (if any)
553  
554 restartgetnext:
555 0 my $line = shift @{$self->{inlines}};
556  
557 0 ▶ if(!defined($line)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
558 0 return;
559 }
560  
561 0 my %data;
562 #print STDERR "> $line\n";
563 0 ▶ if($line =~ /^NOTIFY\ (.+)/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($line =~ /^NOTIFY\ (.+)/u) { } 0 0 0
elsif ($line =~ /^SET\ (.+?)\=(.*)/u) { } 0 0 0
elsif ($line =~ /^CLACKS\ (.+)/u) { } 0 0 0
elsif ($line =~ /^DEBUG\ (.+?)\=(.*)/u) { } 0 0 0
elsif ($line =~ /^QUIT/u) { } 0 0 0
elsif ($line =~ /^TIMEOUT/u) { } 0 0 0
elsif ($line =~ /^RECONNECTED/u) { } 0 0 0
elsif ($line =~ /^OVERHEAD\ (.+?)\ (.+)/u) { } 0 0 0
564 0 %data = (
565 type => 'notify',
566 name => $1,
567 );
568 } elsif($line =~ /^SET\ (.+?)\=(.*)/) {
569 0 %data = (
570 type => 'set',
571 name => $1,
572 data => $2,
573 );
574 } elsif($line =~ /^CLACKS\ (.+)/) {
575 0 %data = (
576 type => 'serverinfo',
577 data => $1,
578 );
579 } elsif($line =~ /^DEBUG\ (.+?)\=(.*)/) {
580 0 %data = (
581 type => 'debug',
582 host => $1,
583 command => $2,
584 );
585 } elsif($line =~ /^QUIT/) {
586 0 %data = (
587 type => 'disconnect',
588 data => 'quit',
589 );
590 0 $self->{needreconnect} = 1;
591 } elsif($line =~ /^TIMEOUT/) {
592 0 %data = (
593 type => 'disconnect',
594 data => 'timeout',
595 );
596 0 $self->{needreconnect} = 1;
597 } elsif($line =~ /^RECONNECTED/) {
598 0 %data = (
599 type => 'reconnected',
600 data => 'send your LISTEN requests again',
601 );
602 } elsif($line =~ /^OVERHEAD\ (.+?)\ (.+)/) {
603 # Minimal handling of OVERHEAD flags
604 0 my ($flags, $value) = ($1, $2);
605 0 my @flagparts = split//, $flags;
606 0 my %parsedflags;
607 0 foreach my $key (sort keys %overheadflags) {
608 0 ▶ if(contains($key, \@flagparts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (contains($key, \@flagparts)) { } 0 0 0
609 0 $parsedflags{$overheadflags{$key}} = 1;
610 } else {
611 0 $parsedflags{$overheadflags{$key}} = 0;
612 }
613 }
614  
615 0 ▶ if($parsedflags{auth_ok}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($parsedflags{'auth_ok'}) { } 0 0 0
elsif ($parsedflags{'error_message'}) { } 0 0 0
elsif ($parsedflags{'auth_failed'}) { } 0 0 0
elsif ($parsedflags{'informal_message'}) { } 0 0 0
616 #print STDERR "Clacks AUTH OK\n";
617 0 goto restartgetnext; # try the next message
618 } elsif($parsedflags{error_message}) {
619 0 %data = (
620 type => 'error_message',
621 data => $value,
622 );
623 } elsif($parsedflags{auth_failed}) {
624 0 croak("Clacks Authentication failed!");
625 } elsif($parsedflags{informal_message}) {
626 0 ▶ if($parsedflags{forward_message}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsedflags{"forward_message"}) 0 0 0
627 0 %data = (
628 type => 'informal',
629 data => $value,
630 );
631 }
632 0 ▶ if($parsedflags{return_to_sender}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsedflags{"return_to_sender"}) 0 0 0
633 0 my $uturn = 'OVERHEAD M';
634 0 ▶ if($parsedflags{no_logging}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsedflags{"no_logging"}) 0 0 0
635 0 $uturn .= 'N';
636 }
637 0 $uturn .= ' ' . $value;
638 0 $self->{outbuffer} .= $uturn;
639 }
640 }
641  
642 } else {
643 # UNKNOWN, ignore
644 0 goto restartgetnext; # try the next message
645 }
646  
647 0 ▶ if(!defined($data{type})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data{"type"}) 0 0 0
648 0 return;
649 }
650  
651 0 $data{rawline} = $line;
652  
653 0 return \%data;
654 }
655  
656  
657 0 ▶sub ping($self) {
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
658 0 ▶ if($self->{lastping} < (time - 120)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lastping"} < &Net::Clacks::Client::time() - 120) 0 0 0
659 # Only send a ping every 120 seconds or less
660 0 $self->{outbuffer} .= "PING\r\n";
661 0 $self->{lastping} = time;
662  
663 # The server resumes ping checking for this client the moment it
664 # receives a PING (it sets lastping back to a real timestamp), so a
665 # client that pings has ended its own exemption and must not have it
666 # replayed on the next reconnect.
667 0 $self->{noping} = 0;
668 }
669  
670 0 return;
671 }
672  
673 0 ▶sub disablePing($self) {
Subroutine disablePing: not called CC=1 SCAR=6.9
Pod documented
674 0 $self->{noping} = 1;
675 0 $self->{outbuffer} .= "NOPING\r\n";
676  
677 0 return;
678 }
679  
680  
681 0 ▶sub notify($self, $varname) {
Subroutine notify: not called CC=7 SCAR=40.3
Pod documented
682 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
683 0 carp("varname not defined!");
684 0 return;
685 }
686  
687 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
688 0 $self->reconnect;
689 }
690  
691 0 $self->{outbuffer} .= "NOTIFY $varname\r\n";
692  
693 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
694 0 while(1) {
695 0 $self->doNetwork();
696 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
697 # Nothing we can do, really...
698 0 return;
699 }
700 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
701 0 usleep(1000);
702 }
703 0 $self->autohandle_messages();
704 }
705  
706 0 return;
707 }
708  
709 0 ▶sub set($self, $varname, $value, $forcesend = 0) { ## no critic (NamingConventions::ProhibitAmbiguousNames)
Subroutine set: not called CC=15 SCAR=54.8
Pod documented
710 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
711 0 carp("varname not defined!");
712 0 return;
713 }
714 0 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
715 0 carp("value not defined!");
716 0 return;
717 }
718  
719 0 ▶ if(!defined($forcesend)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $forcesend) 0 0 0
720 0 $forcesend = 0;
721 }
722  
723 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
724 0 $self->reconnect;
725 }
726  
727 # Handle caching to lower output volumne
728 0 ▶ if($self->{iscaching} && !$forcesend && defined($self->{cache}->{$varname}) && $self->{cache}->{$varname} eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"iscaching"} and not $forcesend and defined $self->{"cache"}{$varname} and $self->{"cache"}{$varname} eq $value) 0 0 0
Condition0%0/9
$self->{"iscaching"} and not $forcesend and defined $self->{"cache"}{$varname} and $self->{"cache"}{$varname} eq $value
!ll&&!rl&&r
000
$self->{"iscaching"} and not $forcesend and defined $self->{"cache"}{$varname}
!ll&&!rl&&r
000
$self->{"iscaching"} and not $forcesend
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{"iscaching"} and not $forcesend and defined $self->{"cache"}{$varname} and $self->{"cache"}{$varname} eq $value
$self->{"iscaching"} not $forcesend defined $self->{"cache"}{$varname} $self->{"cache"}{$varname} eq $value
Truth table0%0/5
A and not(B) and C and D
A= $self->{"iscaching"}B= not $forcesendC= defined $self->{"cache"}{$varname}D= $self->{"cache"}{$varname} eq $value
ABCDresult
0XXX0
10XX0
110X0
11100
11111
729 # Already the same value send
730 0 return;
731 }
732  
733 0 ▶ if($self->{iscaching}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"iscaching"}) 0 0 0
734 0 $self->{cache}->{$varname} = $value;
735 }
736  
737 0 $self->{outbuffer} .= "SET $varname=$value\r\n";
738  
739 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
740 0 while(1) {
741 0 $self->doNetwork();
742 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
743 # Nothing we can do, really...
744 0 return;
745 }
746 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
747 0 usleep(1000);
748 }
749  
750 0 $self->autohandle_messages();
751 }
752  
753 0 return;
754 }
755  
756 0 ▶sub listen($self, $varname) { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
Subroutine listen: not called CC=4 SCAR=30.0
Pod documented
757 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
758 0 carp("varname not defined!");
759 0 return;
760 }
761  
762 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
763 0 $self->reconnect;
764 }
765  
766 0 $self->{outbuffer} .= "LISTEN $varname\r\n";
767  
768 0 return;
769 }
770  
771 0 ▶sub unlisten($self, $varname) {
Subroutine unlisten: not called CC=4 SCAR=30.0
Pod documented
772 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
773 0 carp("varname not defined!");
774 0 return;
775 }
776  
777 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
778 0 $self->reconnect;
779 }
780  
781 0 $self->{outbuffer} .= "UNLISTEN $varname\r\n";
782  
783 0 return;
784 }
785  
786 0 ▶sub setMonitormode($self, $active) {
Subroutine setMonitormode: not called CC=4 SCAR=30.0
Pod documented
787 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
788 0 $self->reconnect;
789 }
790  
791 0 ▶ if(!defined($active) || !$active) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $active && $active) { } 0 0 0
Condition0%0/3
defined $active && $active
!ll&&!rl&&r
000
MC/DC0%0/2
defined $active && $active
defined $active $active
Truth table0%0/3
A && B
A= defined $activeB= $active
ABresult
0X0
100
111
792 0 $self->{outbuffer} .= "UNMONITOR\r\n";
793 } else {
794 0 $self->{outbuffer} .= "MONITOR\r\n";
795 }
796  
797 0 return;
798 }
799  
800 0 ▶sub getServerinfo($self) {
Subroutine getServerinfo: not called CC=1 SCAR=6.9
Pod documented
801 0 return $self->{serverinfo};
802 }
803  
804 # ---------------- ClackCache handling --------------------
805 # ClacksCache handling always implies doNetwork()
806 # Also, we do NOT use the caching system used for SET
807 0 ▶sub store($self, $varname, $value) {
Subroutine store: not called CC=8 SCAR=42.8
Pod documented
808 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
809 0 carp("varname not defined!");
810 0 return;
811 }
812 0 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
813 0 carp("value not defined!");
814 0 return;
815 }
816  
817 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
818 0 $self->reconnect;
819 }
820  
821 0 $self->{outbuffer} .= "STORE $varname=$value\r\n";
822 0 while(1) {
823 0 $self->doNetwork();
824 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
825 # Nothing we can do, really...
826 0 return;
827 }
828 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
829 0 usleep(1000);
830 }
831  
832 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
833 0 $self->autohandle_messages();
834 }
835  
836 0 return;
837 }
838  
839 #
840 # Watch for a server-side REFUSAL while waiting for a request answer.
841 #
842 # THE SERVER DOES NOT ALWAYS ANSWER A REQUEST. Two of its paths reply with an
843 # OVERHEAD line and nothing else: a client that is not authenticated has its
844 # commands ignored outright (Server.pm, "Ignore other command when not
845 # authenticated"), and an authenticated client that lacks the permission for
846 # the command gets 'OVERHEAD E permission_denied' from _requirePermission().
847 # Neither ever produces the RETRIEVED / KEYLISTEND / CLIENTLISTEND / FLUSHED
848 # line the four request methods below sit and wait for.
849 #
850 # Those lines are decoded in getNext(), but a caching client never calls it -
851 # it calls retrieve() and nothing else. So every refusal used to be
852 # indistinguishable from a dead server: the caller blocked for the full
853 # requesttimeout, reported a timeout, marked the connection for reconnect,
854 # and the reconnect presented the very same rejected credentials again. The
855 # actual reason was sitting in the inline buffer the whole time, unread.
856 # Diagnosed on a PageCamel web backend 2026-09-03, where it cost 30 seconds
857 # per cache lookup and said only "timed out".
858 #
859 # Deliberately does NOT set needreconnect: a refusal is an answer. The
860 # connection is fine and reconnecting only re-runs the same rejection.
861 #
862 # Returns the server's own message, having removed the line, or undef.
863 #
864 0 ▶sub _checkRefusal($self) {
Subroutine _checkRefusal: not called CC=4 SCAR=30.0
865 0 ▶ for(my $i = 0; $i < scalar @{$self->{inlines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"inlines"};}) 0 0 0
866 0 ▶ next if($self->{inlines}->[$i] !~ /^OVERHEAD\ (.+?)\ (.+)/);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"inlines"}[$i] =~ /^OVERHEAD\ (.+?)\ (.+)/u 0 0 0
867 0 my ($flags, $value) = ($1, $2);
868  
869 # E = error_message, F = auth_failed. Flags are a letter set, so a
870 # line may legitimately carry others alongside.
871 0 ▶ next if($flags !~ /[EF]/);
Branch: 1 branch
BranchTrueFalseTotal
unless $flags =~ /[EF]/u 0 0 0
872  
873 0 splice @{$self->{inlines}}, $i, 1;
874 0 return $value;
875 }
876  
877 0 return;
878 }
879  
880 0 ▶sub retrieve($self, $varname) {
Subroutine retrieve: not called CC=15 SCAR=54.8
Pod documented
881 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
882 0 carp("varname not defined!");
883 0 return;
884 }
885  
886 0 my $value;
887  
888 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
889 0 $self->reconnect;
890 }
891  
892 0 $self->{outbuffer} .= "RETRIEVE $varname\r\n";
893  
894 # Make sure we send everything
895 0 while(1) {
896 0 $self->doNetwork();
897 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
898 # Nothing we can do, really...
899 0 return;
900 }
901 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
902 }
903  
904 # Now, wait for the answer
905 0 my $answerline;
906 0 my $deadline = time + $self->{requesttimeout};
907 0 while(1) {
908 0 ▶ if(time > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Net::Clacks::Client::time() > $deadline) 0 0 0
909 # Server didn't answer in time — assume the connection is wedged so
910 # the next call reconnects rather than hanging here forever.
911 0 carp("retrieve($varname) timed out after $self->{requesttimeout}s; marking connection for reconnect");
912 0 $self->{needreconnect} = 1;
913 0 return;
914 }
915 0 $self->doNetwork(0.5);
916 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
917 # Nothing we can do, really...
918 0 return;
919 }
920  
921 0 my $refused = $self->_checkRefusal();
922 0 ▶ if(defined($refused)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refused) 0 0 0
923 0 carp("retrieve($varname) refused by server: $refused");
924 0 return;
925 }
926 0 ▶ for(my $i = 0; $i < scalar @{$self->{inlines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"inlines"};}) 0 0 0
927 0 ▶ if($self->{inlines}->[$i] =~ /^RETRIEVED\ $varname/ || $self->{inlines}->[$i] =~ /^NOTRETRIEVED\ $varname/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/u or $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u) 0 0 0
Condition0%0/3
$self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/u or $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/u or $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u
$self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/u $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u
Truth table0%0/3
A or B
A= $self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/uB= $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u
ABresult
1X1
011
000
928 # Remove the answer from in in-queue directly (out of sequence), because we don't need in in the getNext function
929 0 $answerline = splice @{$self->{inlines}}, $i, 1;
930 0 last;
931 }
932 }
933 0 ▶ last if(defined($answerline));
Branch: 1 branch
BranchTrueFalseTotal
if defined $answerline 0 0 0
934 }
935  
936 0 ▶ if($answerline =~ /^RETRIEVED\ (.+?)\=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answerline =~ /^RETRIEVED\ (.+?)\=(.*)/u) 0 0 0
937 0 my ($key, $val) = ($1, $2);
938 0 ▶ if($key ne $varname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne $varname) 0 0 0
939 #print STDERR "Retrieved clacks key $key does not match requested varname $varname!\n";
940 0 return;
941 }
942 0 return $val;
943 }
944  
945 # No matching key
946 0 return;
947 }
948  
949 0 ▶sub remove($self, $varname) {
Subroutine remove: not called CC=7 SCAR=40.3
Pod documented
950 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
951 0 carp("varname not defined!");
952 0 return;
953 }
954  
955 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
956 0 $self->reconnect;
957 }
958  
959 0 $self->{outbuffer} .= "REMOVE $varname\r\n";
960 0 while(1) {
961 0 $self->doNetwork();
962 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
963 # Nothing we can do, really...
964 0 return;
965 }
966 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
967 0 usleep(1000);
968 }
969  
970 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
971 0 $self->autohandle_messages();
972 }
973  
974 0 return;
975 }
976  
977 0 ▶sub increment($self, $varname, $stepsize = '') {
Subroutine increment: not called CC=10 SCAR=47.0
Pod documented
978 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
979 0 carp("varname not defined!");
980 0 return;
981 }
982  
983 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
984 0 $self->reconnect;
985 }
986  
987 0 ▶ if(!defined($stepsize) || $stepsize eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stepsize or $stepsize eq '') { } 0 0 0
Condition0%0/3
not defined $stepsize or $stepsize eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $stepsize or $stepsize eq ""
not defined $stepsize $stepsize eq ""
Truth table0%0/3
not(A) or B
A= not defined $stepsizeB= $stepsize eq ""
ABresult
1X1
011
000
988 0 $self->{outbuffer} .= "INCREMENT $varname\r\n";
989 } else {
990 0 $stepsize = 0 + $stepsize;
991 0 $self->{outbuffer} .= "INCREMENT $varname=$stepsize\r\n";
992 }
993  
994 0 while(1) {
995 0 $self->doNetwork();
996 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
997 # Nothing we can do, really...
998 0 return;
999 }
1000 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1001 0 usleep(1000);
1002 }
1003  
1004 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
1005 0 $self->autohandle_messages();
1006 }
1007  
1008 0 return;
1009 }
1010  
1011 0 ▶sub decrement($self, $varname, $stepsize = '') {
Subroutine decrement: not called CC=10 SCAR=47.0
Pod documented
1012 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
1013 0 carp("varname not defined!");
1014 0 return;
1015 }
1016  
1017 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1018 0 $self->reconnect;
1019 }
1020  
1021 0 ▶ if(!defined($stepsize) || $stepsize eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stepsize or $stepsize eq '') { } 0 0 0
Condition0%0/3
not defined $stepsize or $stepsize eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $stepsize or $stepsize eq ""
not defined $stepsize $stepsize eq ""
Truth table0%0/3
not(A) or B
A= not defined $stepsizeB= $stepsize eq ""
ABresult
1X1
011
000
1022 0 $self->{outbuffer} .= "DECREMENT $varname\r\n";
1023 } else {
1024 0 $stepsize = 0 + $stepsize;
1025 0 $self->{outbuffer} .= "DECREMENT $varname=$stepsize\r\n";
1026 }
1027 0 while(1) {
1028 0 $self->doNetwork();
1029 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1030 # Nothing we can do, really...
1031 0 return;
1032 }
1033 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1034 0 usleep(1000);
1035 }
1036  
1037 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
1038 0 $self->autohandle_messages();
1039 }
1040  
1041 0 return;
1042 }
1043  
1044 0 ▶sub clearcache($self) {
Subroutine clearcache: not called CC=5 SCAR=34.0
Pod documented
1045 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1046 0 $self->reconnect;
1047 }
1048  
1049 0 $self->{outbuffer} .= "CLEARCACHE\r\n";
1050 0 while(1) {
1051 0 $self->doNetwork();
1052 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1053 # Nothing we can do, really...
1054 0 return;
1055 }
1056 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1057 0 usleep(1000);
1058 }
1059  
1060 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
1061 0 $self->autohandle_messages();
1062 }
1063  
1064 0 return;
1065 }
1066  
1067 0 ▶sub keylist($self) {
Subroutine keylist: not called CC=18 SCAR=58.3
Pod documented
1068 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1069 0 $self->reconnect;
1070 }
1071  
1072 0 my $value;
1073  
1074 0 $self->{outbuffer} .= "KEYLIST\r\n";
1075  
1076 # Make sure we send everything
1077 0 while(1) {
1078 0 $self->doNetwork();
1079 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1080 # Nothing we can do, really...
1081 0 return;
1082 }
1083 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1084 0 usleep(1000);
1085 }
1086  
1087 # Now, wait for the answer
1088 0 my $liststartfound = 0;
1089 0 my $listendfound = 0;
1090 0 my $deadline = time + $self->{requesttimeout};
1091 0 while(1) {
1092 0 ▶ if(time > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Net::Clacks::Client::time() > $deadline) 0 0 0
1093 0 carp("keylist() timed out after $self->{requesttimeout}s; marking connection for reconnect");
1094 0 $self->{needreconnect} = 1;
1095 0 return;
1096 }
1097 0 $self->doNetwork(0.5);
1098 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1099 # Nothing we can do, really...
1100 0 return;
1101 }
1102  
1103 0 my $refused = $self->_checkRefusal();
1104 0 ▶ if(defined($refused)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refused) 0 0 0
1105 0 carp("keylist() refused by server: $refused");
1106 0 return;
1107 }
1108 0 $liststartfound = 0;
1109 0 $listendfound = 0;
1110 0 ▶ for(my $i = 0; $i < scalar @{$self->{inlines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"inlines"};}) 0 0 0
1111 0 ▶ if($self->{inlines}->[$i] =~ /^KEYLISTSTART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^KEYLISTSTART/u) 0 0 0
1112 0 $liststartfound = 1;
1113 0 next;
1114 }
1115 0 ▶ next unless($liststartfound);
Branch: 1 branch
BranchTrueFalseTotal
unless $liststartfound 0 0 0
1116 0 ▶ if($self->{inlines}->[$i] =~ /^KEYLISTEND/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^KEYLISTEND/u) 0 0 0
1117 0 $listendfound = 1;
1118 0 last;
1119 }
1120 }
1121 0 ▶ last if($listendfound);
Branch: 1 branch
BranchTrueFalseTotal
if $listendfound 0 0 0
1122 }
1123  
1124 # Now, grab the keys from inlines buffer
1125 0 my @keys;
1126 0 my $idx = 0;
1127 0 my $listfound = 0;
1128 0 ▶ while($idx < scalar @{$self->{inlines}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < scalar @{$self->{"inlines"};}) 0 0 0
1129 0 ▶ if($self->{inlines}->[$idx] =~ /^KEYLISTSTART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$idx] =~ /^KEYLISTSTART/u) 0 0 0
1130 # Just remove this line
1131 0 splice @{$self->{inlines}}, $idx, 1;
1132 0 $listfound = 1;
1133 0 next;
1134 }
1135  
1136 0 ▶ if(!$listfound) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listfound) 0 0 0
1137 0 $idx++;
1138 0 next;
1139 }
1140  
1141 0 ▶ if($self->{inlines}->[$idx] =~ /^KEYLISTEND/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$idx] =~ /^KEYLISTEND/u) 0 0 0
1142 # End of list
1143 0 last;
1144 }
1145  
1146 0 ▶ if($self->{inlines}->[$idx] =~ /^KEY\ (.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'inlines'}[$idx] =~ /^KEY\ (.+)/u) { } 0 0 0
1147 0 push @keys, $1;
1148 # Don't increment $idx, but the rest of the array one element down
1149 0 splice @{$self->{inlines}}, $idx, 1;
1150 } else {
1151 0 $idx++;
1152 }
1153 }
1154  
1155 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
1156 0 $self->autohandle_messages();
1157 }
1158  
1159 0 return @keys;
1160 }
1161  
1162 0 ▶sub clientlist($self) {
Subroutine clientlist: not called CC=19 SCAR=59.4
Pod documented
1163 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1164 0 $self->reconnect;
1165 }
1166  
1167 0 my $value;
1168  
1169 0 $self->{outbuffer} .= "CLIENTLIST\r\n";
1170  
1171 # Make sure we send everything
1172 0 while(1) {
1173 0 $self->doNetwork();
1174 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1175 # Nothing we can do, really...
1176 0 return;
1177 }
1178 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1179 0 usleep(1000);
1180 }
1181  
1182 # Now, wait for the answer
1183 0 my $liststartfound = 0;
1184 0 my $listendfound = 0;
1185 0 my $deadline = time + $self->{requesttimeout};
1186 0 while(1) {
1187 0 ▶ if(time > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Net::Clacks::Client::time() > $deadline) 0 0 0
1188 0 carp("clientlist() timed out after $self->{requesttimeout}s; marking connection for reconnect");
1189 0 $self->{needreconnect} = 1;
1190 0 return;
1191 }
1192 0 $self->doNetwork(0.5);
1193 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1194 # Nothing we can do, really...
1195 0 return;
1196 }
1197  
1198 0 my $refused = $self->_checkRefusal();
1199 0 ▶ if(defined($refused)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refused) 0 0 0
1200 0 carp("clientlist() refused by server: $refused");
1201 0 return;
1202 }
1203 0 $liststartfound = 0;
1204 0 $listendfound = 0;
1205 0 ▶ for(my $i = 0; $i < scalar @{$self->{inlines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"inlines"};}) 0 0 0
1206 0 ▶ if($self->{inlines}->[$i] =~ /^CLIENTLISTSTART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^CLIENTLISTSTART/u) 0 0 0
1207 0 $liststartfound = 1;
1208 0 next;
1209 }
1210 0 ▶ next unless($liststartfound);
Branch: 1 branch
BranchTrueFalseTotal
unless $liststartfound 0 0 0
1211 0 ▶ if($self->{inlines}->[$i] =~ /^CLIENTLISTEND/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^CLIENTLISTEND/u) 0 0 0
1212 0 $listendfound = 1;
1213 0 last;
1214 }
1215 }
1216 0 ▶ last if($listendfound);
Branch: 1 branch
BranchTrueFalseTotal
if $listendfound 0 0 0
1217 }
1218  
1219 # Now, grab the keys from inlines buffer
1220 0 my @keys;
1221 0 my $idx = 0;
1222 0 my $listfound = 0;
1223 0 ▶ while($idx < scalar @{$self->{inlines}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < scalar @{$self->{"inlines"};}) 0 0 0
1224 0 ▶ if($self->{inlines}->[$idx] =~ /^CLIENTLISTSTART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$idx] =~ /^CLIENTLISTSTART/u) 0 0 0
1225 # Just remove this line
1226 0 splice @{$self->{inlines}}, $idx, 1;
1227 0 $listfound = 1;
1228 0 next;
1229 }
1230  
1231 0 ▶ if(!$listfound) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listfound) 0 0 0
1232 0 $idx++;
1233 0 next;
1234 }
1235  
1236 0 ▶ if($self->{inlines}->[$idx] =~ /^CLIENTLISTEND/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$idx] =~ /^CLIENTLISTEND/u) 0 0 0
1237 # End of list
1238 0 last;
1239 }
1240  
1241 0 ▶ if($self->{inlines}->[$idx] =~ /^CLIENT\ (.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'inlines'}[$idx] =~ /^CLIENT\ (.+)/u) { } 0 0 0
1242 0 my @parts = split/\;/, $1;
1243 0 my %data;
1244 0 foreach my $part (@parts) {
1245 0 my ($datakey, $datavalue) = split/\=/, $part, 2;
1246 #$datakey = lc $datakey;
1247 0 $data{lc $datakey} = $datavalue;
1248 }
1249 0 push @keys, \%data;
1250 # Don't increment $idx, but the rest of the array one element down
1251 0 splice @{$self->{inlines}}, $idx, 1;
1252 } else {
1253 0 $idx++;
1254 }
1255 }
1256  
1257 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
1258 0 $self->autohandle_messages();
1259 }
1260  
1261 0 return @keys;
1262 }
1263  
1264 0 ▶sub flush($self, $flushid = '') {
Subroutine flush: not called CC=13 SCAR=52.0
Pod documented
1265 0 ▶ if(!defined($flushid) || $flushid eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $flushid or $flushid eq "") 0 0 0
Condition0%0/3
not defined $flushid or $flushid eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $flushid or $flushid eq ""
not defined $flushid $flushid eq ""
Truth table0%0/3
not(A) or B
A= not defined $flushidB= $flushid eq ""
ABresult
1X1
011
000
1266 0 $flushid = 'AUTO' . int(rand(1_000_000)) . int(rand(1_000_000));
1267 }
1268  
1269 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1270 0 $self->reconnect;
1271 }
1272  
1273 0 $self->{outbuffer} .= "FLUSH $flushid\r\n";
1274  
1275 # Make sure we send everything
1276 0 while(1) {
1277 0 $self->doNetwork();
1278 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1279 # Nothing we can do, really...
1280 0 return;
1281 }
1282 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1283 }
1284  
1285 # Now, wait for the answer
1286 0 my $answerline;
1287 0 my $deadline = time + $self->{requesttimeout};
1288 0 while(1) {
1289 0 ▶ if(time > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Net::Clacks::Client::time() > $deadline) 0 0 0
1290 0 carp("flush($flushid) timed out after $self->{requesttimeout}s; marking connection for reconnect");
1291 0 $self->{needreconnect} = 1;
1292 0 return;
1293 }
1294 0 $self->doNetwork(0.5);
1295 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1296 # Nothing we can do, really...
1297 0 return;
1298 }
1299  
1300 0 my $refused = $self->_checkRefusal();
1301 0 ▶ if(defined($refused)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refused) 0 0 0
1302 0 carp("flush($flushid) refused by server: $refused");
1303 0 return;
1304 }
1305 0 ▶ for(my $i = 0; $i < scalar @{$self->{inlines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"inlines"};}) 0 0 0
1306 0 ▶ if($self->{inlines}->[$i] =~ /^FLUSHED\ $flushid/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^FLUSHED\ $flushid/u) 0 0 0
1307 # Remove the answer from in in-queue directly (out of sequence), because we don't need in in the getNext function
1308 0 $answerline = splice @{$self->{inlines}}, $i, 1;
1309 0 last;
1310 }
1311 }
1312 0 ▶ last if(defined($answerline));
Branch: 1 branch
BranchTrueFalseTotal
if defined $answerline 0 0 0
1313 }
1314  
1315 0 return;
1316 }
1317  
1318  
1319 0 ▶sub autohandle_messages($self) {
Subroutine autohandle_messages: not called CC=3 SCAR=24.8
Pod documented
1320 0 $self->doNetwork();
1321  
1322 0 ▶ while((my $line = $self->getNext())) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->getNext) 0 0 0
1323 0 ▶ if($line->{type} eq 'disconnect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"type"} eq "disconnect") 0 0 0
1324 0 $self->{needreconnect} = 1;
1325 }
1326 }
1327  
1328 0 return;
1329 }
1330  
1331 # ---------------- ClackCache handling --------------------
1332  
1333 0 ▶sub sendRawCommand($self, $command) {
Subroutine sendRawCommand: not called CC=4 SCAR=30.0
Pod documented
1334 0 ▶ if(!defined($command) || !length($command)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command and length $command) 0 0 0
Condition0%0/3
defined $command and length $command
!ll&&!rl&&r
000
MC/DC0%0/2
defined $command and length $command
defined $command length $command
Truth table0%0/3
A and B
A= defined $commandB= length $command
ABresult
0X0
100
111
1335 0 carp("command not defined!");
1336 0 return;
1337 }
1338  
1339 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1340 0 $self->reconnect;
1341 }
1342  
1343 0 $self->{outbuffer} .= $command . "\r\n";
1344  
1345 0 return;
1346 }
1347  
1348 # setAndStore combines the SET and STORE command into the SETANDSTORE server command. This is mostly done
1349 # for optimizing interclacks connections
1350 # Other clients will only get a SET notification, but the server also runs a STORE operation
1351 0 ▶sub setAndStore($self, $varname, $value, $forcesend = 0) {
Subroutine setAndStore: not called CC=9 SCAR=45.0
Pod documented
1352 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
1353 0 carp("varname not defined!");
1354 0 return;
1355 }
1356  
1357 0 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
1358 0 carp("value not defined!");
1359 0 return;
1360 }
1361  
1362 0 ▶ if(!defined($forcesend)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $forcesend) 0 0 0
1363 0 $forcesend = 0;
1364 }
1365  
1366 0 $self->{outbuffer} .= "SETANDSTORE $varname=$value\r\n";
1367  
1368 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
1369 0 while(1) {
1370 0 $self->doNetwork();
1371 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1372 # Nothing we can do, really...
1373 0 return;
1374 }
1375 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1376 0 usleep(1000);
1377 }
1378  
1379 0 $self->autohandle_messages();
1380 }
1381  
1382 0 return;
1383 }
1384  
1385 0 ▶sub fastdisconnect($self) {
Subroutine fastdisconnect: not called CC=4 SCAR=30.0
Pod documented
1386 # Mirror reconnect()'s teardown sequence: remove from selector, close, then
1387 # delete. The previous "delete only" approach left the FD open whenever the
1388 # selector or any other reference still pinned the socket.
1389 0 ▶ if(defined($self->{selector}) && defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"selector"} and defined $self->{"socket"}) 0 0 0
Condition0%0/3
defined $self->{"selector"} and defined $self->{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"selector"} and defined $self->{"socket"}
defined $self->{"selector"} defined $self->{"socket"}
Truth table0%0/3
A and B
A= defined $self->{"selector"}B= defined $self->{"socket"}
ABresult
0X0
100
111
1390 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
1391 0 $self->{selector}->remove($self->{socket});
1392 };
1393 }
1394 0 undef $self->{selector};
1395 0 ▶ if(defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket"}) 0 0 0
1396 0 $self->_safeCloseSocket($self->{socket});
1397 0 delete $self->{socket};
1398 }
1399 0 $self->{needreconnect} = 1;
1400 0 return;
1401 }
1402  
1403 0 ▶sub disconnect($self) {
Subroutine disconnect: not called CC=8 SCAR=42.8
Pod documented
1404 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1405 # We are not connected, just do nothing
1406 0 return;
1407 }
1408  
1409 0 $self->flush();
1410 0 $self->{outbuffer} .= "QUIT\r\n";
1411 0 my $endtime = time + 0.5; # Wait a maximum of half a second to send
1412 0 while(1) {
1413 0 ▶ last if(time > $endtime);
Branch: 1 branch
BranchTrueFalseTotal
if &Net::Clacks::Client::time() > $endtime 0 0 0
1414 0 my $xstart = time;
1415 0 $self->doNetwork(-1);
1416 0 my $xend = time;
1417 0 my $timetaken = $xend - $xstart;
1418 0 ▶ if($timetaken > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timetaken > 1) 0 0 0
1419 #print STDERR "\n §§§§§§§§§§§§§§§§§§§§§§§§§§§§§ doNetwork() took ", $timetaken, " seconds\n";
1420 }
1421 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1422 0 sleep(0.02);
1423 }
1424 0 sleep(0.2); # Wait for the OS to flush the socket
1425  
1426 # Tear down properly: deselect, close, delete. Just `delete` left the FD open
1427 # whenever IO::Select still held a reference, which is true here because we
1428 # never removed it from the selector.
1429 0 ▶ if(defined($self->{selector}) && defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"selector"} and defined $self->{"socket"}) 0 0 0
Condition0%0/3
defined $self->{"selector"} and defined $self->{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"selector"} and defined $self->{"socket"}
defined $self->{"selector"} defined $self->{"socket"}
Truth table0%0/3
A and B
A= defined $self->{"selector"}B= defined $self->{"socket"}
ABresult
0X0
100
111
1430 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
1431 0 $self->{selector}->remove($self->{socket});
1432 };
1433 }
1434 0 undef $self->{selector};
1435 0 ▶ if(defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket"}) 0 0 0
1436 0 $self->_safeCloseSocket($self->{socket});
1437 0 delete $self->{socket};
1438 }
1439 0 $self->{needreconnect} = 1;
1440  
1441 0 return;
1442 }
1443  
1444 0 ▶sub DESTROY($self) {
Subroutine DESTROY: not called CC=2 SCAR=17.9
1445 # During Perl's global destruction phase, package symbol tables are torn
1446 # down in arbitrary order. By the time DESTROY runs here, methods on
1447 # IO::Socket::SSL, IO::Select, or even IO::Socket may already be
1448 # unavailable. Skip cleanup entirely in that phase — the kernel closes
1449 # any leftover FD when the process exits, and the server detects that
1450 # as EOF and removes us cleanly. Trying to be "polite" here can stall
1451 # exit (flush() waits up to requesttimeout seconds) or call methods on
1452 # half-destroyed package state.
1453 0 ▶ return if(${^GLOBAL_PHASE} eq 'DESTRUCT');
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 0 0
1454  
1455 # Outside global destruction we do a *fast* close (no flush(), no QUIT,
1456 # no sleeps). DESTROY can run at moments where the graceful path is
1457 # inappropriate — e.g. a worker child unwinding after fork, an exception
1458 # being propagated, a local-block exit. Callers who want a graceful
1459 # protocol-level close should call $client->disconnect() explicitly
1460 # before dropping their reference; this DESTROY is just the safety net.
1461 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
1462 0 $self->fastdisconnect();
1463 };
1464  
1465 0 return;
1466 }
1467  
1468 4 1;
1469 __END__
« Clacks/Server.pm ↑ Summary Clacks/PostgreSQL2Clacks.pm »