Session/Store/Redis.pm

filter: Statementstmt 96.4% 493 / 511 Branchbran 88.5% 232 / 262 Conditioncond 72.8% 110 / 151 MC/DCmcdc 56.3% 49 / 87 Subroutinesub 100% 59 / 59 Podpod 11.1% 1 / 9 totaltotal 87.4% 944 / 1079 CC 198 SCAR 22.8
CC
198
Cov
90%
new
24.4
_getRedis
18.8
_compareAndSet
17.0
CRAP 9.7
« Session/DBI.pm ↑ Summary Session/Store/Postgres.pm »
1 package Web::Session::Store::Redis;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 our $VERSION = '1.2.0';
8  
9 # Key of the session object holding the result of update(), see "DESCRIPTION"
10 10 ▶use constant STATUS => 'web_session_redis_update';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Key of the session object holding the version read, see _writeMerged()
13 10 ▶use constant READ => 'web_session_redis_read';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Writes tried before a session updated concurrently is overwritten
16 10 our $MAX_TRIES = 5;
17  
18 10 our $redis;
19 10 our %reused_connections;
20 10 our %selected; # reuse key => [ refaddr of the connection, database ]
21  
22 # A reused connection that lost its server (Redis restarted, network
23 # failure) stays broken unless the client reconnects by itself (reconnect
24 # option): Redis.pm and Redis::Fast then fail every command with "Not
25 # connected to any server". After a read timeout, a late reply would be taken
26 # for the one of the next command. After a Sentinel failover, it may be
27 # connected to a read-only replica. So when an operation fails with such an
28 # error, the connection is forgotten and the next tie opens a new one
29 # (Sentinel giving the new primary). A read is tried again at once on a new
30 # connection: it changes nothing; so is the write of a new session: same id,
31 # same data. An update is not: the error is raised, as the update may have
32 # been done.
33 # The messages of Redis.pm and Redis::Fast are in English whatever the locale;
34 # only the system error that may follow them is translated. READONLY is a
35 # reply of the server
36 10 our $CONNECTION_ERROR = qr/
37 Not\ connected\ to\ any\ server
38 | Could\ not\ (?:connect|write)\ to\ Redis\ server
39 | Error\ while\ reading\ from\ Redis\ server
40 | EOF\ from\ server
41 | Redis\ server\ closed\ connection
42 | \bREADONLY\b
43 /x;
44  
45 10 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.5
46 10 $redis = 'Redis::Fast';
47 10 ▶ eval 'use Redis::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
49 0 require Redis;
50 0 $redis = 'Redis';
51 }
52 }
53  
54 # Redis clients croak on characters above U+00FF: key names built from a field
55 # name or a value are sent as Latin-1 bytes (existing index sets keep their
56 # name) or, if they hold such characters, as UTF-8 bytes
57 224 ▶sub keyName {
Subroutine keyName: called CC=2 SCAR=6.9
Pod undocumented
58 224 my $name = shift;
59 224 $name = "$name";
60 224 ▶ utf8::encode($name) unless ( utf8::downgrade( $name, 1 ) );
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($name, 1) 5 219 224
61 224 return $name;
62 }
63  
64 10 our $HASH_INDEX_PREFIX = 'hidx:';
65  
66 # Index set of value $value of field $field
67 139 ▶sub indexName {
Subroutine indexName: called CC=1 SCAR=0.0
Pod undocumented
68 139 my ( $field, $value ) = @_;
69 139 return keyName("${field}_$value");
70 }
71  
72 # Index hash of value $value of field $field, used with utimeOffset
73 61 ▶sub hashIndexName {
Subroutine hashIndexName: called CC=1 SCAR=0.0
Pod undocumented
74 61 my ( $field, $value ) = @_;
75 61 return keyName("$HASH_INDEX_PREFIX${field}_$value");
76 }
77  
78 # '00' is truthy: the numeric test disables it
79 224 ▶sub hashIndex {
Subroutine hashIndex: called CC=2 SCAR=6.9
Pod undocumented
80 224 my ($args) = @_;
81 224 ▶ return ( $args->{utimeOffset} and $args->{utimeOffset} > 0 );
Condition100%3/3
$args->{'utimeOffset'} && $args->{'utimeOffset'} > 0
!ll&&!rl&&r
131192
MC/DC100%2/2
$args->{'utimeOffset'} && $args->{'utimeOffset'} > 0
$args->{'utimeOffset'} $args->{'utimeOffset'} > 0
Truth table100%3/3
A && B
A= $args->{'utimeOffset'}B= $args->{'utimeOffset'} > 0
ABresult
0X0
100
111
82 }
83  
84 5 ▶sub new {
Subroutine new: called CC=7 SCAR=32.0
Pod undocumented
85 5 my ( $class, $session ) = @_;
86 5 my $self;
87  
88 5 my $offset = $session->{args}->{utimeOffset};
89 5 ▶ die "utimeOffset must be a number of seconds\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $offset and length $offset and not $offset =~ /^[0-9]{1,10}\z/ 5 0 5
Condition33%2/6
defined $offset and length $offset and not $offset =~ /^[0-9]{1,10}\z/
!ll&&!rl&&r
005
defined $offset and length $offset
!ll&&!rl&&r
005
MC/DC0%0/3
defined $offset and length $offset and not $offset =~ /^[0-9]{1,10}\z/
defined $offset length $offset not $offset =~ /^[0-9]{1,10}\z/
Truth table25%1/4
A and B and not(C)
A= defined $offsetB= length $offsetC= not $offset =~ /^[0-9]{1,10}\z/
ABCresult
0XX0
10X0
1100
1111
90 if ( defined $offset
91 and length $offset
92 and $offset !~ /^[0-9]{1,10}\z/ );
93  
94 0 my $args = $session->{args};
95 0 $self->{cache} = $class->_getRedis($args);
96 _checkHexpire( $self->{cache}, $args )
97 if ( hashIndex($args)
98 0 ▶ and ( ref( $args->{Index} ) ? @{ $args->{Index} } : $args->{Index} ) );
Branch: 2 branches
BranchTrueFalseTotal
if hashIndex($args) and ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"} 0 0 0
ref $args->{'Index'} ? : 0 0 0
Condition0%0/3
hashIndex($args) and ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"}
!ll&&!rl&&r
000
MC/DC0%0/2
hashIndex($args) and ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"}
hashIndex($args) ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"}
Truth table0%0/3
A and B
A= hashIndex($args)B= ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"}
ABresult
0X0
100
111
99  
100 0 bless $self, $class;
101 }
102  
103 # Result of the HEXPIRE probe per server: 1 if supported, otherwise
104 # [ time of the next probe (undef: never), error to raise ]
105 10 our %hexpire;
106  
107 # Seconds before probing again a read-only replica
108 10 our $READONLY_RETRY = 60;
109  
110 # Fail at tie time: Apache::Session updates the session at untie, where an
111 # error is only a warning
112 17 ▶sub _checkHexpire {
Subroutine _checkHexpire: called CC=12 SCAR=24.8
113 17 my ( $redisObj, $args ) = @_;
114 my $server = join ' ',
115 17 ▶ map { ref($_) ? @$_ : $_ // '' } @$args{qw(server sock service sentinels)};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 3 65 68
Condition100%2/2
$_ // ""
l!l
1451
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
116 17 ▶ if ( my $known = $hexpire{$server} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $known = $Web::Session::Store::Redis::hexpire{$server}) 9 8 17
117 9 ▶ return unless ( ref $known );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $known 2 7 9
118 7 my ( $retry, $die ) = @$known;
119 7 ▶ if ( !defined $retry or time < $retry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $retry or time < $retry) 6 1 7
Condition100%3/3
not defined $retry or time < $retry
l!l&&r!l&&!r
331
MC/DC100%2/2
not defined $retry or time < $retry
not defined $retry time < $retry
Truth table100%3/3
not(A) or B
A= not defined $retryB= time < $retry
ABresult
1X1
011
000
120 6 ▶ die $die if ($die);
Branch: 1 branch
BranchTrueFalseTotal
if $die 1 5 6
121 5 return;
122 }
123 }
124  
125 # No index hash has this name. Errors other than a missing command (ACL,
126 # read-only replica...) are left to the commands that will fail. So that
127 # each tie doesn't pay a failed round trip, a missing command and NOPERM
128 # are remembered until the process ends, READONLY for $READONLY_RETRY
129 # seconds only (a Sentinel failover may promote the replica). Other errors
130 # may be transient
131 9 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $redisObj->hexpire($Web::Session::Store::Redis::HASH_INDEX_PREFIX, 1, "FIELDS", 1, "x"); 1 } }) 6 3 9
132 eval {
133 9 $redisObj->hexpire( $HASH_INDEX_PREFIX, 1, 'FIELDS', 1, 'x' );
134 3 1;
135 }
136 )
137 {
138 6 ( my $err = $@ ) =~ s/\n.*//s;
139 6 ▶ if ( $err =~ /unknown command/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /unknown command/i) 1 5 6
140 1 my $die = "utimeOffset needs Redis >= 7.4 or Valkey >= 9: $err\n";
141 1 $hexpire{$server} = [ undef, $die ];
142 1 die $die;
143 }
144 5 ▶ if ( $err =~ /\bNOPERM\b/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err =~ /\bNOPERM\b/) { } 1 4 5
elsif ($err =~ /\bREADONLY\b/) { } 1 3 4
145 1 $hexpire{$server} = [undef];
146 }
147 elsif ( $err =~ /\bREADONLY\b/ ) {
148 1 $hexpire{$server} = [ time + $READONLY_RETRY ];
149 }
150 else {
151 3 delete $hexpire{$server};
152 }
153 5 return;
154 }
155 3 $hexpire{$server} = 1;
156 }
157  
158 # Expiration of a session in seconds, see "OPTIONS"
159 130 ▶sub _ttl {
Subroutine _ttl: called CC=7 SCAR=19.5
160 130 my ($session) = @_;
161 130 my $args = $session->{args};
162 130 my $data = $session->{data};
163  
164 # Persistent sessions keep the _utime of their creation and never expire
165 130 ▶ if ( hashIndex($args)
Branch: 1 branch
BranchTrueFalseTotal
if (hashIndex($args) and defined $data->{"_utime"} and $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/ and ($data->{"_session_kind"} // "") ne "Persistent") 47 83 130
Condition100%11/11
hashIndex($args) and defined $data->{"_utime"} and $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/ and ($data->{"_session_kind"} // "") ne "Persistent"
!ll&&!rl&&r
80347
hashIndex($args) and defined $data->{"_utime"} and $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/
!ll&&!rl&&r
79150
hashIndex($args) and defined $data->{"_utime"}
!ll&&!rl&&r
78151
$data->{"_session_kind"} // ""
l!l
347
MC/DC100%4/4
hashIndex($args) and defined $data->{"_utime"} and $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/ and ($data->{"_session_kind"} // "") ne "Persistent"
hashIndex($args) defined $data->{"_utime"} $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/ $data->{"_session_kind"}
Truth table100%5/5
A and B and C and D
A= hashIndex($args)B= defined $data->{"_utime"}C= $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/D= $data->{"_session_kind"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
166 and defined $data->{_utime}
167 and $data->{_utime} =~ /^[0-9]+(?:\.[0-9]+)?\z/
168 and ( $data->{_session_kind} // '' ) ne 'Persistent' )
169 {
170 47 my $ttl = int( $data->{_utime} + $args->{utimeOffset} - time );
171  
172 # An expired session is still written: it may be read again before
173 # the end of the request
174 47 ▶ return $ttl > 0 ? $ttl : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ttl > 0 ? : 46 1 47
175 }
176 83 return $args->{TTL};
177 }
178  
179 18 ▶sub _set {
Subroutine _set: called CC=1 SCAR=0.0
180 18 my ( $self, $session, @opts ) = @_;
181 18 return $self->{cache}->set( _setArgs( $session, @opts ) );
182 }
183  
184 36 ▶sub _setArgs {
Subroutine _setArgs: called CC=2 SCAR=6.9
185 36 my ( $session, @opts ) = @_;
186 36 my $ttl = _ttl($session);
187 return (
188 $session->{data}->{_session_id},
189 36 ▶ $session->{serialized}, ( $ttl ? ( 'EX', $ttl ) : () ), @opts
Branch: 1 branch
BranchTrueFalseTotal
$ttl ? : 5 31 36
190 );
191 }
192  
193 # Sends the commands [ $command, $fatal, @args ] in one round trip. Dies with
194 # the error of the first $fatal command that failed; the errors of the others
195 # are ignored (key of another type)
196 35 ▶sub _pipeline {
Subroutine _pipeline: called CC=4 SCAR=13.9
197 35 my ( $self, @cmds ) = @_;
198 35 my $r = $self->{cache};
199 35 my $err;
200 35 my $sent = eval {
201 35 foreach (@cmds) {
202 67 my ( $cmd, $fatal, @args ) = @$_;
203 $r->$cmd( @args,
204 59 ▶ sub { $err //= "[$cmd] $_[1]" if ( $fatal and defined $_[1] ) }
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if $fatal and defined $_[1] 5 54 59
Condition100%5/5
$fatal and defined $_[1]
!ll&&!rl&&r
18365
$err //= "[$cmd] $_[1]"
l!l
23
MC/DC100%2/2
$fatal and defined $_[1]
$fatal defined $_[1]
MC/DC100%1/1
$err //= "[$cmd] $_[1]"
$err
Truth table100%3/3
A and B
A= $fatalB= defined $_[1]
ABresult
0X0
100
111
Truth table100%2/2
A
A= $err
Aresult
11
00
205 67 );
206 }
207 27 $r->wait_all_responses;
208 27 1;
209 };
210  
211 # Replies left unread would be taken for those of the next commands
212 35 ▶ unless ($sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 8 27 35
213 8 my $e = $@;
214 8 eval { $r->wait_all_responses };
215 8 die $e;
216 }
217 27 ▶ die "$err\n" if ( defined $err );
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 3 24 27
218 24 return 1;
219 }
220  
221 # XX: another process may have deleted the session (logout, revocation,
222 # purge) or Redis may have expired it since it was read; a plain SET would
223 # write it back
224 18 ▶sub _overwrite {
Subroutine _overwrite: called CC=2 SCAR=6.9
225 18 my ( $self, $session ) = @_;
226 18 ▶ return 1 if ( defined $self->_set( $session, 'XX' ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_set($session, "XX") 12 5 17
227 5 return 0;
228 }
229  
230 # With utimeOffset, the session and its index entries are written in one
231 # transaction. The entries of a session that expires are fields of an index
232 # hash that expire with it (HEXPIRE: Redis >= 7.4, Valkey >= 9; otherwise
233 # Redis refuses the whole transaction); the others stay in index sets. $xx:
234 # overwrite an existing session only. Returns false if it doesn't exist,
235 # undef if EXEC was aborted by a WATCH of the caller
236 42 ▶sub _writeHashIndex {
Subroutine _writeHashIndex: called CC=15 SCAR=27.1
237 42 my ( $self, $session, $index, $old, $xx ) = @_;
238 42 my $r = $self->{cache};
239 42 my $ttl = _ttl($session);
240 42 my ( @cmds, $queueErr, $reply, $execErr );
241  
242 # $fatal: an error of this command fails the write, like outside a
243 # transaction. Other errors are ignored (key of another type)
244 196 ▶ my $queue = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
245 196 my ( $cmd, $fatal, @args ) = @_;
246 196 push @cmds, [ $cmd, $fatal, @args ];
247 $r->$cmd( @args,
248 196 ▶ sub { $queueErr //= "[$cmd] $_[1]" if ( defined $_[1] ) } );
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 5 189 194
Condition50%1/2
$queueErr //= "[$cmd] $_[1]"
l!l
05
MC/DC0%0/1
$queueErr //= "[$cmd] $_[1]"
$queueErr
Truth table50%1/2
A
A= $queueErr
Aresult
11
00
249 42 };
250 42 my $sent = eval {
251 $r->multi(
252 42 ▶ sub { $queueErr //= "[multi] $_[1]" if ( defined $_[1] ) } );
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 1 41 42
Condition50%1/2
$queueErr //= "[multi] $_[1]"
l!l
01
MC/DC0%0/1
$queueErr //= "[multi] $_[1]"
$queueErr
Truth table50%1/2
A
A= $queueErr
Aresult
11
00
253 42 $self->_queueHashIndex( $queue, $session, $index, $old, $ttl, $xx );
254 40 ▶ $r->exec( sub { ( $reply, $execErr ) = @_ } );
Subroutine __ANON__: called CC=1 SCAR=0.0
255 40 $r->wait_all_responses;
256 40 1;
257 };
258  
259 # A connection left in a transaction would queue the next commands
260 42 ▶ unless ($sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 2 40 42
261 2 my $err = $@;
262 2 eval { $r->wait_all_responses };
263 2 eval { $r->discard };
264 2 die $err;
265 }
266 40 ▶ if ( my $err = $queueErr // $execErr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $queueErr // $execErr) 6 34 40
Condition66%2/3
$queueErr // $execErr
l!l&&r!l&&!r
6034
MC/DC50%1/2
$queueErr // $execErr
$queueErr $execErr
Truth table66%2/3
A // B
A= $queueErrB= $execErr
ABresult
1X1
011
000
267 6 ▶ die "utimeOffset needs Redis >= 7.4 or Valkey >= 9: $err\n"
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /unknown command/i 2 4 6
268 if ( $err =~ /unknown command/i );
269 4 die "$err\n";
270 }
271 34 ▶ return undef unless ( defined $reply );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reply 7 27 34
272 die "[exec] unexpected reply\n"
273 unless ( ref($reply) eq 'ARRAY'
274 and @$reply == @cmds
275 27 ▶ and !grep { ref($_) ne 'ARRAY' } @$reply );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $reply eq "ARRAY" and @$reply == @cmds and not grep({ref $_ ne "ARRAY";} @$reply) 8 19 27
Condition100%6/6
ref $reply eq "ARRAY" and @$reply == @cmds and not grep({ref $_ ne "ARRAY";} @$reply)
!ll&&!rl&&r
6219
ref $reply eq "ARRAY" and @$reply == @cmds
!ll&&!rl&&r
2421
MC/DC100%3/3
ref $reply eq "ARRAY" and @$reply == @cmds and not grep({ref $_ ne "ARRAY";} @$reply)
ref $reply eq "ARRAY" @$reply == @cmds not grep({ref $_ ne "ARRAY";} @$reply)
Truth table100%4/4
A and B and not(C)
A= ref $reply eq "ARRAY"B= @$reply == @cmdsC= not grep({ref $_ ne "ARRAY";} @$reply)
ABCresult
0XX0
10X0
1100
1111
276 19 foreach ( 0 .. $#cmds ) {
277 86 my $err = $reply->[$_]->[1];
278 86 ▶ die "[$cmds[$_]->[0]] $err\n" if ( defined $err and $cmds[$_]->[1] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $err and $cmds[$_][1] 2 84 86
Condition100%3/3
defined $err and $cmds[$_][1]
!ll&&!rl&&r
8132
MC/DC100%2/2
defined $err and $cmds[$_][1]
defined $err $cmds[$_][1]
Truth table100%3/3
A and B
A= defined $errB= $cmds[$_][1]
ABresult
0X0
100
111
279 }
280 17 ▶ return 1 if ( defined $reply->[0]->[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $reply->[0][0] 16 1 17
281  
282 # SET XX refused: the entries added in the transaction are orphans
283 1 foreach ( grep { $_->[0] =~ /^(?:hset|sadd)\z/ } @cmds ) {
284 1 my ( $cmd, undef, $name, $id ) = @$_;
285 1 eval {
286 1 ▶ $cmd eq 'hset' ? $r->hdel( $name, $id ) : $r->srem( $name, $id );
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'hset' ? : 1 0 1
287 };
288 }
289 1 return 0;
290 }
291  
292 42 ▶sub _queueHashIndex {
Subroutine _queueHashIndex: called CC=12 SCAR=24.9
293 42 my ( $self, $queue, $session, $index, $old, $ttl, $xx ) = @_;
294 42 my $data = $session->{data};
295 42 my $id = $data->{_session_id};
296 $queue->(
297 'set', 1, $id, $session->{serialized},
298 42 ▶ ( $ttl ? ( 'EX', $ttl ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
$ttl ? : 41 1 42
$xx ? : 21 21 42
299 ( $xx ? 'XX' : () )
300 );
301 42 foreach my $i (@$index) {
302 42 ▶ my $old_val = $old ? $old->{$i} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$old ? : 21 21 42
303 42 my $new_val = $data->{$i};
304 42 ▶ my $add = ( defined($new_val) and length($new_val) > 0 );
Condition66%2/3
defined $new_val && length $new_val > 0
!ll&&!rl&&r
1041
MC/DC50%1/2
defined $new_val && length $new_val > 0
defined $new_val length $new_val > 0
Truth table66%2/3
A && B
A= defined $new_valB= length $new_val > 0
ABresult
0X0
100
111
305 42 ▶ if ( defined($old_val)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_val and length $old_val > 0 and !$add || $old_val ne $new_val) 17 25 42
Condition88%8/9
defined $old_val and length $old_val > 0 and !$add || $old_val ne $new_val
!ll&&!rl&&r
21417
defined $old_val and length $old_val > 0
!ll&&!rl&&r
21021
not $add or $old_val ne $new_val
l!l&&r!l&&!r
1164
MC/DC75%3/4
defined $old_val and length $old_val > 0 and !$add || $old_val ne $new_val
defined $old_val length $old_val > 0 not $add $old_val ne $new_val
Truth table80%4/5
A and B and not(C) or D
A= defined $old_valB= length $old_val > 0C= not $addD= $old_val ne $new_val
ABCDresult
0XXX0
10XX0
11000
111X1
11011
306 and length($old_val) > 0
307 and ( !$add or $old_val ne $new_val ) )
308 {
309 17 $queue->( 'srem', 0, indexName( $i, $old_val ), $id );
310 17 $queue->( 'hdel', 0, hashIndexName( $i, $old_val ), $id );
311 }
312 42 ▶ next unless ($add);
Branch: 1 branch
BranchTrueFalseTotal
unless $add 1 41 42
313 41 my $set = indexName( $i, $new_val );
314 41 my $hash = hashIndexName( $i, $new_val );
315 41 ▶ if ($ttl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttl) { } 40 1 41
316  
317 # The set holds the sessions stored before utimeOffset was set
318 40 $queue->( 'srem', 0, $set, $id );
319 40 $queue->( 'hset', 1, $hash, $id, 1 );
320 38 $queue->( 'hexpire', 1, $hash, $ttl, 'FIELDS', 1, $id );
321 }
322 else {
323 1 $queue->( 'hdel', 0, $hash, $id );
324 1 $queue->( 'sadd', 1, $set, $id );
325 }
326 }
327 }
328  
329 # Stores of this process holding a new session not written yet (weak
330 # references), written by the END block if their session is still tied when
331 # the program ends: during the global destruction, the Redis client may be
332 # freed before the store
333 10 my %PENDING;
334 10 ▶END { _flushPending() }
Subroutine END: called CC=1 SCAR=0.0
335  
336 # A new session is written once, by update() at untie or, if it is untied
337 # unmodified, when the store is freed (see "DESCRIPTION"). Neither the
338 # session nor its arguments are kept: Lemonldap::NG's cache wrapper keeps
339 # this store in the arguments (object_store), a cycle that would keep both
340 # until the end of the program
341 29 ▶sub insert {
Subroutine insert: called CC=2 SCAR=6.9
Pod undocumented
342 29 my ( $self, $session ) = @_;
343 29 my $args = $session->{args};
344 my $index =
345 29 ▶ ref( $args->{Index} ) ? $args->{Index} : [ split /\s+/, $args->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'Index'} ? : 0 29 29
346 $self->{pending} = {
347 data => {
348 67 map { ( $_ => $session->{data}->{$_} ) }
349 117 grep { exists $session->{data}->{$_} }
350 qw(_session_id _utime _session_kind), @$index
351 },
352 serialized => $session->{serialized},
353 # The arguments open a new connection if the reused one is broken
354 # (see $CONNECTION_ERROR); objects are left out
355 args => {
356 (
357 47 map { ( $_ => $args->{$_} ) }
358 29 grep { !Scalar::Util::blessed( $args->{$_} ) } keys %$args
359 ),
360 Index => [@$index],
361 },
362 pid => $$,
363 };
364 29 Scalar::Util::weaken( $PENDING{ Scalar::Util::refaddr($self) } = $self );
365 29 return 1;
366 }
367  
368 11 ▶sub _flushPending {
Subroutine _flushPending: called CC=3 SCAR=11.1
369 11 foreach my $self ( grep { defined } values %PENDING ) {
370 1 ▶ my $pending = $self->_forgetPending or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pending = $self->_forgetPending or next 1 0 1
371 1 $self->_writePending($pending);
372 }
373 11 %PENDING = ();
374 }
375  
376 113 ▶sub _forgetPending {
Subroutine _forgetPending: called CC=1 SCAR=0.0
377 113 my ($self) = @_;
378 113 delete $PENDING{ Scalar::Util::refaddr($self) };
379 113 return delete $self->{pending};
380 }
381  
382 # A failure is reported as a warning, like a failed update at untie. A child
383 # created by fork() leaves the sessions of its parent to it: the connection
384 # is the parent's
385 14 ▶sub _writePending {
Subroutine _writePending: called CC=3 SCAR=11.0
386 14 my ( $self, $pending ) = @_;
387 14 ▶ return if ( $pending->{pid} != $$ );
Branch: 1 branch
BranchTrueFalseTotal
if $pending->{"pid"} != $$ 1 13 14
388 13 local $@;
389 13 ▶ return if ( eval { $self->_retried( $pending, \&_write ); 1 } );
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->_retried($pending, \&Web::Session::Store::Redis::_write); 1 } } 3 10 13
390 10 warn "Web::Session: unable to store new session "
391 . "$pending->{data}->{_session_id}: $@";
392 }
393  
394 91 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.1
395 91 my ($self) = @_;
396 91 ▶ my $pending = $self->_forgetPending or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pending = $self->_forgetPending or return 13 78 91
397 13 ▶ $self->_writePending($pending) unless ( ${^GLOBAL_PHASE} eq 'DESTRUCT' );
Branch: 1 branch
BranchTrueFalseTotal
unless ${^GLOBAL_PHASE} eq "DESTRUCT" 13 0 13
398 }
399  
400 # Writes a new session
401 39 ▶sub _write {
Subroutine _write: called CC=8 SCAR=20.8
402 39 my ( $self, $session ) = @_;
403 my $index =
404 ref( $session->{args}->{Index} )
405 ? $session->{args}->{Index}
406 39 ▶ : [ split /\s+/, $session->{args}->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $session->{'args'}{'Index'} ? : 14 25 39
407  
408 39 ▶ if ( @$index and hashIndex( $session->{args} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$index and hashIndex($session->{'args'})) { } 21 18 39
Condition100%3/3
@$index and hashIndex($session->{"args"})
!ll&&!rl&&r
61221
MC/DC100%2/2
@$index and hashIndex($session->{"args"})
@$index hashIndex($session->{"args"})
Truth table100%3/3
A and B
A= @$indexB= hashIndex($session->{"args"})
ABresult
0X0
100
111
409 21 ▶ $self->_writeHashIndex( $session, $index )
Condition50%1/2
$self->_writeHashIndex($session, $index) // die("[exec] unexpected reply\n")
l!l
50
MC/DC0%0/1
$self->_writeHashIndex($session, $index) // die("[exec] unexpected reply\n")
$self->_writeHashIndex($session, $index)
Truth table50%1/2
A
A= $self->_writeHashIndex($session, $index)
Aresult
11
00
410 // die "[exec] unexpected reply\n";
411 }
412 else {
413 18 my $id = $session->{data}->{_session_id};
414 18 my @cmds = ( [ 'set', 1, _setArgs($session) ] );
415 18 foreach my $i (@$index) {
416 20 my $t = $session->{data}->{$i};
417 20 ▶ next unless ( defined($t) and ( length($t) > 0 ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and length $t > 0 3 17 20
Condition66%2/3
defined $t and length $t > 0
!ll&&!rl&&r
3017
MC/DC50%1/2
defined $t and length $t > 0
defined $t length $t > 0
Truth table66%2/3
A and B
A= defined $tB= length $t > 0
ABresult
0X0
100
111
418 17 push @cmds, [ 'sadd', 1, indexName( $i, $t ), $id ];
419 }
420 18 $self->_pipeline(@cmds);
421 }
422 15 $session->{ +READ } = $session->{serialized};
423 15 $session->{ +STATUS } = 'written';
424 15 return 1;
425 }
426  
427 # Dies if the session was deleted meanwhile: a wrapper that caches it (such
428 # as the local cache of Lemonldap::NG) must not keep it
429 78 ▶sub _updateSession {
Subroutine _updateSession: called CC=3 SCAR=11.0
430 78 my ( $self, $session ) = @_;
431 78 delete $session->{ +STATUS };
432 78 ▶ if ( my $pending = $self->{pending} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pending = $self->{"pending"}) 25 53 78
433  
434 # Written under the id the session had at tie: Lemonldap::NG's hashed
435 # session store changes _session_id while the session is tied and
436 # puts it back before untie
437 25 my $id = $pending->{data}->{_session_id};
438  
439 # If the write fails, the store tries it again when it is freed: with
440 # the data of this save, not those of the tie (an empty session that
441 # would replace what the failed write may have stored, as a
442 # transaction is not rolled back when one of its commands fails)
443 25 $pending->{serialized} = $session->{serialized};
444 $pending->{data} = {
445 (
446 41 map { ( $_ => $session->{data}->{$_} ) }
447 77 grep { exists $session->{data}->{$_} }
448 25 qw(_utime _session_kind), @{ $pending->{args}->{Index} }
449 ),
450 _session_id => $id,
451 };
452 25 $pending->{tried} = 1;
453 25 local $session->{data} = { %{ $session->{data} }, _session_id => $id };
454 25 $self->_write($session);
455  
456 # Forgotten once written only: after a failure, a new save() writes
457 # the session again, without version check
458 12 $self->_forgetPending;
459 12 return 1;
460 }
461 53 $session->{ +STATUS } = $self->_update($session);
462 die "Session '$session->{data}->{_session_id}' was deleted or expired, "
463 . "not stored again\n"
464 45 ▶ if ( $session->{ +STATUS } eq 'deleted' );
Branch: 1 branch
BranchTrueFalseTotal
if $session->{"web_session_redis_update"} eq "deleted" 11 34 45
465 34 return 1;
466 }
467  
468 # Returns the status of the update, see "DESCRIPTION"
469 53 ▶sub _update {
Subroutine _update: called CC=14 SCAR=26.4
470 53 my ( $self, $session ) = @_;
471 my $index =
472 ref( $session->{args}->{Index} )
473 ? $session->{args}->{Index}
474 53 ▶ : [ split /\s+/, $session->{args}->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $session->{'args'}{'Index'} ? : 1 52 53
475  
476 return $self->_updateHashIndex( $session, $index )
477 53 ▶ if ( @$index and hashIndex( $session->{args} ) );
Branch: 1 branch
BranchTrueFalseTotal
if @$index and hashIndex($session->{"args"}) 16 37 53
Condition100%3/3
@$index and hashIndex($session->{"args"})
!ll&&!rl&&r
132416
MC/DC100%2/2
@$index and hashIndex($session->{"args"})
@$index hashIndex($session->{"args"})
Truth table100%3/3
A and B
A= @$indexB= hashIndex($session->{"args"})
ABresult
0X0
100
111
478  
479 37 my $id = $session->{data}->{_session_id};
480 37 my $old_data;
481  
482 37 my $status = 'overwritten';
483 37 ▶ if ( defined $session->{ +READ } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session->{'web_session_redis_read'}) { } 26 11 37
484 26 ( $status, my $old_raw, my $indexed, my $err ) =
485 $self->_writeMerged( $session, $id, $index );
486 23 ▶ return $status if ( $status eq 'deleted' or !@$index );
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "deleted" or not @$index 11 12 23
Condition100%3/3
$status eq "deleted" or not @$index
l!l&&r!l&&!r
5612
MC/DC100%2/2
$status eq "deleted" or not @$index
$status eq "deleted" not @$index
Truth table100%3/3
A or not(B)
A= $status eq "deleted"B= not @$index
ABresult
1X1
011
000
487  
488 # Index entries replaced by the Lua script (see $CAS)
489 12 ▶ if ($indexed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indexed) 3 9 12
490 3 ▶ die "[sadd] $err\n" if ( defined $err );
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 1 2 3
491 2 return $status;
492 }
493 9 $old_data = _decodeOld( $session, $old_raw );
494 }
495 else {
496  
497 # The version read is unknown (unserialize isn't the one installed by
498 # Web::Session::Redis): overwrite. Nothing to clean up without index
499 11 ▶ unless (@$index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$index) 6 5 11
500 6 ▶ return $self->_overwrite($session) ? $status : 'deleted';
Branch: 1 branch
BranchTrueFalseTotal
$self->_overwrite($session) ? : 3 2 5
501 }
502  
503 # Read old data to clean up stale index entries
504 5 my $old_raw = eval { $self->{cache}->get($id) };
505 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
506 0 warn "Failed to read previous session '$id' from Redis: $@";
507 }
508 5 $old_data = _decodeOld( $session, $old_raw );
509  
510 5 ▶ return 'deleted' unless ( $self->_overwrite($session) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_overwrite($session) 2 3 5
511 }
512  
513 12 my @cmds = _indexCmds( $index, $old_data, $session->{data}, $id );
514 12 ▶ $self->_pipeline(@cmds) if (@cmds);
Branch: 1 branch
BranchTrueFalseTotal
if @cmds 10 2 12
515 12 return $status;
516 }
517  
518 # Commands [ srem|sadd, fatal, set, $id ] that replace the index entries of
519 # $old_data (the version replaced) by those of $data
520 43 ▶sub _indexCmds {
Subroutine _indexCmds: called CC=10 SCAR=23.4
521 43 my ( $index, $old_data, $data, $id ) = @_;
522 43 my @cmds;
523 43 foreach my $i (@$index) {
524 44 ▶ my $old_val = defined($old_data) ? $old_data->{$i} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $old_data ? : 43 1 44
525 44 my $new_val = $data->{$i};
526  
527 # Remove old index entry if value changed
528 44 ▶ if ( defined($old_val) && length($old_val) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_val and length $old_val > 0) 32 12 44
Condition66%2/3
defined $old_val and length $old_val > 0
!ll&&!rl&&r
12032
MC/DC50%1/2
defined $old_val and length $old_val > 0
defined $old_val length $old_val > 0
Truth table66%2/3
A and B
A= defined $old_valB= length $old_val > 0
ABresult
0X0
100
111
529 32 ▶ if ( !defined($new_val)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $new_val or length $new_val == 0 or $old_val ne $new_val) 21 11 32
Condition50%3/6
not defined $new_val or length $new_val == 0 or $old_val ne $new_val
l!l&&r!l&&!r
02111
not defined $new_val or length $new_val == 0
l!l&&r!l&&!r
0032
MC/DC33%1/3
not defined $new_val or length $new_val == 0 or $old_val ne $new_val
not defined $new_val length $new_val == 0 $old_val ne $new_val
Truth table50%2/4
not(A) or B or C
A= not defined $new_valB= length $new_val == 0C= $old_val ne $new_val
ABCresult
1XX1
01X1
0011
0000
530 || length($new_val) == 0
531 || $old_val ne $new_val )
532 {
533 21 push @cmds, [ 'srem', 0, indexName( $i, $old_val ), $id ];
534 }
535 }
536  
537 # Add new index entry
538 44 ▶ if ( defined($new_val) && length($new_val) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_val and length $new_val > 0) 34 10 44
Condition66%2/3
defined $new_val and length $new_val > 0
!ll&&!rl&&r
10034
MC/DC50%1/2
defined $new_val and length $new_val > 0
defined $new_val length $new_val > 0
Truth table66%2/3
A and B
A= defined $new_valB= length $new_val > 0
ABresult
0X0
100
111
539 34 push @cmds, [ 'sadd', 1, indexName( $i, $new_val ), $id ];
540 }
541 }
542 43 return @cmds;
543 }
544  
545 # Decoded like the session itself: Redis returns Latin-1, not UTF-8
546 96 ▶sub _decode {
Subroutine _decode: called CC=1 SCAR=0.0
547 96 my ( $session, $raw ) = @_;
548 96 my $tmp = { serialized => $raw };
549 96 $session->{unserialize}->($tmp);
550 96 return $tmp->{data};
551 }
552  
553 # Previous version, whose index entries are replaced
554 66 ▶sub _decodeOld {
Subroutine _decodeOld: called CC=4 SCAR=14.6
555 66 my ( $session, $raw ) = @_;
556 66 ▶ return undef unless ( defined $raw and length $raw );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw and length $raw 2 64 66
Condition66%2/3
defined $raw and length $raw
!ll&&!rl&&r
2064
MC/DC50%1/2
defined $raw and length $raw
defined $raw length $raw
Truth table66%2/3
A and B
A= defined $rawB= length $raw
ABresult
0X0
100
111
557 64 my $data = eval { _decode( $session, $raw ) };
558 64 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 64 64
559 0 my $id = $session->{data}->{_session_id};
560 0 warn "Failed to decode previous session '$id': $@";
561 }
562 64 return $data;
563 }
564  
565 # Sessions aren't locked (see "DESCRIPTION"): a write is accepted only if Redis
566 # still holds the version this process read, otherwise the caller's changes are
567 # merged into the stored version and the write is tried again. The index
568 # entries of the version replaced are replaced by the Lua script (see $CAS).
569 # Returns the status of the update, the version replaced and, if the script
570 # replaced the index entries, a true value and the error of SADD (or undef)
571 26 ▶sub _writeMerged {
Subroutine _writeMerged: called CC=12 SCAR=24.9
572 26 my ( $self, $session, $id, $index ) = @_;
573 26 my $expected = $session->{ +READ };
574 26 my $merge = _merger( $session, $id );
575 26 my ( $merged, $failed );
576 26 for ( 1 .. $MAX_TRIES ) {
577  
578 # Index entries of the version that would be replaced
579 my @cmds =
580 ( $index and @$index )
581 ? _indexCmds( $index, _decodeOld( $session, $expected ),
582 40 ▶ $session->{data}, $id )
Branch: 1 branch
BranchTrueFalseTotal
$index && @$index ? : 31 9 40
Condition66%2/3
$index and @$index
!ll&&!rl&&r
0931
MC/DC50%1/2
$index and @$index
$index @$index
Truth table66%2/3
A and B
A= $indexB= @$index
ABresult
0X0
100
111
583 : ();
584 my ( $res, $cur, $err ) =
585 40 @{ $self->_compareAndSet( $id, $expected, $session->{serialized},
586 40 _ttl($session), \@cmds ) };
587 37 ▶ if ( $res eq 'written' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res eq "written") 12 25 37
588 12 $session->{ +READ } = $session->{serialized};
589 12 ▶ return ( $merged ? 'merged' : 'written', $expected, $cur, $err );
Branch: 1 branch
BranchTrueFalseTotal
$merged ? : 5 7 12
590 }
591 25 ▶ return ('deleted') if ( $res eq 'deleted' );
Branch: 1 branch
BranchTrueFalseTotal
if $res eq "deleted" 4 21 25
592 21 ▶ next if ( $res eq 'aborted' );
Branch: 1 branch
BranchTrueFalseTotal
if $res eq "aborted" 1 20 21
593 20 ▶ if ( $res eq 'refused' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res eq "refused") 5 15 20
594 5 _warnUnchecked($cur);
595 5 $failed = 1;
596 5 last;
597 }
598 15 $expected = $cur;
599 15 ▶ unless ( $merge->($expected) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$merge($expected)) 0 15 15
600 0 $failed = 1;
601 0 last;
602 }
603 15 $merged = 1;
604 }
605 7 ▶ _warnOverwrite($id) unless ($failed);
Branch: 1 branch
BranchTrueFalseTotal
unless $failed 2 5 7
606 7 ▶ return ('deleted') unless ( $self->_overwrite($session) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_overwrite($session) 1 6 7
607 6 $session->{ +READ } = $session->{serialized};
608 6 return ( 'overwritten', $expected );
609 }
610  
611 # Same as _writeMerged() with index hashes: the version check is part of the
612 # transaction of _writeHashIndex(), so that the index entries replaced are
613 # those of the version replaced. WATCH aborts EXEC if the session changes after
614 # GET; if WATCH is refused, the transaction is run without it. Without the
615 # version read, the session is overwritten. Returns the status of the update
616 16 ▶sub _updateHashIndex {
Subroutine _updateHashIndex: called CC=16 SCAR=27.8
617 16 my ( $self, $session, $index ) = @_;
618 16 my $r = $self->{cache};
619 16 my $id = $session->{data}->{_session_id};
620 16 my $expected = $session->{ +READ };
621 16 ▶ my $status = defined($expected) ? 'written' : 'overwritten';
Branch: 1 branch
BranchTrueFalseTotal
defined $expected ? : 11 5 16
622 16 my $merge = _merger( $session, $id );
623 16 my ( $cur, $done, $failed );
624 eval {
625 16 for ( 1 .. $MAX_TRIES ) {
626 28 ( $cur, my $err ) = $self->_watchGet($id);
627 27 ▶ if ( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 2 25 27
628 2 ▶ die "$err\n" unless ( _refused($err) );
Branch: 1 branch
BranchTrueFalseTotal
unless _refused($err) 0 2 2
629 2 _warnUnchecked($err);
630 2 $failed = 1;
631 2 ▶ $done = 0 unless ( defined $cur );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cur 0 2 2
632 2 last;
633 }
634 25 ▶ unless ( defined $cur ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cur) 1 24 25
635 1 $r->unwatch;
636 1 $done = 0;
637 1 last;
638 }
639 24 ▶ if ( defined $expected and $cur ne $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected and $cur ne $expected) 7 17 24
Condition100%3/3
defined $expected and $cur ne $expected
!ll&&!rl&&r
5127
MC/DC100%2/2
defined $expected and $cur ne $expected
defined $expected $cur ne $expected
Truth table100%3/3
A and B
A= defined $expectedB= $cur ne $expected
ABresult
0X0
100
111
640 7 $r->unwatch;
641 7 $expected = $cur;
642 7 $status = 'merged';
643 7 ▶ next if ( $merge->($cur) );
Branch: 1 branch
BranchTrueFalseTotal
if &$merge($cur) 7 0 7
644 0 $failed = 1;
645 0 last;
646 }
647  
648 # undef: the session changed after GET, read it again
649 17 $done = $self->_writeHashIndex( $session, $index,
650 _decodeOld( $session, $cur ), 1 );
651 16 ▶ last if ( defined $done );
Branch: 1 branch
BranchTrueFalseTotal
if defined $done 9 7 16
652 }
653 14 1;
654 16 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach $_ (1 .. $Web::Session::Store::Redis::MAX_TRIES) { ($cur, my $err) = $self->_watchGet($id); if (defined $err) { die "$err\n" unless _refused($err); _warnUnchecked($err); $failed = 1; $done = 0 unless defined $cur; last; } ; unless (defined $cur) { $r->unwatch; $done = 0; last; } ; if (defined $expected and $cur ne $expected) { $r->unwatch; $expected = $cur; $status = "merged"; next if &$merge($cur); $failed = 1; last; } ; $done = $self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1); last if defined $done; } ; 1 } } or do { my $err = $@; eval { do { $r->unwatch } }; die $err } 14 2 16
655 2 my $err = $@;
656 2 eval { $r->unwatch };
657 2 die $err;
658 };
659  
660 # Not watched: the index entries replaced are those of the last version
661 # read
662 14 ▶ unless ( defined $done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $done) 4 10 14
663 4 ▶ _warnOverwrite($id) unless ($failed);
Branch: 1 branch
BranchTrueFalseTotal
unless $failed 2 2 4
664 4 $status = 'overwritten';
665 4 ▶ $done = $self->_writeHashIndex( $session, $index,
Condition50%1/2
$self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1) // die("[exec] unexpected reply\n")
l!l
30
MC/DC0%0/1
$self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1) // die("[exec] unexpected reply\n")
$self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1)
Truth table50%1/2
A
A= $self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1)
Aresult
11
00
666 _decodeOld( $session, $cur ), 1 )
667 // die "[exec] unexpected reply\n";
668 }
669 13 ▶ return 'deleted' unless ($done);
Branch: 1 branch
BranchTrueFalseTotal
unless $done 2 11 13
670 11 $session->{ +READ } = $session->{serialized};
671 11 return $status;
672 }
673  
674 # WATCH and GET of session $id in one round trip: GET runs even if WATCH is
675 # refused. Returns the session and the error of WATCH; dies if GET fails
676 28 ▶sub _watchGet {
Subroutine _watchGet: called CC=3 SCAR=11.0
677 28 my ( $self, $id ) = @_;
678 28 my $r = $self->{cache};
679 28 my ( $cur, $watchErr, $getErr );
680 28 my $sent = eval {
681 $r->watch( $id,
682 28 ▶ sub { $watchErr = "[watch] $_[1]" if ( defined $_[1] ) } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 2 26 28
683 28 ▶ $r->get( $id, sub { ( $cur, $getErr ) = @_ } );
Subroutine __ANON__: called CC=1 SCAR=0.0
684 27 $r->wait_all_responses;
685 27 1;
686 };
687 28 ▶ unless ($sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 1 27 28
688 1 my $e = $@;
689 1 eval { $r->wait_all_responses };
690 1 die $e;
691 }
692 27 ▶ die "[get] $getErr\n" if ( defined $getErr );
Branch: 1 branch
BranchTrueFalseTotal
if defined $getErr 0 27 27
693 27 return ( $cur, $watchErr );
694 }
695  
696 # Refusals that a retry won't change: ACL, command renamed or not supported by
697 # a proxy. Connection errors are not
698 9 ▶sub _refused {
Subroutine _refused: called CC=2 SCAR=7.2
699 9 ▶ return $_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/i
Condition66%2/3
$_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/i || $_[0] =~ /unsupported|forbid/i
l!l&&r!l&&!r
702
MC/DC50%1/2
$_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/i || $_[0] =~ /unsupported|forbid/i
$_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/i $_[0] =~ /unsupported|forbid/i
Truth table66%2/3
A || B
A= $_[0] =~ /\bNOPERM\b|unknown command|not (?:allowed|supported)/iB= $_[0] =~ /unsupported|forbid/i
ABresult
1X1
011
000
700 || $_[0] =~ /unsupported|forbid/i;
701 }
702  
703 10 our $unchecked_warned;
704  
705 7 ▶sub _warnUnchecked {
Subroutine _warnUnchecked: called CC=2 SCAR=6.9
706 7 ▶ return if ($unchecked_warned);
Branch: 1 branch
BranchTrueFalseTotal
if $Web::Session::Store::Redis::unchecked_warned 2 5 7
707 5 $unchecked_warned = 1;
708  
709 # First line only: Redis::Fast adds a backtrace holding the arguments
710 5 my ($err) = split /\n/, $_[0];
711 5 warn "Redis refused WATCH/MULTI, concurrent updates of sessions are not "
712 . "merged: $err\n";
713 }
714  
715 4 ▶sub _warnOverwrite {
Subroutine _warnOverwrite: called CC=1 SCAR=0.0
716 4 warn "Session '$_[0]' was updated concurrently too often, overwriting it\n";
717 }
718  
719 # Returns a function that applies to the version stored, its argument, what
720 # the caller changed since the version it read, and serializes the result in
721 # $session. It returns false, with a warning, if they can't be merged
722 42 ▶sub _merger {
Subroutine _merger: called CC=1 SCAR=0.2
723 42 my ( $session, $id ) = @_;
724 42 my ( $orig, $new );
725 22 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=17.8
726 22 my ($cur) = @_;
727 22 my $merged = eval {
728 22 ▶ $orig //= _decode( $session, $session->{ +READ } );
Condition66%2/3
$orig //= _decode($session, $session->{"web_session_redis_read"})
l!l&&r!l&&!r
12100
MC/DC0%0/2
$orig //= _decode($session, $session->{"web_session_redis_read"})
$orig _decode($session, $session->{"web_session_redis_read"})
Truth table66%2/3
A //= B
A= $origB= _decode($session, $session->{"web_session_redis_read"})
ABresult
1X1
011
000
729 22 ▶ $new //= $session->{data};
Condition66%2/3
$new //= $session->{"data"}
l!l&&r!l&&!r
12100
MC/DC0%0/2
$new //= $session->{"data"}
$new $session->{"data"}
Truth table66%2/3
A //= B
A= $newB= $session->{"data"}
ABresult
1X1
011
000
730 22 _merge( $orig, _decode( $session, $cur ), $new, $id );
731 };
732 22 ▶ unless ( ref($merged) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $merged eq "HASH") 0 22 22
733 0 ▶ warn "Unable to merge session '$id' with a concurrent update, "
Condition0%0/2
$@ || "not a session\n"
l!l
00
MC/DC0%0/1
$@ || "not a session\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
734 . "overwriting it: "
735 . ( $@ || "not a session\n" );
736 0 return 0;
737 }
738 22 $session->{data} = $merged;
739 22 $session->{serialize}->($session);
740 22 return 1;
741 42 };
742 }
743  
744 # Three-way merge of top-level keys: what the caller changed or deleted since
745 # $orig is applied to $cur
746 32 ▶sub _merge {
Subroutine _merge: called CC=6 SCAR=17.9
747 32 my ( $orig, $cur, $new, $id ) = @_;
748 32 my %res = %$cur;
749 32 foreach my $k ( keys %$orig ) {
750 123 ▶ delete $res{$k} unless ( exists $new->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $new->{$k} 1 122 123
751 }
752 32 foreach my $k ( keys %$new ) {
753 $res{$k} = $new->{$k}
754 125 ▶ unless ( exists $orig->{$k} and _same( $orig->{$k}, $new->{$k} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $orig->{$k} and _same($orig->{$k}, $new->{$k}) 31 94 125
Condition100%3/3
exists $orig->{$k} and _same($orig->{$k}, $new->{$k})
!ll&&!rl&&r
32894
MC/DC100%2/2
exists $orig->{$k} and _same($orig->{$k}, $new->{$k})
exists $orig->{$k} _same($orig->{$k}, $new->{$k})
Truth table100%3/3
A and B
A= exists $orig->{$k}B= _same($orig->{$k}, $new->{$k})
ABresult
0X0
100
111
755 }
756 32 $res{_session_id} = $id;
757 32 return \%res;
758 }
759  
760 10 my $cmpJson = JSON->new->canonical->allow_nonref;
761  
762 # A number used as a string is still the same value: scalars are compared
763 # with eq, references by their canonical JSON
764 122 ▶sub _same {
Subroutine _same: called CC=8 SCAR=22.1
765 122 my ( $x, $y ) = @_;
766 122 ▶ return !defined($y) unless ( defined $x );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 122 122
767 122 ▶ return 0 unless ( defined $y );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y 0 122 122
768 122 ▶ return $x eq $y unless ( ref($x) or ref($y) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x or ref $y 112 10 122
Condition66%2/3
ref $x or ref $y
l!l&&r!l&&!r
100112
MC/DC50%1/2
ref $x or ref $y
ref $x ref $y
Truth table66%2/3
A or B
A= ref $xB= ref $y
ABresult
1X1
011
000
769 10 ▶ return 0 unless ( ref($x) and ref($y) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x and ref $y 0 10 10
Condition33%1/3
ref $x and ref $y
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $x and ref $y
ref $x ref $y
Truth table33%1/3
A and B
A= ref $xB= ref $y
ABresult
0X0
100
111
770 10 ▶ return eval { $cmpJson->encode($x) eq $cmpJson->encode($y) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $cmpJson->encode($x) eq $cmpJson->encode($y) } } ? : 9 1 10
771 }
772  
773 # KEYS[1]: session, then the index sets to leave, then those to join. ARGV:
774 # version read, new version, TTL or '', number of sets to leave. Errors of
775 # SREM are ignored; the first error of SADD is returned, the session being
776 # written, as outside the script (see "DESCRIPTION" for proxies)
777 10 our $CAS = q{local cur = redis.call('get', KEYS[1])
778 if not cur then return {'deleted'} end
779 if cur ~= ARGV[1] then return {'conflict', cur} end
780 if ARGV[3] == '' then redis.call('set', KEYS[1], ARGV[2], 'XX')
781 else redis.call('set', KEYS[1], ARGV[2], 'EX', ARGV[3], 'XX') end
782 local nrem = tonumber(ARGV[4]) or 0
783 local err
784 for i = 2, #KEYS do
785 if i <= nrem + 1 then redis.pcall('srem', KEYS[i], KEYS[1])
786 else
787 local r = redis.pcall('sadd', KEYS[i], KEYS[1])
788 if type(r) == 'table' and r.err and not err then err = r.err end
789 end
790 end
791 return {'written', 1, err}};
792  
793 10 our $lua_warned;
794  
795 # Write $serialized if $id still holds $expected, and replace its index
796 # entries with the commands @$cmds of _indexCmds(). Returns ['written', 1,
797 # error of SADD or undef], ['written'] if the index entries were not replaced
798 # (without Lua), ['deleted'], ['conflict', current version], ['aborted'] or
799 # ['refused', error]. Without Lua (EVAL forbidden by ACL, proxy...), WATCH the
800 # session so that a concurrent write aborts EXEC
801 40 ▶sub _compareAndSet {
Subroutine _compareAndSet: called CC=17 SCAR=28.4
802 40 my ( $self, $id, $expected, $serialized, $ttl, $cmds ) = @_;
803 40 my $redisObj = $self->{cache};
804 40 ▶ my @rem = map { $_->[2] } grep { $_->[0] eq 'srem' } @{ $cmds || [] };
Condition50%1/2
$cmds || []
l!l
400
MC/DC0%0/1
$cmds || []
$cmds
Truth table50%1/2
A
A= $cmds
Aresult
11
00
805 40 ▶ my @add = map { $_->[2] } grep { $_->[0] eq 'sadd' } @{ $cmds || [] };
Condition50%1/2
$cmds || []
l!l
400
MC/DC0%0/1
$cmds || []
$cmds
Truth table50%1/2
A
A= $cmds
Aresult
11
00
806 40 my $res = eval {
807 40 ▶ $redisObj->eval( $CAS, 1 + @rem + @add, $id, @rem, @add, $expected,
Branch: 1 branch
BranchTrueFalseTotal
$ttl ? : 3 37 40
808 $serialized, ( $ttl ? $ttl : '' ), scalar(@rem) );
809 };
810 40 ▶ return $res if ( ref($res) eq 'ARRAY' and !$@ );
Branch: 1 branch
BranchTrueFalseTotal
if ref $res eq "ARRAY" and not $@ 26 14 40
Condition66%2/3
ref $res eq "ARRAY" and not $@
!ll&&!rl&&r
14026
MC/DC50%1/2
ref $res eq "ARRAY" and not $@
ref $res eq "ARRAY" not $@
Truth table66%2/3
A and not(B)
A= ref $res eq "ARRAY"B= not $@
ABresult
0X0
100
111
811  
812 # Lost connection or failover: not a refusal of EVAL
813 14 ▶ die $@ if ( $@ =~ $CONNECTION_ERROR );
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /$Web::Session::Store::Redis::CONNECTION_ERROR/ 1 13 14
814 13 ▶ unless ($lua_warned) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Web::Session::Store::Redis::lua_warned) 10 3 13
815 10 $lua_warned = 1;
816  
817 # First line only: Redis::Fast adds a backtrace holding the arguments,
818 # that is the session id and data
819 10 ▶ my ($err) = split /\n/, ( $@ || 'unexpected reply' );
Condition50%1/2
$@ || 'unexpected reply'
l!l
100
MC/DC0%0/1
$@ || 'unexpected reply'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
820 10 print STDERR "Redis EVAL failed, sessions are updated without Lua: "
821 . "$err\n";
822 }
823 eval {
824 13 $redisObj->watch($id);
825 8 my $cur = $redisObj->get($id);
826 7 ▶ if ( !defined $cur or $cur ne $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cur or $cur ne $expected) 2 5 7
Condition100%3/3
not defined $cur or $cur ne $expected
l!l&&r!l&&!r
115
MC/DC100%2/2
not defined $cur or $cur ne $expected
not defined $cur $cur ne $expected
Truth table100%3/3
not(A) or B
A= not defined $curB= $cur ne $expected
ABresult
1X1
011
000
827 2 $redisObj->unwatch;
828 2 ▶ $res = defined($cur) ? [ 'conflict', $cur ] : ['deleted'];
Branch: 1 branch
BranchTrueFalseTotal
defined $cur ? : 1 1 2
829 2 return 1;
830 }
831 5 $redisObj->multi;
832 4 ▶ $ttl
Branch: 1 branch
BranchTrueFalseTotal
$ttl ? : 1 3 4
833 ? $redisObj->set( $id, $serialized, 'EX', $ttl, 'XX' )
834 : $redisObj->set( $id, $serialized, 'XX' );
835 4 my $done = $redisObj->exec;
836  
837 # undef: the session changed after GET
838 4 ▶ $res =
Branch: 2 branches
BranchTrueFalseTotal
!$done ? : 1 3 4
defined $done->[0] ? : 2 1 3
839 !$done ? ['aborted']
840 : defined( $done->[0] ) ? ['written']
841 : ['deleted'];
842 4 1;
843 13 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $redisObj->watch($id); my $cur = $redisObj->get($id); if (not defined $cur or $cur ne $expected) { $redisObj->unwatch; $res = defined $cur ? ["conflict", $cur] : ["deleted"]; return 1; } ; $redisObj->multi; $ttl ? $redisObj->set($id, $serialized, "EX", $ttl, "XX") : $redisObj->set($id, $serialized, "XX"); my $done = $redisObj->exec; $res = !$done ? ["aborted"] : (defined $done->[0] ? ["written"] : ["deleted"]); 1 } } or do { my $err = $@; eval { do { $redisObj->discard } }; eval { do { $redisObj->unwatch } }; return ["refused", $err] if _refused($err); die $err } 6 7 13
844 7 my $err = $@;
845 7 eval { $redisObj->discard };
846 7 eval { $redisObj->unwatch };
847 7 ▶ return [ 'refused', $err ] if ( _refused($err) );
Branch: 1 branch
BranchTrueFalseTotal
if _refused($err) 5 2 7
848 2 die $err;
849 };
850 6 return $res;
851 }
852  
853 6 ▶sub _read {
Subroutine _read: called CC=2 SCAR=7.2
854 6 my ( $self, $session ) = @_;
855 $session->{serialized} =
856 $self->{cache}->get( $session->{data}->{_session_id} )
857 6 ▶ or die 'Object does not exist in data store.';
Branch: 1 branch
BranchTrueFalseTotal
$session->{"serialized"} = $self->{"cache"}->get($session->{"data"}{"_session_id"}) or die "Object does not exist in data store." 2 0 2
858 }
859  
860 8 ▶sub _removeSession {
Subroutine _removeSession: called CC=7 SCAR=19.5
861 8 my ( $self, $session ) = @_;
862  
863 # Created and removed in the same tie: nothing was written, so nothing is
864 # deleted, unless a write was tried
865 8 my $pending = $self->_forgetPending;
866 8 ▶ return 1 if ( $pending and !$pending->{tried} );
Branch: 1 branch
BranchTrueFalseTotal
if $pending and not $pending->{"tried"} 1 7 8
Condition100%3/3
$pending and not $pending->{"tried"}
!ll&&!rl&&r
611
MC/DC100%2/2
$pending and not $pending->{"tried"}
$pending not $pending->{"tried"}
Truth table100%3/3
A and not(B)
A= $pendingB= not $pending->{"tried"}
ABresult
0X0
100
111
867 my $index =
868 ref( $session->{args}->{Index} )
869 ? $session->{args}->{Index}
870 7 ▶ : [ split /\s+/, $session->{args}->{Index} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $session->{'args'}{'Index'} ? : 0 7 7
871  
872 7 my $id = $session->{data}->{_session_id};
873 7 my $hash = hashIndex( $session->{args} );
874 7 my @cmds;
875 7 foreach my $i (@$index) {
876 9 my $t;
877 9 ▶ next unless ( $t = $session->{data}->{$i} );
Branch: 1 branch
BranchTrueFalseTotal
unless $t = $session->{"data"}{$i} 0 9 9
878 9 push @cmds, [ 'srem', 0, indexName( $i, $t ), $id ];
879 9 ▶ push @cmds, [ 'hdel', 0, hashIndexName( $i, $t ), $id ] if ($hash);
Branch: 1 branch
BranchTrueFalseTotal
if $hash 3 6 9
880 }
881 7 $self->_pipeline( @cmds, [ 'del', 1, $id ] );
882 }
883  
884 # Errors of a reused connection, see $CONNECTION_ERROR
885 4 ▶sub materialize {
Subroutine materialize: called CC=1 SCAR=0.0
Pod undocumented
886 4 my ( $self, $session ) = @_;
887 4 return $self->_retried( $session, \&_read );
888 }
889  
890 # The write of a new session is tried again at once on a new connection: it
891 # writes the same data under the same id. An update is not (see
892 # $CONNECTION_ERROR)
893 77 ▶sub update {
Subroutine update: called CC=2 SCAR=6.9
Pod documented
894 77 my ( $self, $session ) = @_;
895 return $self->_retried( $session, \&_updateSession )
896 77 ▶ if ( $self->{pending} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pending"} 24 53 77
897 53 return $self->_onConnection( $session, \&_updateSession );
898 }
899  
900 # Runs $code; on a connection error, runs it again once on a new connection
901 41 ▶sub _retried {
Subroutine _retried: called CC=3 SCAR=11.0
902 41 my ( $self, $session, $code ) = @_;
903 41 my $res;
904 41 ▶ return $res if ( eval { $res = $self->$code($session); 1 } );
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $res = $self->$code($session); 1 } } 13 28 41
905 28 my $err = $@;
906 28 ▶ die $err unless ( $self->_forgetBroken( $session->{args}, $err ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_forgetBroken($session->{"args"}, $err) 24 4 28
907 4 $self->{cache} = ref($self)->_getRedis( $session->{args} );
908 4 return $self->_onConnection( $session, $code );
909 }
910  
911 8 ▶sub remove {
Subroutine remove: called CC=1 SCAR=0.0
Pod undocumented
912 8 my ( $self, $session ) = @_;
913 8 return $self->_onConnection( $session, \&_removeSession );
914 }
915  
916 65 ▶sub _onConnection {
Subroutine _onConnection: called CC=2 SCAR=6.9
917 65 my ( $self, $session, $code ) = @_;
918 65 my $res;
919 65 ▶ return $res if ( eval { $res = $self->$code($session); 1 } );
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $res = $self->$code($session); 1 } } 43 22 65
920 22 my $err = $@;
921 22 $self->_forgetBroken( $session->{args}, $err );
922 22 die $err;
923 }
924  
925 # Forgets the reused connection of this store if $err is a connection error.
926 # Returns true then
927 50 ▶sub _forgetBroken {
Subroutine _forgetBroken: called CC=5 SCAR=16.1
928 50 my ( $self, $args, $err ) = @_;
929 50 ▶ my $key = $args->{reuse} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $key = $args->{"reuse"} or return 0 9 41 50
930 9 ▶ return 0 unless ( $err =~ $CONNECTION_ERROR );
Branch: 1 branch
BranchTrueFalseTotal
unless $err =~ /$Web::Session::Store::Redis::CONNECTION_ERROR/ 1 8 9
931 8 ▶ my $cached = $reused_connections{$key} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $cached = $Web::Session::Store::Redis::reused_connections{$key} or return 0 8 0 8
932 return 0
933 unless ( Scalar::Util::refaddr($cached) ==
934 8 ▶ Scalar::Util::refaddr( $self->{cache} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::refaddr($cached) == builtin::refaddr($self->{"cache"}) 1 7 8
935 7 delete $reused_connections{$key};
936 7 delete $selected{$key};
937 7 return 1;
938 }
939  
940 10 ▶sub _getRedis {
Subroutine _getRedis: called CC=16 SCAR=29.3
941 10 my $class = shift;
942 10 my $args = shift;
943 10 my $redisObj;
944  
945 # Manage undef encoding
946 $args->{encoding} = undef
947 if ( $args->{encoding}
948 10 ▶ and $args->{encoding} eq "undef" );
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"encoding"} and $args->{"encoding"} eq "undef" 0 10 10
Condition33%1/3
$args->{"encoding"} and $args->{"encoding"} eq "undef"
!ll&&!rl&&r
1000
MC/DC0%0/2
$args->{"encoding"} and $args->{"encoding"} eq "undef"
$args->{"encoding"} $args->{"encoding"} eq "undef"
Truth table33%1/3
A and B
A= $args->{"encoding"}B= $args->{"encoding"} eq "undef"
ABresult
0X0
100
111
949  
950 # If sentinels is not given as an array ref, try to parse
951 # a comma delimited list instead
952 10 ▶ if ( $args->{sentinels}
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"sentinels"} and ref $args->{"sentinels"} ne "ARRAY") 0 10 10
Condition33%1/3
$args->{"sentinels"} and ref $args->{"sentinels"} ne "ARRAY"
!ll&&!rl&&r
1000
MC/DC0%0/2
$args->{"sentinels"} and ref $args->{"sentinels"} ne "ARRAY"
$args->{"sentinels"} ref $args->{"sentinels"} ne "ARRAY"
Truth table33%1/3
A and B
A= $args->{"sentinels"}B= ref $args->{"sentinels"} ne "ARRAY"
ABresult
0X0
100
111
953 and ref $args->{sentinels} ne 'ARRAY' )
954 {
955 $args->{sentinels} =
956 0 [ split /[,\s]+/, $args->{sentinels} ];
957 }
958  
959 10 my $key = $args->{reuse};
960 10 my $db = $args->{database};
961 10 ▶ if ( $key and my $cached = $reused_connections{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key and my $cached = $Web::Session::Store::Redis::reused_connections{$key}) 7 3 10
Condition100%3/3
$key and my $cached = $Web::Session::Store::Redis::reused_connections{$key}
!ll&&!rl&&r
217
MC/DC100%2/2
$key and my $cached = $Web::Session::Store::Redis::reused_connections{$key}
$key my $cached = $Web::Session::Store::Redis::reused_connections{$key}
Truth table100%3/3
A and B
A= $keyB= my $cached = $Web::Session::Store::Redis::reused_connections{$key}
ABresult
0X0
100
111
962  
963 # A reused connection stays on the database it selected (Redis.pm and
964 # Redis::Fast select it again when they reconnect): no SELECT round
965 # trip at each tie
966 7 my $sel = $selected{$key};
967 7 ▶ return $cached
Branch: 1 branch
BranchTrueFalseTotal
if not defined $db or $sel and $sel->[0] == builtin::refaddr($cached) and $sel->[1] eq $db 4 3 7
Condition66%6/9
not defined $db or $sel and $sel->[0] == builtin::refaddr($cached) and $sel->[1] eq $db
l!l&&r!l&&!r
133
$sel and $sel->[0] == builtin::refaddr($cached) and $sel->[1] eq $db
!ll&&!rl&&r
033
$sel and $sel->[0] == builtin::refaddr($cached)
!ll&&!rl&&r
006
MC/DC50%2/4
not defined $db or $sel and $sel->[0] == builtin::refaddr($cached) and $sel->[1] eq $db
not defined $db $sel $sel->[0] == builtin::refaddr($cached) $sel->[1] eq $db
Truth table60%3/5
not(A) or B and C and D
A= not defined $dbB= $selC= $sel->[0] == builtin::refaddr($cached)D= $sel->[1] eq $db
ABCDresult
1XXX1
01111
00XX0
010X0
01100
968 if (
969 !defined $db
970 or ( $sel
971 and $sel->[0] == Scalar::Util::refaddr($cached)
972 and $sel->[1] eq $db )
973 );
974 3 ▶ if ( eval { $cached->select($db); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $cached->select($db); 1 } }) 1 2 3
975 1 $selected{$key} = [ Scalar::Util::refaddr($cached), $db ];
976 1 return $cached;
977 }
978 2 ▶ die $@ unless ( $@ =~ $CONNECTION_ERROR );
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /$Web::Session::Store::Redis::CONNECTION_ERROR/ 0 2 2
979  
980 # Broken (see $CONNECTION_ERROR): replaced
981 2 delete $reused_connections{$key};
982 }
983 5 $redisObj = $redis->new( %{$args} );
984 5 ▶ $redisObj->select($db) if ( defined $db );
Branch: 1 branch
BranchTrueFalseTotal
if defined $db 5 0 5
985 4 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 2 2 4
986 2 $reused_connections{$key} = $redisObj;
987 2 ▶ if ( defined $db ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $db) { } 2 0 2
988 2 $selected{$key} = [ Scalar::Util::refaddr($redisObj), $db ];
989 }
990 else {
991 0 delete $selected{$key};
992 }
993 }
994 4 return $redisObj;
995 }
996  
997 10 1;
998 __END__
« Session/DBI.pm ↑ Summary Session/Store/Postgres.pm »