Session/Store/Postgres.pm

filter: Statementstmt 95.1% 175 / 184 Branchbran 83.7% 67 / 80 Conditioncond 71.9% 64 / 89 MC/DCmcdc 42.5% 20 / 47 Subroutinesub 96.3% 26 / 27 Podpod 0.0% 0 / 4 totaltotal 81.6% 352 / 431 CC 78 SCAR 21.7
CC
78
Cov
87%
_dropIdle
30.0
_giveBack
16.9
_borrow
13.3
CRAP 8.7
« Session/PgJSON.pm ↑ Summary Session/MariaDBJSON.pm »
1 package Web::Session::Store::Postgres;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Web::Session::Store::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Apache::Session::Store::Postgres;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 our @ISA =
11 qw(Web::Session::Store::DBI Apache::Session::Store::Postgres);
12 12 our $VERSION = '1.0.0';
13  
14 # A pool of connections, per process and per data source (DataSource +
15 # UserName), lent to one store at a time: two sessions tied together (a
16 # session and its persistent session) never share a connection, so never a
17 # transaction. With the connection reuse of the other stores
18 # (connect_cached(), see _connection() in Store/DBI.pm), they share the
19 # handle and its transaction: untying one commits or rolls back what the
20 # other has done so far.
21 #
22 # So the pool is the default connection reuse of this store when writes are
23 # committed (see _commit()), noreuse isn't set and no Handle is given.
24 # KeepConnections => 0 gives back the connect_cached() handle of the other
25 # stores. With Commit => 0, writes must not be committed (AutoCommit off,
26 # rolled back at untie), which the pool, where each statement commits
27 # itself, can't do: connect_cached() is kept.
28 #
29 # In the pool, each statement of this store is its own transaction
30 # (AutoCommit): it reads without a lock (Apache::Session::Store::DBI's
31 # materialize, no FOR UPDATE; the lock manager is Null), and a tie writes once
32 # (see insert()), so a transaction around a tie protected nothing and cost a
33 # BEGIN and a COMMIT round trip each. The Patroni store, whose materialize() locks the row, opens a
34 # transaction per tie on its pooled connection; _giveBack() never keeps a
35 # connection inside a transaction.
36 #
37 # With the same arguments, the class methods of Web::Session::PgJSON borrow
38 # a connection of this pool for the duration of each call (see _lend()), so
39 # that a process using both sessions and class methods doesn't keep one more
40 # connection for the latter.
41 # Times the idle connections of a data source were dropped by _dropIdle():
42 # a connection lent before the last drop is closed when given back
43 12 our %DROPS; # "datasource\0user" => count
44 12 our %IDLE; # "datasource\0user" => [ [ $dbh, $since ], ... ]
45 12 my %PENDING; # refaddr => store with a pending INSERT (weak), see END
46  
47 # Defaults of the MaxIdle, IdleTimeout and MaxLifetime arguments
48 12 our $MAX_IDLE = 4; # idle connections kept per data source
49 12 our $IDLE_TIMEOUT = 120; # seconds: an idle connection older is closed
50 12 our $MAX_LIFETIME = 3600; # seconds: an older connection isn't kept
51  
52 # An idle connection given back less than $PING_AFTER seconds ago is lent
53 # without a round trip if its socket has nothing to read (see _usable())
54 12 our $PING_AFTER = 30;
55  
56 # Integer argument $name of $args, or $default
57 309 ▶sub _intArg {
Subroutine _intArg: called CC=3 SCAR=11.0
58 309 my ( $args, $name, $default ) = @_;
59 309 my $v = $args->{$name};
60 309 ▶ return ( defined($v) and $v =~ /^\s*([0-9]+)\s*\z/ ) ? $1 : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v =~ /^\s*([0-9]+)\s*\z/ ? : 6 303 309
Condition66%2/3
defined $v and $v =~ /^\s*([0-9]+)\s*\z/
!ll&&!rl&&r
30306
MC/DC50%1/2
defined $v and $v =~ /^\s*([0-9]+)\s*\z/
defined $v $v =~ /^\s*([0-9]+)\s*\z/
Truth table66%2/3
A and B
A= defined $vB= $v =~ /^\s*([0-9]+)\s*\z/
ABresult
0X0
100
111
61 }
62  
63 # Whether an idle handle can be lent without a round trip: one given back
64 # lately, its statements done, whose socket has nothing to read, is alive (a
65 # server that goes away closes it, which makes it readable). One idle for
66 # longer, or whose socket is readable or unknown, is pinged.
67 29 ▶sub _usable {
Subroutine _usable: called CC=8 SCAR=20.9
68 29 my ( $handle, $since ) = @_;
69 29 ▶ return 0 unless $handle->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $handle->{"Active"} 0 29 29
70 29 my $fd = eval { $handle->{pg_socket} };
71 29 ▶ if ( time - $since < $PING_AFTER and defined $fd and $fd >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $since < $Web::Session::Store::Postgres::PING_AFTER and defined $fd and $fd >= 0) 2 27 29
Condition83%5/6
time - $since < $Web::Session::Store::Postgres::PING_AFTER and defined $fd and $fd >= 0
!ll&&!rl&&r
2702
time - $since < $Web::Session::Store::Postgres::PING_AFTER and defined $fd
!ll&&!rl&&r
2252
MC/DC66%2/3
time - $since < $Web::Session::Store::Postgres::PING_AFTER and defined $fd and $fd >= 0
time - $since < $Web::Session::Store::Postgres::PING_AFTER defined $fd $fd >= 0
Truth table75%3/4
A and B and C
A= time - $since < $Web::Session::Store::Postgres::PING_AFTERB= defined $fdC= $fd >= 0
ABCresult
0XX0
10X0
1100
1111
72 2 my $rin = '';
73 2 vec( $rin, $fd, 1 ) = 1;
74  
75 # The descriptor number only: no Perl handle opened, none closed
76 2 my $ready = select( my $rout = $rin, undef, undef, 0 );
77 2 ▶ return 1 if ( defined $ready and $ready == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $ready and $ready == 0 1 1 2
Condition66%2/3
defined $ready and $ready == 0
!ll&&!rl&&r
011
MC/DC50%1/2
defined $ready and $ready == 0
defined $ready $ready == 0
Truth table66%2/3
A and B
A= defined $readyB= $ready == 0
ABresult
0X0
100
111
78 }
79 28 ▶ return eval { $handle->ping } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $handle->ping } } ? : 27 1 28
80 }
81  
82 # Whether an idle connection given back at $since is expired
83 46 ▶sub _expired {
Subroutine _expired: called CC=2 SCAR=7.1
84 46 my ( $handle, $since, $now, $idleTimeout, $maxLifetime ) = @_;
85 return ( $now - $since > $idleTimeout
86 46 ▶ or $now - $handle->{private_ws_born} > $maxLifetime );
Condition66%2/3
$now - $since > $idleTimeout || $now - $handle->{'private_ws_born'} > $maxLifetime
l!l&&r!l&&!r
2044
MC/DC50%1/2
$now - $since > $idleTimeout || $now - $handle->{'private_ws_born'} > $maxLifetime
$now - $since > $idleTimeout $now - $handle->{'private_ws_born'} > $maxLifetime
Truth table66%2/3
A || B
A= $now - $since > $idleTimeoutB= $now - $handle->{'private_ws_born'} > $maxLifetime
ABresult
1X1
011
000
87 }
88  
89 # HandleError of pooled connections (inherited by their statement handles):
90 # a connection that met an error is closed when given back, never lent again.
91 # For example, after a failover behind a virtual IP, a connection to the
92 # former primary still answers ping() but every write fails. Returns false:
93 # RaiseError still applies.
94 9 ▶sub _flagError {
Subroutine _flagError: called CC=4 SCAR=14.2
95 9 my ( $msg, $h ) = @_;
96 9 ▶ my $dbh = ( $h->{Type} // '' ) eq 'st' ? $h->{Database} : $h;
Branch: 1 branch
BranchTrueFalseTotal
($h->{'Type'} // '') eq 'st' ? : 8 1 9
Condition50%1/2
$h->{"Type"} // ""
l!l
90
MC/DC0%0/1
$h->{"Type"} // ""
$h->{"Type"}
Truth table50%1/2
A
A= $h->{"Type"}
Aresult
11
00
97 9 ▶ $dbh->{private_ws_error} = 1 if ($dbh);
Branch: 1 branch
BranchTrueFalseTotal
if $dbh 9 0 9
98 9 return 0;
99 }
100  
101 # Commit policy of a session. Without a Handle, writes are committed unless
102 # Commit is explicitly false: a missing or empty Commit keeps the default (as
103 # the Patroni store does), else a session written without it would be rolled
104 # back at untie. With a Handle, the caller owns the transaction: Commit is
105 # used as given, as in Apache::Session::Store::Postgres
106 154 ▶sub _commit {
Subroutine _commit: called CC=4 SCAR=13.9
107 154 my ($args) = @_;
108 154 ▶ return $args->{Commit} if ( exists $args->{Handle} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"Handle"} 8 146 154
109 146 my $commit = $args->{Commit};
110 146 ▶ return ( defined $commit and $commit ne '' ) ? $commit : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $commit && $commit ne '' ? : 139 7 146
Condition100%3/3
defined $commit and $commit ne ""
!ll&&!rl&&r
61139
MC/DC100%2/2
defined $commit and $commit ne ""
defined $commit $commit ne ""
Truth table100%3/3
A and B
A= defined $commitB= $commit ne ""
ABresult
0X0
100
111
111 }
112  
113 # Whether the pool applies to these arguments: KeepConnections not disabled,
114 # writes committed, no noreuse, no Handle. $commit is the commit policy of
115 # the store, _commit($args) by default (the Patroni store has its own, see
116 # Store/Patroni.pm). Shared by connection(), by the Patroni store and by the
117 # class methods of Web::Session::PgJSON (see _lend()), so that they stay
118 # consistent
119 142 ▶sub _poolApplies {
Subroutine _poolApplies: called CC=8 SCAR=20.8
120 142 my ( $args, $commit ) = @_;
121 142 ▶ $commit = _commit($args) if ( @_ < 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 131 11 142
122  
123 # KeepConnections is on unless explicitly disabled: a missing or empty
124 # value (configuration interfaces may store an empty option as '') keeps
125 # the default
126 142 my $keep = $args->{KeepConnections};
127 142 ▶ $keep = 1 unless ( defined $keep and $keep ne '' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keep and $keep ne "" 119 23 142
Condition100%3/3
defined $keep and $keep ne ""
!ll&&!rl&&r
117223
MC/DC100%2/2
defined $keep and $keep ne ""
defined $keep $keep ne ""
Truth table100%3/3
A and B
A= defined $keepB= $keep ne ""
ABresult
0X0
100
111
128 return ( $keep
129 and $commit
130 and !$args->{noreuse}
131 142 ▶ and !exists $args->{Handle} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$keep && $commit && !$args->{'noreuse'} && !exists $args->{'Handle'} ? : 103 39 142
Condition100%9/9
$keep and $commit and not $args->{"noreuse"} and not exists $args->{"Handle"}
!ll&&!rl&&r
354103
$keep and $commit and not $args->{"noreuse"}
!ll&&!rl&&r
314107
$keep and $commit
!ll&&!rl&&r
229111
MC/DC100%4/4
$keep and $commit and not $args->{"noreuse"} and not exists $args->{"Handle"}
$keep $commit not $args->{"noreuse"} not exists $args->{"Handle"}
Truth table100%5/5
A and B and not(C) and not(D)
A= $keepB= $commitC= not $args->{"noreuse"}D= not exists $args->{"Handle"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
132 }
133  
134 # Key of the idle connections of a data source in %IDLE
135 103 ▶sub _poolKey {
Subroutine _poolKey: called CC=3 SCAR=11.1
136 103 my ( $datasource, $username ) = @_;
137 103 ▶ return join "\0", $datasource // '', $username // '';
Condition75%3/4
$datasource // ""
l!l
1030
$username // ""
l!l
976
MC/DC0%0/1
$datasource // ""
$datasource
MC/DC100%1/1
$username // ""
$username
Truth table50%1/2
A
A= $datasource
Aresult
11
00
Truth table100%2/2
A
A= $username
Aresult
11
00
138 }
139  
140 # Takes a connection to $datasource as $username of the pool, an idle one if
141 # possible, a new one otherwise. Without $datasource, connects to the data
142 # source of $args; $args also gives the MaxIdle, IdleTimeout and MaxLifetime
143 # options. Lent means removed from the idle ones: a connection is never lent
144 # twice at once. Returns the handle and what _giveBack() needs:
145 # { dbh, pool_key, pool_maxidle, pool_idle, pool_maxlife }
146 103 ▶sub _borrow {
Subroutine _borrow: called CC=13 SCAR=25.9
147 103 my ( $args, $datasource, $username, $password ) = @_;
148 103 ▶ if ( @_ < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 100 3 103
149 $datasource = $args->{DataSource}
150 100 ▶ || $Apache::Session::Store::Postgres::DataSource;
Condition33%1/3
$args->{'DataSource'} || $Apache::Session::Store::Postgres::DataSource
l!l&&r!l&&!r
10000
MC/DC0%0/2
$args->{'DataSource'} || $Apache::Session::Store::Postgres::DataSource
$args->{'DataSource'} $Apache::Session::Store::Postgres::DataSource
Truth table33%1/3
A || B
A= $args->{'DataSource'}B= $Apache::Session::Store::Postgres::DataSource
ABresult
1X1
011
000
151 $username = $args->{UserName}
152 100 ▶ || $Apache::Session::Store::Postgres::UserName;
Condition66%2/3
$args->{'UserName'} || $Apache::Session::Store::Postgres::UserName
l!l&&r!l&&!r
9703
MC/DC50%1/2
$args->{'UserName'} || $Apache::Session::Store::Postgres::UserName
$args->{'UserName'} $Apache::Session::Store::Postgres::UserName
Truth table66%2/3
A || B
A= $args->{'UserName'}B= $Apache::Session::Store::Postgres::UserName
ABresult
1X1
011
000
153 $password = $args->{Password}
154 100 ▶ || $Apache::Session::Store::Postgres::Password;
Condition33%1/3
$args->{'Password'} || $Apache::Session::Store::Postgres::Password
l!l&&r!l&&!r
00100
MC/DC0%0/2
$args->{'Password'} || $Apache::Session::Store::Postgres::Password
$args->{'Password'} $Apache::Session::Store::Postgres::Password
Truth table33%1/3
A || B
A= $args->{'Password'}B= $Apache::Session::Store::Postgres::Password
ABresult
1X1
011
000
155 }
156 103 my $key = _poolKey( $datasource, $username );
157 103 my $idleTimeout = _intArg( $args, 'IdleTimeout', $IDLE_TIMEOUT );
158 103 my $maxLifetime = _intArg( $args, 'MaxLifetime', $MAX_LIFETIME );
159 103 my $now = time;
160 103 my $dbh;
161 103 ▶ while ( my $idle = pop @{ $IDLE{$key} ||= [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $idle = pop @{$Web::Session::Store::Postgres::IDLE{$key} ||= [];}) 31 75 106
Condition100%2/2
$Web::Session::Store::Postgres::IDLE{$key} ||= []
l!l
4858
MC/DC100%1/1
$Web::Session::Store::Postgres::IDLE{$key} ||= []
$Web::Session::Store::Postgres::IDLE{$key}
Truth table100%2/2
A
A= $Web::Session::Store::Postgres::IDLE{$key}
Aresult
11
00
162 31 my ( $handle, $since ) = @$idle;
163  
164 # Inherited from the parent before a fork: its socket is the parent's.
165 # AutoInactiveDestroy keeps this process from closing it
166 31 ▶ next if ( $handle->{private_ws_pid} != $$ );
Branch: 1 branch
BranchTrueFalseTotal
if $handle->{"private_ws_pid"} != $$ 1 30 31
167  
168 # Idle for too long, a NAT or load balancer may have dropped it: a
169 # ping could then wait for the TCP timeout. Closed without a ping
170 30 ▶ if ( _expired( $handle, $since, $now, $idleTimeout, $maxLifetime )
Branch: 1 branch
BranchTrueFalseTotal
if (_expired($handle, $since, $now, $idleTimeout, $maxLifetime) or not _usable($handle, $since)) 2 28 30
Condition100%3/3
_expired($handle, $since, $now, $idleTimeout, $maxLifetime) or not _usable($handle, $since)
l!l&&r!l&&!r
1128
MC/DC100%2/2
_expired($handle, $since, $now, $idleTimeout, $maxLifetime) or not _usable($handle, $since)
_expired($handle, $since, $now, $idleTimeout, $maxLifetime) not _usable($handle, $since)
Truth table100%3/3
A or not(B)
A= _expired($handle, $since, $now, $idleTimeout, $maxLifetime)B= not _usable($handle, $since)
ABresult
1X1
011
000
171 or !_usable( $handle, $since ) )
172 {
173 2 eval { $handle->disconnect };
174 2 next;
175 }
176 28 $dbh = $handle;
177 28 last;
178 }
179 103 ▶ unless ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 75 28 103
180 75 ▶ $dbh = DBI->connect(
Condition50%1/2
'DBI'->connect($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1, 'AutoInactiveDestroy', 1, 'HandleError', \&Web::Session::Store::Postgres::_flagError}) || die($DBI::errstr)
l!l
750
MC/DC0%0/1
'DBI'->connect($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1, 'AutoInactiveDestroy', 1, 'HandleError', \&Web::Session::Store::Postgres::_flagError}) || die($DBI::errstr)
'DBI'->connect($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1, 'AutoInactiveDestroy', 1, 'HandleError', \&Web::Session::Store::Postgres::_flagError})
Truth table50%1/2
A
A= 'DBI'->connect($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1, 'AutoInactiveDestroy', 1, 'HandleError', \&Web::Session::Store::Postgres::_flagError})
Aresult
11
00
181 $datasource,
182 $username,
183 $password,
184 {
185 RaiseError => 1,
186 AutoCommit => 1,
187 AutoInactiveDestroy => 1,
188 HandleError => \&_flagError,
189 }
190 ) || die $DBI::errstr;
191 75 $dbh->{private_ws_born} = time;
192 75 $dbh->{private_ws_pid} = $$;
193 75 $dbh->{pg_enable_utf8} = 1;
194 }
195 103 $dbh->{private_ws_error} = 0;
196 return {
197 dbh => $dbh,
198 pool_key => $key,
199 103 ▶ pool_drops => $DROPS{$key} // 0,
Condition50%1/2
$Web::Session::Store::Postgres::DROPS{$key} // 0
l!l
0103
MC/DC0%0/1
$Web::Session::Store::Postgres::DROPS{$key} // 0
$Web::Session::Store::Postgres::DROPS{$key}
Truth table50%1/2
A
A= $Web::Session::Store::Postgres::DROPS{$key}
Aresult
11
00
200 pool_maxidle => _intArg( $args, 'MaxIdle', $MAX_IDLE ),
201 pool_idle => $idleTimeout,
202 pool_maxlife => $maxLifetime,
203 };
204 }
205  
206 # Lends to the store $self a connection to $datasource as $username (see
207 # _borrow()), given back by _giveBack() from DESTROY. The Patroni store calls
208 # it (as a fully qualified method, it doesn't inherit from this class) with
209 # the DSN of the primary it found.
210 3 ▶sub _pooledConnection {
Subroutine _pooledConnection: called CC=2 SCAR=6.9
211 3 my ( $self, $datasource, $username, $password, $args ) = @_;
212 3 my $loan = _borrow( $args, $datasource, $username, $password );
213 3 $self->{$_} = $loan->{$_} foreach ( keys %$loan );
214 3 $self->{disconnect} = 0;
215 }
216  
217 # Closes the idle connections of this process to the data source of key $key
218 # (see _poolKey()); those lent at this time are closed when given back. The
219 # Patroni store drops those to a former primary
220 0 ▶sub _dropIdle {
Subroutine _dropIdle: not called CC=5 SCAR=34.0
221 0 my ($key) = @_;
222 0 $DROPS{$key}++;
223 0 ▶ my $idle = delete $IDLE{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $idle = delete $Web::Session::Store::Postgres::IDLE{$key} or return 0 0 0
224 0 local $@;
225 0 foreach (@$idle) {
226 0 my $h = $_->[0];
227  
228 # Inherited from a parent process: left to it (AutoInactiveDestroy)
229 0 eval { $h->disconnect }
230 0 ▶ if ( $h->{Active} and $h->{private_ws_pid} == $$ );
Branch: 1 branch
BranchTrueFalseTotal
if $h->{"Active"} and $h->{"private_ws_pid"} == $$ 0 0 0
Condition0%0/3
$h->{"Active"} and $h->{"private_ws_pid"} == $$
!ll&&!rl&&r
000
MC/DC0%0/2
$h->{"Active"} and $h->{"private_ws_pid"} == $$
$h->{"Active"} $h->{"private_ws_pid"} == $$
Truth table0%0/3
A and B
A= $h->{"Active"}B= $h->{"private_ws_pid"} == $$
ABresult
0X0
100
111
231 }
232 }
233  
234 # Gives back the connection $loan->{dbh} taken by _borrow(). $loan is a store
235 # or a Web::Session::Store::Postgres::Loan (a plain function: the Patroni
236 # store calls it as a fully qualified method)
237 99 ▶sub _giveBack {
Subroutine _giveBack: called CC=15 SCAR=28.3
238 99 my ($loan) = @_;
239 99 my $key = $loan->{pool_key};
240 99 ▶ my $dbh = delete $loan->{dbh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = delete $loan->{"dbh"} or return 99 0 99
241 99 local $@; # disconnect() below must not clear the caller's $@
242  
243 # End of the program: the handles may already be freed, DBI closes them
244 99 ▶ return if ( ${^GLOBAL_PHASE} eq 'DESTRUCT' );
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 99 99
245  
246 # Lent when the process forked: the connection belongs to the parent.
247 # Neither given back nor closed here (AutoInactiveDestroy)
248 99 ▶ return if ( $dbh->{private_ws_pid} != $$ );
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"private_ws_pid"} != $$ 0 99 99
249  
250 # The connection goes back to the idle ones with AutoCommit on (no
251 # transaction left open, see the Patroni store), unless it met an error,
252 # is too old or was lent before its data source was dropped (a former
253 # Patroni primary). Expired idle
254 # connections (at the bottom of the stack, never reached when the pool
255 # stays busy) are closed now
256 99 my $now = time;
257 99 ▶ my $idle = $IDLE{$key} ||= [];
Condition50%1/2
$Web::Session::Store::Postgres::IDLE{$key} ||= []
l!l
990
MC/DC0%0/1
$Web::Session::Store::Postgres::IDLE{$key} ||= []
$Web::Session::Store::Postgres::IDLE{$key}
Truth table50%1/2
A
A= $Web::Session::Store::Postgres::IDLE{$key}
Aresult
11
00
258 @$idle = grep {
259 99 my ( $h, $since ) = @$_;
260 0 ▶ if ( $h->{private_ws_pid} != $$ ) { 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($h->{'private_ws_pid'} != $$) { } 0 16 16
elsif (_expired($h, $since, $now, $loan->{'pool_idle'}, $loan->{'pool_maxlife'})) { } 1 15 16
261 elsif (
262 _expired(
263 $h, $since, $now, $loan->{pool_idle}, $loan->{pool_maxlife}
264 )
265 )
266 {
267 1 eval { $h->disconnect };
268 1 0;
269 }
270 15 else { 1 }
271 } @$idle;
272 99 ▶ if ( !$dbh->{private_ws_error}
Branch: 1 branch
BranchTrueFalseTotal
if (not $dbh->{"private_ws_error"} and $dbh->{"Active"} and $dbh->{"AutoCommit"} and ($Web::Session::Store::Postgres::DROPS{$key} // 0) == ($loan->{"pool_drops"} // 0) and $now - $dbh->{"private_ws_born"} <= $loan->{"pool_maxlife"} and @$idle < $loan->{"pool_maxidle"}) 87 12 99
Condition73%14/19
not $dbh->{"private_ws_error"} and $dbh->{"Active"} and $dbh->{"AutoCommit"} and ($Web::Session::Store::Postgres::DROPS{$key} // 0) == ($loan->{"pool_drops"} // 0) and $now - $dbh->{"private_ws_born"} <= $loan->{"pool_maxlife"} and @$idle < $loan->{"pool_maxidle"}
!ll&&!rl&&r
10287
not $dbh->{"private_ws_error"} and $dbh->{"Active"} and $dbh->{"AutoCommit"} and ($Web::Session::Store::Postgres::DROPS{$key} // 0) == ($loan->{"pool_drops"} // 0) and $now - $dbh->{"private_ws_born"} <= $loan->{"pool_maxlife"}
!ll&&!rl&&r
9189
not $dbh->{"private_ws_error"} and $dbh->{"Active"} and $dbh->{"AutoCommit"} and ($Web::Session::Store::Postgres::DROPS{$key} // 0) == ($loan->{"pool_drops"} // 0)
!ll&&!rl&&r
9090
not $dbh->{"private_ws_error"} and $dbh->{"Active"} and $dbh->{"AutoCommit"}
!ll&&!rl&&r
9090
not $dbh->{"private_ws_error"} and $dbh->{"Active"}
!ll&&!rl&&r
9090
$Web::Session::Store::Postgres::DROPS{$key} // 0
l!l
090
$loan->{"pool_drops"} // 0
l!l
900
MC/DC0%0/6
not $dbh->{"private_ws_error"} and $dbh->{"Active"} and $dbh->{"AutoCommit"} and ($Web::Session::Store::Postgres::DROPS{$key} // 0) == ($loan->{"pool_drops"} // 0) and $now - $dbh->{"private_ws_born"} <= $loan->{"pool_maxlife"} and @$idle < $loan->{"pool_maxidle"}
not $dbh->{"private_ws_error"} $dbh->{"Active"} $dbh->{"AutoCommit"} $Web::Session::Store::Postgres::DROPS{$key} $now - $dbh->{"private_ws_born"} <= $loan->{"pool_maxlife"} @$idle < $loan->{"pool_maxidle"}
MC/DC0%0/1
$loan->{"pool_drops"} // 0
$loan->{"pool_drops"}
Truth table14%1/7
not(A) and B and C and D and E and F
A= not $dbh->{"private_ws_error"}B= $dbh->{"Active"}C= $dbh->{"AutoCommit"}D= $Web::Session::Store::Postgres::DROPS{$key}E= $now - $dbh->{"private_ws_born"} <= $loan->{"pool_maxlife"}F= @$idle < $loan->{"pool_maxidle"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
Truth table50%1/2
A
A= $loan->{"pool_drops"}
Aresult
11
00
273 and $dbh->{Active}
274 and $dbh->{AutoCommit}
275 and ( $DROPS{$key} // 0 ) == ( $loan->{pool_drops} // 0 )
276 and $now - $dbh->{private_ws_born} <= $loan->{pool_maxlife}
277 and @$idle < $loan->{pool_maxidle} )
278 {
279 87 push @$idle, [ $dbh, $now ];
280 87 return;
281 }
282 12 eval { $dbh->disconnect };
283 }
284  
285 # A new session written once. Apache::Session's TIEHASH saves a new session
286 # at once (an INSERT of its empty data), then its untie saves it again with
287 # its data (an UPDATE): two writes, the second one a new row version and an
288 # entry in each index. On the pooled path the INSERT waits for the untie: the
289 # update() that follows inserts the session with its data; a session untied
290 # unmodified is inserted by DESTROY, or by the END block below if it is still
291 # tied when the program ends; one removed in its own tie is never written.
292 # The connection is still taken at tie, so an unreachable database still
293 # fails the tie.
294 #
295 # Kept for the INSERT: the session id, the values of the indexed fields and
296 # the list of these fields, copied. Neither the session nor its arguments:
297 # Lemonldap::NG's cache wrapper keeps this store in the arguments
298 # (object_store), a cycle that would keep both until the end of the program.
299 43 ▶sub insert {
Subroutine insert: called CC=6 SCAR=17.9
Pod undocumented
300 43 my ( $self, $session ) = @_;
301 43 $self->connection($session);
302 43 ▶ return $self->SUPER::insert($session) unless defined $self->{pool_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pool_key"} 12 31 43
303  
304 # A generated id is random, no row has it. An id given by the caller
305 # (setId, as Lemonldap::NG does) may be used: the tie dies then, as the
306 # INSERT at tie does without the pool, so that the caller doesn't take
307 # another session's row for its new session
308 31 ▶ if ( $session->{args}->{setId} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"args"}{"setId"}) 2 29 31
309 2 my $id = $session->{data}->{_session_id};
310 my $sth = $self->{dbh}->prepare_cached( 'SELECT 1 FROM '
311 . $self->_quoteTable( $self->{table_name} )
312 2 . ' WHERE id = ?' );
313 2 $sth->execute($id);
314 2 my $used = $sth->fetchrow_arrayref;
315 2 $sth->finish;
316 2 ▶ die "Web::Session: session $id already exists\n" if ($used);
Branch: 1 branch
BranchTrueFalseTotal
if $used 1 1 2
317 }
318 30 my $index = $session->{args}->{Index};
319 30 ▶ $index = [ ref($index) ? @$index : split( /\s+/, $index // '' ) ];
Branch: 1 branch
BranchTrueFalseTotal
ref $index ? : 0 30 30
Condition100%2/2
$index // ""
l!l
228
MC/DC100%1/1
$index // ""
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
320 $self->{pending_insert} = {
321 data => {
322 33 map { ( $_ => $session->{data}->{$_} ) } '_session_id',
323 @$index
324 },
325 serialized => $session->{serialized},
326 30 args => { Index => $index },
327 };
328 30 Scalar::Util::weaken( $PENDING{ Scalar::Util::refaddr($self) } = $self );
329 30 return 1;
330 }
331  
332 # Stores of this process with a pending INSERT (weak references), inserted by
333 # the END block if their session is still tied when the program ends. Not
334 # from DESTROY: during the global destruction, DBI's objects may be freed
335 # before the store, in an order that Perl doesn't specify. This END block
336 # runs before DBI's, which this module loads first. A store lent a connection
337 # by a parent process (fork) is left to the parent. Under mod_perl, END may
338 # also run at the end of a request: an INSERT then only comes earlier, the
339 # untie that follows updates the row
340 11 ▶END { _flushPending() }
Subroutine END: called CC=1 SCAR=0.0
341  
342 15 ▶sub _flushPending {
Subroutine _flushPending: called CC=5 SCAR=16.3
343 15 foreach my $self ( grep { defined } values %PENDING ) {
344 2 ▶ my $dbh = $self->{dbh} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->{"dbh"} or next 2 0 2
345 2 ▶ next if ( $dbh->{private_ws_pid} != $$ );
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"private_ws_pid"} != $$ 1 1 2
346 1 ▶ my $pending = delete $self->{pending_insert} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pending = delete $self->{"pending_insert"} or next 1 0 1
347 1 $self->_insertPending($pending);
348 }
349 15 %PENDING = ();
350 }
351  
352 72 ▶sub _forgetPending {
Subroutine _forgetPending: called CC=1 SCAR=0.0
353 72 my ($self) = @_;
354 72 delete $PENDING{ Scalar::Util::refaddr($self) };
355 72 return delete $self->{pending_insert};
356 }
357  
358 # A failure is reported as a warning, like a failed update at untie
359 16 ▶sub _insertPending {
Subroutine _insertPending: called CC=2 SCAR=6.9
360 16 my ( $self, $pending ) = @_;
361 16 local $@;
362 16 ▶ eval { $self->SUPER::insert($pending); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->SUPER::insert($pending); 1 } } or warn 'Web::Session: unable to store new session ' . qq[$pending->{"data"}{"_session_id"}: $@] 14 2 16
363 or warn "Web::Session: unable to store new session "
364 . "$pending->{data}->{_session_id}: $@";
365 }
366  
367 15 ▶sub update {
Subroutine update: called CC=2 SCAR=6.9
Pod undocumented
368 15 my ( $self, $session ) = @_;
369 15 ▶ return $self->SUPER::update($session) unless ( $self->{pending_insert} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pending_insert"} 5 10 15
370  
371 # Inserted under the id the session had at tie (the one checked there
372 # when given by setId): Lemonldap::NG's hashed session store changes
373 # _session_id while the session is tied and puts it back before untie.
374 # If it didn't, an UPDATE would match no row; an INSERT under the
375 # current id would create a session under another id
376 10 my $id = $self->{pending_insert}->{data}->{_session_id};
377 my $r = $self->SUPER::insert(
378 {
379 %$session,
380 10 data => { %{ $session->{data} }, _session_id => $id },
381 }
382 );
383  
384 # Forgotten once written only: after a failed INSERT, the row still
385 # doesn't exist, a new save() must insert it, not update another row.
386 # Remembered as written: saved again unchanged in this tie, the session
387 # is not rewritten (see _unchanged() in Store/DBI.pm)
388 8 $self->_forgetPending;
389 8 $self->_remember($session);
390 8 return $r;
391 }
392  
393 5 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod undocumented
394 5 my ( $self, $session ) = @_;
395  
396 # Created and removed in the same tie: nothing was written, so nothing is
397 # deleted. A row with this id (given by setId, inserted by another
398 # process since the tie) is another session's
399 5 ▶ return 1 if ( $self->_forgetPending );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_forgetPending 4 1 5
400 1 return $self->SUPER::remove($session);
401 }
402  
403 # A connection of the pool lent outside of a store, for the duration of a
404 # class method of Web::Session::PgJSON (callers check _poolApplies() first).
405 # Returns a Web::Session::Store::Postgres::Loan object holding the handle in
406 # $loan->{dbh}, given back when the object is freed: when the method returns,
407 # and also when a query or a callback dies, so a connection never leaks.
408 42 ▶sub _lend {
Subroutine _lend: called CC=1 SCAR=0.0
409 42 my ( $class, $args ) = @_;
410 42 return bless _borrow($args), 'Web::Session::Store::Postgres::Loan';
411 }
412  
413 124 ▶sub connection {
Subroutine connection: called CC=5 SCAR=16.1
Pod undocumented
414 124 my ( $self, $session ) = @_;
415 124 $self->_forgetInheritedHandle;
416 124 ▶ return if defined $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dbh"} 43 81 124
417 81 my $args = $session->{args};
418 81 ▶ unless ( _poolApplies($args) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_poolApplies($args)) 23 58 81
419  
420 # AutoCommit off: see "Why AutoCommit differs" in Store/DBI.pm
421 23 $self->_connection( $session, 'Apache::Session::Store::Postgres',
422 { RaiseError => 1, AutoCommit => 0 } );
423 23 $self->{dbh}->{pg_enable_utf8} = 1;
424 23 $self->{commit} = _commit($args);
425 23 return;
426 }
427 $self->{table_name} =
428 58 ▶ $args->{TableName} || $Apache::Session::Store::DBI::TableName;
Condition66%2/3
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
l!l&&r!l&&!r
2560
MC/DC0%0/2
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
$args->{'TableName'} $Apache::Session::Store::DBI::TableName
Truth table66%2/3
A || B
A= $args->{'TableName'}B= $Apache::Session::Store::DBI::TableName
ABresult
1X1
011
000
429 58 my $loan = _borrow($args);
430 58 $self->{$_} = $loan->{$_} foreach ( keys %$loan );
431 58 $self->{commit} = 1;
432 58 $self->{disconnect} = 0;
433 }
434  
435 123 ▶sub DESTROY {
Subroutine DESTROY: called CC=6 SCAR=18.0
436 123 my $self = shift;
437  
438 # Lent when the process forked: the connection belongs to the parent,
439 # which writes its pending session
440 123 ▶ if ( $self->_forgetInheritedHandle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_forgetInheritedHandle) 5 118 123
441 5 $self->_forgetPending;
442 5 return;
443 }
444 118 ▶ return $self->SUPER::DESTROY(@_) unless defined $self->{pool_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pool_key"} 64 54 118
445  
446 # A new session untied without a successful update: inserted now, as it
447 # was tied. During the global destruction, the END block already did it
448 54 my $pending = $self->_forgetPending;
449 $self->_insertPending($pending)
450 54 ▶ if ( $pending and $self->{dbh} and ${^GLOBAL_PHASE} ne 'DESTRUCT' );
Branch: 1 branch
BranchTrueFalseTotal
if $pending and $self->{"dbh"} and ${^GLOBAL_PHASE} ne "DESTRUCT" 15 39 54
Condition66%4/6
$pending and $self->{"dbh"} and ${^GLOBAL_PHASE} ne "DESTRUCT"
!ll&&!rl&&r
39015
$pending and $self->{"dbh"}
!ll&&!rl&&r
39015
MC/DC33%1/3
$pending and $self->{"dbh"} and ${^GLOBAL_PHASE} ne "DESTRUCT"
$pending $self->{"dbh"} ${^GLOBAL_PHASE} ne "DESTRUCT"
Truth table50%2/4
A and B and C
A= $pendingB= $self->{"dbh"}C= ${^GLOBAL_PHASE} ne "DESTRUCT"
ABCresult
0XX0
10X0
1100
1111
451 54 _giveBack($self);
452 }
453  
454 package Web::Session::Store::Postgres::Loan;
455  
456 42 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
457 42 Web::Session::Store::Postgres::_giveBack( $_[0] );
458 }
459  
460 12 1;
« Session/PgJSON.pm ↑ Summary Session/MariaDBJSON.pm »