| 1 | package Web::Session::Store::Redis; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | our $VERSION = '1.1.0'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Key of the session object holding the result of update(), see "DESCRIPTION" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use constant STATUS => 'web_session_redis_update'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # Key of the session object holding the version read, see _writeMerged() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use constant READ => 'web_session_redis_read'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # Writes tried before a session updated concurrently is overwritten | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 9 | our $MAX_TRIES = 5; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 9 | our $redis; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 9 | our %reused_connections; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 9 | $redis = 'Redis::Fast'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | ▶ | eval 'use Redis::Fast'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | require Redis; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | $redis = 'Redis'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # Redis clients croak on characters above U+00FF: key names built from a field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # name or a value are sent as Latin-1 bytes (existing index sets keep their | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # name) or, if they hold such characters, as UTF-8 bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 126 | ▶ | sub keyName { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
keyName: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 126 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 126 | $name = "$name"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 126 | ▶ | utf8::encode($name) unless ( utf8::downgrade( $name, 1 ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 126 | return $name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 9 | our $HASH_INDEX_PREFIX = 'hidx:'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Index set of value $value of field $field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 51 | ▶ | sub indexName { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
indexName: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 51 | my ( $field, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 51 | return keyName("${field}_$value"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # Index hash of value $value of field $field, used with utimeOffset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 51 | ▶ | sub hashIndexName { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hashIndexName: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 51 | my ( $field, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 51 | return keyName("$HASH_INDEX_PREFIX${field}_$value"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # '00' is truthy: the numeric test disables it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 149 | ▶ | sub hashIndex { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hashIndex: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 149 | my ($args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 149 | ▶ | return ( $args->{utimeOffset} and $args->{utimeOffset} > 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$args->{'utimeOffset'} && $args->{'utimeOffset'} > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=32.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 5 | my ( $class, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 5 | my $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | my $offset = $session->{args}->{utimeOffset}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | die "utimeOffset must be a number of seconds\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $offset and length $offset and not $offset =~ /^[0-9]{1,10}\z/
defined $offset and length $offset
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | if ( defined $offset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | and length $offset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | and $offset !~ /^[0-9]{1,10}\z/ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $args = $session->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $self->{cache} = $class->_getRedis($args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | _checkHexpire( $self->{cache}, $args ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | if ( hashIndex($args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | and ( ref( $args->{Index} ) ? @{ $args->{Index} } : $args->{Index} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
hashIndex($args) and ref $args->{"Index"} ? @{$args->{"Index"};} : $args->{"Index"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # Result of the HEXPIRE probe per server: 1 if supported, otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # [ time of the next probe (undef: never), error to raise ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 9 | our %hexpire; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # Seconds before probing again a read-only replica | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 9 | our $READONLY_RETRY = 60; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # Fail at tie time: Apache::Session updates the session at untie, where an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # error is only a warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 17 | ▶ | sub _checkHexpire { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_checkHexpire: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 17 | my ( $redisObj, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | my $server = join ' ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 17 | ▶ | map { ref($_) ? @$_ : $_ // '' } @$args{qw(server sock service sentinels)}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$_ // ""
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 17 | ▶ | if ( my $known = $hexpire{$server} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 9 | ▶ | return unless ( ref $known ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 7 | my ( $retry, $die ) = @$known; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 7 | ▶ | if ( !defined $retry or time < $retry ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $retry or time < $retry
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 6 | ▶ | die $die if ($die); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # No index hash has this name. Errors other than a missing command (ACL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # read-only replica...) are left to the commands that will fail. So that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # each tie doesn't pay a failed round trip, a missing command and NOPERM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # are remembered until the process ends, READONLY for $READONLY_RETRY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # seconds only (a Sentinel failover may promote the replica). Other errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # may be transient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 9 | ▶ | unless ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | $redisObj->hexpire( $HASH_INDEX_PREFIX, 1, 'FIELDS', 1, 'x' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 6 | ( my $err = $@ ) =~ s/\n.*//s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 6 | ▶ | if ( $err =~ /unknown command/i ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | my $die = "utimeOffset needs Redis >= 7.4 or Valkey >= 9: $err\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | $hexpire{$server} = [ undef, $die ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | die $die; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | ▶ | if ( $err =~ /\bNOPERM\b/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | $hexpire{$server} = [undef]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | elsif ( $err =~ /\bREADONLY\b/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | $hexpire{$server} = [ time + $READONLY_RETRY ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | delete $hexpire{$server}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | $hexpire{$server} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # Expiration of a session in seconds, see "OPTIONS" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 98 | ▶ | sub _ttl { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ttl: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 98 | my ($session) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 98 | my $args = $session->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 98 | my $data = $session->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # Persistent sessions keep the _utime of their creation and never expire | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 98 | ▶ | if ( hashIndex($args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%11/11
hashIndex($args) and defined $data->{"_utime"} and $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/ and ($data->{"_session_kind"} // "") ne "Persistent"
hashIndex($args) and defined $data->{"_utime"} and $data->{"_utime"} =~ /^[0-9]+(?:\.[0-9]+)?\z/
hashIndex($args) and defined $data->{"_utime"}
$data->{"_session_kind"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | and defined $data->{_utime} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | and $data->{_utime} =~ /^[0-9]+(?:\.[0-9]+)?\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | and ( $data->{_session_kind} // '' ) ne 'Persistent' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 39 | my $ttl = int( $data->{_utime} + $args->{utimeOffset} - time ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # An expired session is still written: it may be read again before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | # the end of the request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 39 | ▶ | return $ttl > 0 ? $ttl : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 59 | return $args->{TTL}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 18 | ▶ | sub _set { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 18 | my ( $self, $session, @opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 18 | my $ttl = _ttl($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | return $self->{cache}->set( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | $session->{data}->{_session_id}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 18 | ▶ | $session->{serialized}, ( $ttl ? ( 'EX', $ttl ) : () ), @opts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # XX: another process may have deleted the session (logout, revocation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # purge) or Redis may have expired it since it was read; a plain SET would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # write it back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 16 | ▶ | sub _overwrite { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_overwrite: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 16 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 16 | ▶ | return 1 if ( defined $self->_set( $session, 'XX' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 5 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # With utimeOffset, the session and its index entries are written in one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # transaction. The entries of a session that expires are fields of an index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # hash that expire with it (HEXPIRE: Redis >= 7.4, Valkey >= 9; otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # Redis refuses the whole transaction); the others stay in index sets. $xx: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # overwrite an existing session only. Returns false if it doesn't exist, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # undef if EXEC was aborted by a WATCH of the caller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 34 | ▶ | sub _writeHashIndex { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_writeHashIndex: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 34 | my ( $self, $session, $index, $old, $xx ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 34 | my $r = $self->{cache}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 34 | my $ttl = _ttl($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 34 | my ( @cmds, $queueErr, $reply, $execErr ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # $fatal: an error of this command fails the write, like outside a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # transaction. Other errors are ignored (key of another type) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 165 | ▶ | my $queue = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 165 | my ( $cmd, $fatal, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 165 | push @cmds, [ $cmd, $fatal, @args ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | $r->$cmd( @args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 165 | ▶ | sub { $queueErr //= "[$cmd] $_[1]" if ( defined $_[1] ) } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition50%1/2
$queueErr //= "[$cmd] $_[1]"
MC/DC0%0/1
$queueErr //= "[$cmd] $_[1]"
$queueErr
Truth table50%1/2
A
A= $queueErr
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 34 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 34 | my $sent = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | $r->multi( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 34 | ▶ | sub { $queueErr //= "[multi] $_[1]" if ( defined $_[1] ) } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition50%1/2
$queueErr //= "[multi] $_[1]"
MC/DC0%0/1
$queueErr //= "[multi] $_[1]"
$queueErr
Truth table50%1/2
A
A= $queueErr
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 34 | $self->_queueHashIndex( $queue, $session, $index, $old, $ttl, $xx ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 33 | ▶ | $r->exec( sub { ( $reply, $execErr ) = @_ } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 33 | $r->wait_all_responses; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 33 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # A connection left in a transaction would queue the next commands | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 34 | ▶ | unless ($sent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 1 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | eval { $r->wait_all_responses }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | eval { $r->discard }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 33 | ▶ | if ( my $err = $queueErr // $execErr ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$queueErr // $execErr
MC/DC50%1/2
$queueErr // $execErr
$queueErr
$execErr
Truth table66%2/3
A // B
A= $queueErrB= $execErr
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 4 | ▶ | die "utimeOffset needs Redis >= 7.4 or Valkey >= 9: $err\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | if ( $err =~ /unknown command/i ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 3 | die "$err\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 29 | ▶ | return undef unless ( defined $reply ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | die "[exec] unexpected reply\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | unless ( ref($reply) eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | and @$reply == @cmds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 22 | ▶ | and !grep { ref($_) ne 'ARRAY' } @$reply ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
ref $reply eq "ARRAY" and @$reply == @cmds and not grep({ref $_ ne "ARRAY";} @$reply)
ref $reply eq "ARRAY" and @$reply == @cmds
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 18 | foreach ( 0 .. $#cmds ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 83 | my $err = $reply->[$_]->[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 83 | ▶ | die "[$cmds[$_]->[0]] $err\n" if ( defined $err and $cmds[$_]->[1] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $err and $cmds[$_][1]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 17 | ▶ | return 1 if ( defined $reply->[0]->[0] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # SET XX refused: the entries added in the transaction are orphans | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | foreach ( grep { $_->[0] =~ /^(?:hset|sadd)\z/ } @cmds ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 1 | my ( $cmd, undef, $name, $id ) = @$_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | ▶ | $cmd eq 'hset' ? $r->hdel( $name, $id ) : $r->srem( $name, $id ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 34 | ▶ | sub _queueHashIndex { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_queueHashIndex: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 34 | my ( $self, $queue, $session, $index, $old, $ttl, $xx ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 34 | my $data = $session->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 34 | my $id = $data->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | $queue->( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 'set', 1, $id, $session->{serialized}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 34 | ▶ | ( $ttl ? ( 'EX', $ttl ) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | ( $xx ? 'XX' : () ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 34 | foreach my $i (@$index) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 34 | ▶ | my $old_val = $old ? $old->{$i} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 34 | my $new_val = $data->{$i}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 34 | ▶ | my $add = ( defined($new_val) and length($new_val) > 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $new_val && length $new_val > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 34 | ▶ | if ( defined($old_val) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
defined $old_val and length $old_val > 0 and !$add || $old_val ne $new_val
defined $old_val and length $old_val > 0
not $add or $old_val ne $new_val
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | and length($old_val) > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | and ( !$add or $old_val ne $new_val ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 17 | $queue->( 'srem', 0, indexName( $i, $old_val ), $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 17 | $queue->( 'hdel', 0, hashIndexName( $i, $old_val ), $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 34 | ▶ | next unless ($add); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 33 | my $set = indexName( $i, $new_val ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 33 | my $hash = hashIndexName( $i, $new_val ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 33 | ▶ | if ($ttl) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # The set holds the sessions stored before utimeOffset was set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 32 | $queue->( 'srem', 0, $set, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 32 | $queue->( 'hset', 1, $hash, $id, 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 31 | $queue->( 'hexpire', 1, $hash, $ttl, 'FIELDS', 1, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 1 | $queue->( 'hdel', 0, $hash, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 1 | $queue->( 'sadd', 1, $set, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 15 | ▶ | sub insert { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: called CC=8 SCAR=25.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 15 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | my $index = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | ref( $session->{args}->{Index} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | ? $session->{args}->{Index} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 15 | ▶ | : [ split /\s+/, $session->{args}->{Index} ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 15 | ▶ | if ( @$index and hashIndex( $session->{args} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@$index and hashIndex($session->{"args"})
MC/DC50%1/2
@$index and hashIndex($session->{"args"})
@$index
hashIndex($session->{"args"})
Truth table66%2/3
A and B
A= @$indexB= hashIndex($session->{"args"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 13 | ▶ | $self->_writeHashIndex( $session, $index ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_writeHashIndex($session, $index) // die("[exec] unexpected reply\n")
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | // die "[exec] unexpected reply\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 2 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 2 | $self->_set($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 2 | foreach my $i (@$index) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | my $t = $session->{data}->{$i}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | next unless ( defined($t) and ( length($t) > 0 ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $t and length $t > 0
MC/DC0%0/2
defined $t and length $t > 0
defined $t
length $t > 0
Truth table0%0/3
A and B
A= defined $tB= length $t > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | $self->{cache}->sadd( keyName("${i}_$t"), $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 7 | $session->{ +READ } = $session->{serialized}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 7 | $session->{ +STATUS } = 'written'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 7 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # Dies if the session was deleted meanwhile: a wrapper that caches it (such | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # as the local cache of Lemonldap::NG) must not keep it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 46 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 46 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 46 | delete $session->{ +STATUS }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 46 | $session->{ +STATUS } = $self->_update($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | die "Session '$session->{data}->{_session_id}' was deleted or expired, " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | . "not stored again\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 41 | ▶ | if ( $session->{ +STATUS } eq 'deleted' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 30 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # Returns the status of the update, see "DESCRIPTION" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 46 | ▶ | sub _update { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update: called CC=20 SCAR=30.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 46 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | my $index = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | ref( $session->{args}->{Index} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | ? $session->{args}->{Index} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 46 | ▶ | : [ split /\s+/, $session->{args}->{Index} ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | return $self->_updateHashIndex( $session, $index ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 46 | ▶ | if ( @$index and hashIndex( $session->{args} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$index and hashIndex($session->{"args"})
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"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 30 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 30 | my $old_data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 30 | my $status = 'overwritten'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 30 | ▶ | if ( defined $session->{ +READ } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 21 | ( $status, my $old_raw ) = $self->_writeMerged( $session, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 19 | ▶ | return $status if ( $status eq 'deleted' or !@$index ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$status eq "deleted" or not @$index
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 8 | $old_data = _decodeOld( $session, $old_raw ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # The version read is unknown (unserialize isn't the one installed by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # Web::Session::Redis): overwrite. Nothing to clean up without index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 9 | ▶ | unless (@$index) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 5 | ▶ | return $self->_overwrite($session) ? $status : 'deleted'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | # Read old data to clean up stale index entries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 4 | my $old_raw = eval { $self->{cache}->get($id) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 4 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | warn "Failed to read previous session '$id' from Redis: $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 4 | $old_data = _decodeOld( $session, $old_raw ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 4 | ▶ | return 'deleted' unless ( $self->_overwrite($session) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 10 | foreach my $i (@$index) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 10 | ▶ | my $old_val = defined($old_data) ? $old_data->{$i} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 10 | my $new_val = $session->{data}->{$i}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | # Remove old index entry if value changed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 10 | ▶ | if ( defined($old_val) && length($old_val) > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $old_val and length $old_val > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 7 | ▶ | if ( !defined($new_val) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not defined $new_val or length $new_val == 0 or $old_val ne $new_val
not defined $new_val or length $new_val == 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | || length($new_val) == 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | || $old_val ne $new_val ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 4 | eval { $self->{cache}->srem( keyName("${i}_$old_val"), $id ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | # Add new index entry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 10 | ▶ | if ( defined($new_val) && length($new_val) > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $new_val and length $new_val > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 8 | $self->{cache}->sadd( keyName("${i}_$new_val"), $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 10 | return $status; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # Decoded like the session itself: Redis returns Latin-1, not UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 61 | ▶ | sub _decode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 61 | my ( $session, $raw ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 61 | my $tmp = { serialized => $raw }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 61 | $session->{unserialize}->($tmp); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 61 | return $tmp->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | # Previous version, whose index entries are replaced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 33 | ▶ | sub _decodeOld { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decodeOld: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 33 | my ( $session, $raw ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 33 | ▶ | return undef unless ( defined $raw and length $raw ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $raw and length $raw
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 31 | my $data = eval { _decode( $session, $raw ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 31 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | warn "Failed to decode previous session '$id': $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 31 | return $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | # Sessions aren't locked (see "DESCRIPTION"): a write is accepted only if Redis | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | # still holds the version this process read, otherwise the caller's changes are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | # merged into the stored version and the write is tried again. Returns the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | # status of the update and the version replaced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 21 | ▶ | sub _writeMerged { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_writeMerged: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 21 | my ( $self, $session, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 21 | my $expected = $session->{ +READ }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 21 | my $merge = _merger( $session, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 21 | my ( $merged, $failed ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 21 | for ( 1 .. $MAX_TRIES ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | my ( $res, $cur ) = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 34 | @{ $self->_compareAndSet( $id, $expected, $session->{serialized}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 34 | _ttl($session) ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 32 | ▶ | if ( $res eq 'written' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 8 | $session->{ +READ } = $session->{serialized}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 8 | ▶ | return ( $merged ? 'merged' : 'written', $expected ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 24 | ▶ | return ('deleted') if ( $res eq 'deleted' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 20 | ▶ | next if ( $res eq 'aborted' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 19 | ▶ | if ( $res eq 'refused' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 5 | _warnUnchecked($cur); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 5 | $failed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 5 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 14 | $expected = $cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 14 | ▶ | unless ( $merge->($expected) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | $failed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 14 | $merged = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 7 | ▶ | _warnOverwrite($id) unless ($failed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 7 | ▶ | return ('deleted') unless ( $self->_overwrite($session) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 6 | $session->{ +READ } = $session->{serialized}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 6 | return ( 'overwritten', $expected ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | # Same as _writeMerged() with index hashes: the version check is part of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | # transaction of _writeHashIndex(), so that the index entries replaced are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # those of the version replaced. WATCH aborts EXEC if the session changes after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | # GET; if WATCH is refused, the transaction is run without it. Without the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | # version read, the session is overwritten. Returns the status of the update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 16 | ▶ | sub _updateHashIndex { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_updateHashIndex: called CC=16 SCAR=27.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 16 | my ( $self, $session, $index ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 16 | my $r = $self->{cache}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 16 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 16 | my $expected = $session->{ +READ }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 16 | ▶ | my $status = defined($expected) ? 'written' : 'overwritten'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 16 | my $merge = _merger( $session, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 16 | my ( $cur, $done, $failed ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 16 | for ( 1 .. $MAX_TRIES ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 28 | ▶ | unless ( eval { $r->watch($id); 1 } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 2 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 2 | ▶ | die $err unless ( _refused($err) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 2 | _warnUnchecked($err); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 2 | $failed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 2 | $cur = $r->get($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 2 | ▶ | $done = 0 unless ( defined $cur ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 26 | $cur = $r->get($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 25 | ▶ | unless ( defined $cur ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 1 | $r->unwatch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 1 | $done = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 24 | ▶ | if ( defined $expected and $cur ne $expected ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $expected and $cur ne $expected
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 7 | $r->unwatch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 7 | $expected = $cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 7 | $status = 'merged'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 7 | ▶ | next if ( $merge->($cur) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | $failed = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | # undef: the session changed after GET, read it again | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 17 | $done = $self->_writeHashIndex( $session, $index, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | _decodeOld( $session, $cur ), 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 16 | ▶ | last if ( defined $done ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 14 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 16 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 2 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 2 | eval { $r->unwatch }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 2 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | # Not watched: the index entries replaced are those of the last version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | # read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 14 | ▶ | unless ( defined $done ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 4 | ▶ | _warnOverwrite($id) unless ($failed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 4 | $status = 'overwritten'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 4 | ▶ | $done = $self->_writeHashIndex( $session, $index, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_writeHashIndex($session, $index, _decodeOld($session, $cur), 1) // die("[exec] unexpected reply\n")
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | _decodeOld( $session, $cur ), 1 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | // die "[exec] unexpected reply\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 13 | ▶ | return 'deleted' unless ($done); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 11 | $session->{ +READ } = $session->{serialized}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 11 | return $status; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | # Refusals that a retry won't change: ACL, command renamed or not supported by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | # a proxy. Connection errors are not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 9 | ▶ | sub _refused { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refused: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | || $_[0] =~ /unsupported|forbid/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 9 | our $unchecked_warned; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 7 | ▶ | sub _warnUnchecked { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_warnUnchecked: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 7 | ▶ | return if ($unchecked_warned); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 5 | $unchecked_warned = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | # First line only: Redis::Fast adds a backtrace holding the arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 5 | my ($err) = split /\n/, $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 5 | warn "Redis refused WATCH/MULTI, concurrent updates of sessions are not " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | . "merged: $err\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 4 | ▶ | sub _warnOverwrite { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_warnOverwrite: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 4 | warn "Session '$_[0]' was updated concurrently too often, overwriting it\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | # Returns a function that applies to the version stored, its argument, what | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | # the caller changed since the version it read, and serializes the result in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | # $session. It returns false, with a warning, if they can't be merged | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 37 | ▶ | sub _merger { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merger: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 37 | my ( $session, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 37 | my ( $orig, $new ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 21 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 21 | my ($cur) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 21 | my $merged = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 21 | ▶ | $orig //= _decode( $session, $session->{ +READ } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$orig //= _decode($session, $session->{"web_session_redis_read"})
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"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 21 | ▶ | $new //= $session->{data}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$new //= $session->{"data"}
MC/DC0%0/2
$new //= $session->{"data"}
$new
$session->{"data"}
Truth table66%2/3
A //= B
A= $newB= $session->{"data"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 21 | _merge( $orig, _decode( $session, $cur ), $new, $id ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | 21 | ▶ | unless ( ref($merged) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | ▶ | warn "Unable to merge session '$id' with a concurrent update, " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || "not a session\n"
MC/DC0%0/1
$@ || "not a session\n"
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | . "overwriting it: " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | . ( $@ || "not a session\n" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 21 | $session->{data} = $merged; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 21 | $session->{serialize}->($session); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 21 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 37 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | # Three-way merge of top-level keys: what the caller changed or deleted since | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | # $orig is applied to $cur | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 31 | ▶ | sub _merge { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merge: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 31 | my ( $orig, $cur, $new, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | 31 | my %res = %$cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 31 | foreach my $k ( keys %$orig ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 120 | ▶ | delete $res{$k} unless ( exists $new->{$k} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 31 | foreach my $k ( keys %$new ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | $res{$k} = $new->{$k} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 122 | ▶ | unless ( exists $orig->{$k} and _same( $orig->{$k}, $new->{$k} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $orig->{$k} and _same($orig->{$k}, $new->{$k})
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})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 31 | $res{_session_id} = $id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 31 | return \%res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 9 | my $cmpJson = JSON->new->canonical->allow_nonref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | # A number used as a string is still the same value: scalars are compared | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | # with eq, references by their canonical JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 119 | ▶ | sub _same { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_same: called CC=8 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 119 | my ( $x, $y ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 119 | ▶ | return !defined($y) unless ( defined $x ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 119 | ▶ | return 0 unless ( defined $y ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 119 | ▶ | return $x eq $y unless ( ref($x) or ref($y) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $x or ref $y
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 10 | ▶ | return 0 unless ( ref($x) and ref($y) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $x and ref $y
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 10 | ▶ | return eval { $cmpJson->encode($x) eq $cmpJson->encode($y) } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | # KEYS[1]: session, ARGV: version read, new version, TTL or '' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 9 | our $CAS = q{local cur = redis.call('get', KEYS[1]) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | if not cur then return {'deleted'} end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | if cur ~= ARGV[1] then return {'conflict', cur} end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | if ARGV[3] == '' then redis.call('set', KEYS[1], ARGV[2], 'XX') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | else redis.call('set', KEYS[1], ARGV[2], 'EX', ARGV[3], 'XX') end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | return {'written'}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 9 | our $lua_warned; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | # Write $serialized if $id still holds $expected. Returns ['written'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | # ['deleted'], ['conflict', current version], ['aborted'] or ['refused', error]. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | # Without Lua (EVAL forbidden by ACL, proxy...), WATCH the session so that a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | # concurrent write aborts EXEC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 34 | ▶ | sub _compareAndSet { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compareAndSet: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 34 | my ( $self, $id, $expected, $serialized, $ttl ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 34 | my $redisObj = $self->{cache}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | 34 | my $res = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 34 | ▶ | $redisObj->eval( $CAS, 1, $id, $expected, $serialized, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | ( $ttl ? $ttl : '' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 34 | ▶ | return $res if ( ref($res) eq 'ARRAY' and !$@ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $res eq "ARRAY" and not $@
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 $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 13 | ▶ | unless ($lua_warned) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 10 | $lua_warned = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | # First line only: Redis::Fast adds a backtrace holding the arguments, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | # that is the session id and data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 10 | ▶ | my ($err) = split /\n/, ( $@ || 'unexpected reply' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'unexpected reply'
MC/DC0%0/1
$@ || 'unexpected reply'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 10 | print STDERR "Redis EVAL failed, sessions are updated without Lua: " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | . "$err\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 13 | $redisObj->watch($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 8 | my $cur = $redisObj->get($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 7 | ▶ | if ( !defined $cur or $cur ne $expected ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $cur or $cur ne $expected
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | 2 | $redisObj->unwatch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 2 | ▶ | $res = defined($cur) ? [ 'conflict', $cur ] : ['deleted']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 5 | $redisObj->multi; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 4 | ▶ | $ttl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | ? $redisObj->set( $id, $serialized, 'EX', $ttl, 'XX' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | : $redisObj->set( $id, $serialized, 'XX' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 4 | my $done = $redisObj->exec; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | # undef: the session changed after GET | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 4 | ▶ | $res = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | !$done ? ['aborted'] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | : defined( $done->[0] ) ? ['written'] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | : ['deleted']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 13 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 7 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 7 | eval { $redisObj->discard }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 7 | eval { $redisObj->unwatch }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 7 | ▶ | return [ 'refused', $err ] if ( _refused($err) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 2 | die $err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 6 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | ▶ | sub materialize { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
materialize: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 0 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | $session->{serialized} = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | $self->{cache}->get( $session->{data}->{_session_id} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | ▶ | or die 'Object does not exist in data store.'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 1 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 1 | my ( $self, $session ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | my $index = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | ref( $session->{args}->{Index} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | ? $session->{args}->{Index} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 1 | ▶ | : [ split /\s+/, $session->{args}->{Index} ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 1 | my $id = $session->{data}->{_session_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 1 | my $hash = hashIndex( $session->{args} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 1 | foreach my $i (@$index) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 1 | my $t; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 1 | ▶ | next unless ( $t = $session->{data}->{$i} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 1 | eval { $self->{cache}->srem( indexName( $i, $t ), $id ); }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 1 | ▶ | eval { $self->{cache}->hdel( hashIndexName( $i, $t ), $id ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | if ($hash); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | 1 | $self->{cache}->del($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 0 | ▶ | sub _getRedis { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_getRedis: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 0 | my $args = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | 0 | my $redisObj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | # Manage undef encoding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | $args->{encoding} = undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | if ( $args->{encoding} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 0 | ▶ | and $args->{encoding} eq "undef" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"encoding"} and $args->{"encoding"} eq "undef"
MC/DC0%0/2
$args->{"encoding"} and $args->{"encoding"} eq "undef"
$args->{"encoding"}
$args->{"encoding"} eq "undef"
Truth table0%0/3
A and B
A= $args->{"encoding"}B= $args->{"encoding"} eq "undef"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | # If sentinels is not given as an array ref, try to parse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | # a comma delimited list instead | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | 0 | ▶ | if ( $args->{sentinels} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"sentinels"} and ref $args->{"sentinels"} ne "ARRAY"
MC/DC0%0/2
$args->{"sentinels"} and ref $args->{"sentinels"} ne "ARRAY"
$args->{"sentinels"}
ref $args->{"sentinels"} ne "ARRAY"
Truth table0%0/3
A and B
A= $args->{"sentinels"}B= ref $args->{"sentinels"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | and ref $args->{sentinels} ne 'ARRAY' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | $args->{sentinels} = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | 0 | [ split /[,\s]+/, $args->{sentinels} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 0 | ▶ | if ( $args->{reuse} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"reuse"} and $Web::Session::Store::Redis::reused_connections{$args->{"reuse"}}
MC/DC0%0/2
$args->{"reuse"} and $Web::Session::Store::Redis::reused_connections{$args->{"reuse"}}
$args->{"reuse"}
$Web::Session::Store::Redis::reused_connections{$args->{"reuse"}}
Truth table0%0/3
A and B
A= $args->{"reuse"}B= $Web::Session::Store::Redis::reused_connections{$args->{"reuse"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | and $reused_connections{ $args->{reuse} } ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 0 | $redisObj = $reused_connections{ $args->{reuse} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 0 | $redisObj = $redis->new( %{$args} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 0 | ▶ | if ( $args->{reuse} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 0 | $reused_connections{ $args->{reuse} } = $redisObj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | # Manage database | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | $redisObj->select( $args->{database} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 0 | ▶ | if defined $args->{database}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 0 | return $redisObj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||