Session/Store/Postgres.pm

filter: Statementstmt 93.5% 131 / 140 Branchbran 83.8% 52 / 62 Conditioncond 70.5% 55 / 78 MC/DCmcdc 40.4% 17 / 42 Subroutinesub 95.4% 21 / 22 Podpod 0.0% 0 / 4 totaltotal 79.3% 276 / 348 CC 64 SCAR 22.3
CC
64
Cov
85%
_dropIdle
30.0
_giveBack
16.9
_borrow
13.3
CRAP 9.3
« Session/Store/Redis.pm ↑ Summary Session/PgJSON.pm »
1 package Web::Session::Store::Postgres;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 13 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Web::Session::Store::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Apache::Session::Store::Postgres;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 our @ISA =
10 qw(Web::Session::Store::DBI Apache::Session::Store::Postgres);
11 13 our $VERSION = '1.2.0';
12  
13 # A pool of connections, per process and per data source (DataSource +
14 # UserName), lent to one store at a time: two sessions tied together (a
15 # session and its persistent session) never share a connection, so never a
16 # transaction. With the connection reuse of the other stores
17 # (connect_cached(), see _connection() in Store/DBI.pm), they share the
18 # handle and its transaction: untying one commits or rolls back what the
19 # other has done so far.
20 #
21 # So the pool is the default connection reuse of this store when writes are
22 # committed (see _commit()), noreuse isn't set and no Handle is given.
23 # KeepConnections => 0 gives back the connect_cached() handle of the other
24 # stores. With Commit => 0, writes must not be committed (AutoCommit off,
25 # rolled back at untie), which the pool, where each statement commits
26 # itself, can't do: connect_cached() is kept.
27 #
28 # In the pool, each statement of this store is its own transaction
29 # (AutoCommit): it reads without a lock (Apache::Session::Store::DBI's
30 # materialize, no FOR UPDATE; the lock manager is Null), and a tie writes once
31 # (see insert()), so a transaction around a tie protected nothing and cost a
32 # BEGIN and a COMMIT round trip each. The Patroni store, whose materialize() locks the row, opens a
33 # transaction per tie on its pooled connection; _giveBack() never keeps a
34 # connection inside a transaction.
35 #
36 # With the same arguments, the class methods of Web::Session::PgJSON borrow
37 # a connection of this pool for the duration of each call (see _lend()), so
38 # that a process using both sessions and class methods doesn't keep one more
39 # connection for the latter.
40 # Times the idle connections of a data source were dropped by _dropIdle():
41 # a connection lent before the last drop is closed when given back
42 13 our %DROPS; # "datasource\0user" => count
43 13 our %IDLE; # "datasource\0user" => [ [ $dbh, $since ], ... ]
44  
45 # Defaults of the MaxIdle, IdleTimeout and MaxLifetime arguments
46 13 our $MAX_IDLE = 4; # idle connections kept per data source
47 13 our $IDLE_TIMEOUT = 120; # seconds: an idle connection older is closed
48 13 our $MAX_LIFETIME = 3600; # seconds: an older connection isn't kept
49  
50 # Integer argument $name of $args, or $default
51 309 ▶sub _intArg {
Subroutine _intArg: called CC=3 SCAR=11.0
52 309 my ( $args, $name, $default ) = @_;
53 309 my $v = $args->{$name};
54 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
55 }
56  
57 # Whether an idle handle can be lent without a round trip: one given back
58 # lately, its statements done, whose socket has nothing to read, is alive (a
59 # server that goes away closes it, which makes it readable). One idle for
60 # longer, or whose socket is readable or unknown, is pinged.
61 29 ▶sub _usable {
Subroutine _usable: called CC=5 SCAR=16.1
62 29 my ( $handle, $since ) = @_;
63 29 ▶ return 0 unless $handle->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $handle->{"Active"} 0 29 29
64 29 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if time - $since < $Web::Session::_common::PING_AFTER and Web::Session::_common::_quietSocket($handle) 1 28 29
Condition100%3/3
time - $since < $Web::Session::_common::PING_AFTER and Web::Session::_common::_quietSocket($handle)
!ll&&!rl&&r
2261
MC/DC100%2/2
time - $since < $Web::Session::_common::PING_AFTER and Web::Session::_common::_quietSocket($handle)
time - $since < $Web::Session::_common::PING_AFTER Web::Session::_common::_quietSocket($handle)
Truth table100%3/3
A and B
A= time - $since < $Web::Session::_common::PING_AFTERB= Web::Session::_common::_quietSocket($handle)
ABresult
0X0
100
111
65 if ( time - $since < $Web::Session::_common::PING_AFTER
66 and Web::Session::_common::_quietSocket($handle) );
67 28 ▶ return eval { $handle->ping } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $handle->ping } } ? : 27 1 28
68 }
69  
70 # Whether an idle connection given back at $since is expired
71 46 ▶sub _expired {
Subroutine _expired: called CC=2 SCAR=7.1
72 46 my ( $handle, $since, $now, $idleTimeout, $maxLifetime ) = @_;
73 return ( $now - $since > $idleTimeout
74 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
75 }
76  
77 # HandleError of pooled connections (inherited by their statement handles):
78 # a connection that met an error is closed when given back, never lent again.
79 # For example, after a failover behind a virtual IP, a connection to the
80 # former primary still answers ping() but every write fails. Returns false:
81 # RaiseError still applies.
82 9 ▶sub _flagError {
Subroutine _flagError: called CC=4 SCAR=14.2
83 9 my ( $msg, $h ) = @_;
84 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
85 9 ▶ $dbh->{private_ws_error} = 1 if ($dbh);
Branch: 1 branch
BranchTrueFalseTotal
if $dbh 9 0 9
86 9 return 0;
87 }
88  
89 # Commit policy of a session. Without a Handle, writes are committed unless
90 # Commit is explicitly false: a missing or empty Commit keeps the default (as
91 # the Patroni store does), else a session written without it would be rolled
92 # back at untie. With a Handle, the caller owns the transaction: Commit is
93 # used as given, as in Apache::Session::Store::Postgres
94 154 ▶sub _commit {
Subroutine _commit: called CC=4 SCAR=13.9
95 154 my ($args) = @_;
96 154 ▶ return $args->{Commit} if ( exists $args->{Handle} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"Handle"} 8 146 154
97 146 my $commit = $args->{Commit};
98 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
99 }
100  
101 # Whether the pool applies to these arguments: KeepConnections not disabled,
102 # writes committed, no noreuse, no Handle. $commit is the commit policy of
103 # the store, _commit($args) by default (the Patroni store has its own, see
104 # Store/Patroni.pm). Shared by connection(), by the Patroni store and by the
105 # class methods of Web::Session::PgJSON (see _lend()), so that they stay
106 # consistent
107 142 ▶sub _poolApplies {
Subroutine _poolApplies: called CC=8 SCAR=20.8
108 142 my ( $args, $commit ) = @_;
109 142 ▶ $commit = _commit($args) if ( @_ < 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 131 11 142
110  
111 # KeepConnections is on unless explicitly disabled: a missing or empty
112 # value (configuration interfaces may store an empty option as '') keeps
113 # the default
114 142 my $keep = $args->{KeepConnections};
115 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
116 return ( $keep
117 and $commit
118 and !$args->{noreuse}
119 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
120 }
121  
122 # Key of the idle connections of a data source in %IDLE
123 103 ▶sub _poolKey {
Subroutine _poolKey: called CC=3 SCAR=11.1
124 103 my ( $datasource, $username ) = @_;
125 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
126 }
127  
128 # Takes a connection to $datasource as $username of the pool, an idle one if
129 # possible, a new one otherwise. Without $datasource, connects to the data
130 # source of $args; $args also gives the MaxIdle, IdleTimeout and MaxLifetime
131 # options. Lent means removed from the idle ones: a connection is never lent
132 # twice at once. Returns the handle and what _giveBack() needs:
133 # { dbh, pool_key, pool_maxidle, pool_idle, pool_maxlife }
134 103 ▶sub _borrow {
Subroutine _borrow: called CC=13 SCAR=25.9
135 103 my ( $args, $datasource, $username, $password ) = @_;
136 103 ▶ if ( @_ < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 100 3 103
137 $datasource = $args->{DataSource}
138 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
139 $username = $args->{UserName}
140 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
141 $password = $args->{Password}
142 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
143 }
144 103 my $key = _poolKey( $datasource, $username );
145 103 my $idleTimeout = _intArg( $args, 'IdleTimeout', $IDLE_TIMEOUT );
146 103 my $maxLifetime = _intArg( $args, 'MaxLifetime', $MAX_LIFETIME );
147 103 my $now = time;
148 103 my $dbh;
149 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
150 31 my ( $handle, $since ) = @$idle;
151  
152 # Inherited from the parent before a fork: its socket is the parent's.
153 # AutoInactiveDestroy keeps this process from closing it
154 31 ▶ next if ( $handle->{private_ws_pid} != $$ );
Branch: 1 branch
BranchTrueFalseTotal
if $handle->{"private_ws_pid"} != $$ 1 30 31
155  
156 # Idle for too long, a NAT or load balancer may have dropped it: a
157 # ping could then wait for the TCP timeout. Closed without a ping
158 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
159 or !_usable( $handle, $since ) )
160 {
161 2 eval { $handle->disconnect };
162 2 next;
163 }
164 28 $dbh = $handle;
165 28 last;
166 }
167 103 ▶ unless ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 75 28 103
168 75 ▶ $dbh = DBI->connect(
Condition50%1/2
'DBI'->connect(Web::Session::_common::_pgDataSource($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(Web::Session::_common::_pgDataSource($datasource), $username, $password, {'RaiseError', 1, 'AutoCommit', 1, 'AutoInactiveDestroy', 1, 'HandleError', \&Web::Session::Store::Postgres::_flagError}) || die($DBI::errstr)
'DBI'->connect(Web::Session::_common::_pgDataSource($datasource), $username, $password, {'RaiseError', 1, 'AutoCommit', 1, 'AutoInactiveDestroy', 1, 'HandleError', \&Web::Session::Store::Postgres::_flagError})
Truth table50%1/2
A
A= 'DBI'->connect(Web::Session::_common::_pgDataSource($datasource), $username, $password, {'RaiseError', 1, 'AutoCommit', 1, 'AutoInactiveDestroy', 1, 'HandleError', \&Web::Session::Store::Postgres::_flagError})
Aresult
11
00
169 Web::Session::_common::_pgDataSource($datasource),
170 $username,
171 $password,
172 {
173 RaiseError => 1,
174 AutoCommit => 1,
175 AutoInactiveDestroy => 1,
176 HandleError => \&_flagError,
177 }
178 ) || die $DBI::errstr;
179 75 $dbh->{private_ws_born} = time;
180 75 $dbh->{private_ws_pid} = $$;
181 75 $dbh->{pg_enable_utf8} = 1;
182 }
183 103 $dbh->{private_ws_error} = 0;
184 return {
185 dbh => $dbh,
186 pool_key => $key,
187 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
188 pool_maxidle => _intArg( $args, 'MaxIdle', $MAX_IDLE ),
189 pool_idle => $idleTimeout,
190 pool_maxlife => $maxLifetime,
191 };
192 }
193  
194 # Lends to the store $self a connection to $datasource as $username (see
195 # _borrow()), given back by _giveBack() from DESTROY. The Patroni store calls
196 # it (as a fully qualified method, it doesn't inherit from this class) with
197 # the DSN of the primary it found.
198 3 ▶sub _pooledConnection {
Subroutine _pooledConnection: called CC=2 SCAR=6.9
199 3 my ( $self, $datasource, $username, $password, $args ) = @_;
200 3 my $loan = _borrow( $args, $datasource, $username, $password );
201 3 $self->{$_} = $loan->{$_} foreach ( keys %$loan );
202 3 $self->{disconnect} = 0;
203 }
204  
205 # Closes the idle connections of this process to the data source of key $key
206 # (see _poolKey()); those lent at this time are closed when given back. The
207 # Patroni store drops those to a former primary
208 0 ▶sub _dropIdle {
Subroutine _dropIdle: not called CC=5 SCAR=34.0
209 0 my ($key) = @_;
210 0 $DROPS{$key}++;
211 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
212 0 local $@;
213 0 foreach (@$idle) {
214 0 my $h = $_->[0];
215  
216 # Inherited from a parent process: left to it (AutoInactiveDestroy)
217 0 eval { $h->disconnect }
218 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
219 }
220 }
221  
222 # Gives back the connection $loan->{dbh} taken by _borrow(). $loan is a store
223 # or a Web::Session::Store::Postgres::Loan (a plain function: the Patroni
224 # store calls it as a fully qualified method)
225 99 ▶sub _giveBack {
Subroutine _giveBack: called CC=15 SCAR=28.3
226 99 my ($loan) = @_;
227 99 my $key = $loan->{pool_key};
228 99 ▶ my $dbh = delete $loan->{dbh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = delete $loan->{"dbh"} or return 99 0 99
229 99 local $@; # disconnect() below must not clear the caller's $@
230  
231 # End of the program: the handles may already be freed, DBI closes them
232 99 ▶ return if ( ${^GLOBAL_PHASE} eq 'DESTRUCT' );
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 99 99
233  
234 # Lent when the process forked: the connection belongs to the parent.
235 # Neither given back nor closed here (AutoInactiveDestroy)
236 99 ▶ return if ( $dbh->{private_ws_pid} != $$ );
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"private_ws_pid"} != $$ 0 99 99
237  
238 # The connection goes back to the idle ones with AutoCommit on (no
239 # transaction left open, see the Patroni store), unless it met an error,
240 # is too old or was lent before its data source was dropped (a former
241 # Patroni primary). Expired idle
242 # connections (at the bottom of the stack, never reached when the pool
243 # stays busy) are closed now
244 99 my $now = time;
245 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
246 @$idle = grep {
247 99 my ( $h, $since ) = @$_;
248 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
249 elsif (
250 _expired(
251 $h, $since, $now, $loan->{pool_idle}, $loan->{pool_maxlife}
252 )
253 )
254 {
255 1 eval { $h->disconnect };
256 1 0;
257 }
258 15 else { 1 }
259 } @$idle;
260 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
261 and $dbh->{Active}
262 and $dbh->{AutoCommit}
263 and ( $DROPS{$key} // 0 ) == ( $loan->{pool_drops} // 0 )
264 and $now - $dbh->{private_ws_born} <= $loan->{pool_maxlife}
265 and @$idle < $loan->{pool_maxidle} )
266 {
267 87 push @$idle, [ $dbh, $now ];
268 87 return;
269 }
270 12 eval { $dbh->disconnect };
271 }
272  
273 # A new session is written once on the pooled path (see "New sessions written
274 # once" in Store/DBI.pm). The connection is still taken at tie, so an
275 # unreachable database still fails the tie
276 43 ▶sub insert {
Subroutine insert: called CC=2 SCAR=6.9
Pod undocumented
277 43 my ( $self, $session ) = @_;
278 43 $self->connection($session);
279 43 ▶ return $self->SUPER::insert($session) unless defined $self->{pool_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pool_key"} 12 31 43
280 31 return $self->_deferInsert($session);
281 }
282  
283 15 ▶sub update {
Subroutine update: called CC=2 SCAR=6.9
Pod undocumented
284 15 my ( $self, $session ) = @_;
285 15 ▶ return 1 if ( $self->_insertPending($session) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_insertPending($session) 8 5 13
286 5 return $self->SUPER::update($session);
287 }
288  
289 5 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod undocumented
290 5 my ( $self, $session ) = @_;
291 5 ▶ return 1 if ( $self->_removePending );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_removePending 4 1 5
292 1 return $self->SUPER::remove($session);
293 }
294  
295 # A connection of the pool lent outside of a store, for the duration of a
296 # class method of Web::Session::PgJSON (callers check _poolApplies() first).
297 # Returns a Web::Session::Store::Postgres::Loan object holding the handle in
298 # $loan->{dbh}, given back when the object is freed: when the method returns,
299 # and also when a query or a callback dies, so a connection never leaks.
300 42 ▶sub _lend {
Subroutine _lend: called CC=1 SCAR=0.0
301 42 my ( $class, $args ) = @_;
302 42 return bless _borrow($args), 'Web::Session::Store::Postgres::Loan';
303 }
304  
305 124 ▶sub connection {
Subroutine connection: called CC=6 SCAR=18.0
Pod undocumented
306 124 my ( $self, $session ) = @_;
307 124 $self->_forgetInheritedHandle;
308 124 ▶ return if defined $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dbh"} 43 81 124
309 81 my $args = $session->{args};
310 81 ▶ unless ( _poolApplies($args) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_poolApplies($args)) 23 58 81
311  
312 # AutoCommit off: see "Why AutoCommit differs" in Store/DBI.pm
313 local $args->{DataSource} =
314 Web::Session::_common::_pgDataSource( $args->{DataSource}
315 23 ▶ || $Apache::Session::Store::Postgres::DataSource );
Condition33%1/3
$args->{'DataSource'} || $Apache::Session::Store::Postgres::DataSource
l!l&&r!l&&!r
2300
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
316 23 $self->_connection( $session, 'Apache::Session::Store::Postgres',
317 { RaiseError => 1, AutoCommit => 0 } );
318 23 $self->{dbh}->{pg_enable_utf8} = 1;
319 23 $self->{commit} = _commit($args);
320 23 return;
321 }
322 $self->{table_name} =
323 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
324 58 my $loan = _borrow($args);
325 58 $self->{$_} = $loan->{$_} foreach ( keys %$loan );
326 58 $self->{commit} = 1;
327 58 $self->{disconnect} = 0;
328 }
329  
330 123 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
331 123 my $self = shift;
332  
333 # Lent when the process forked: the connection belongs to the parent,
334 # which writes its pending session
335 123 ▶ if ( $self->_forgetInheritedHandle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_forgetInheritedHandle) 5 118 123
336 5 $self->_forgetPending;
337 5 return;
338 }
339 118 ▶ return $self->SUPER::DESTROY(@_) unless defined $self->{pool_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pool_key"} 64 54 118
340 54 $self->_destroyPending;
341 54 _giveBack($self);
342 }
343  
344 # Internal class, kept out of the CPAN index by the line break
345 package # hide
346 Web::Session::Store::Postgres::Loan;
347  
348 42 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
349 42 Web::Session::Store::Postgres::_giveBack( $_[0] );
350 }
351  
352 13 1;
« Session/Store/Redis.pm ↑ Summary Session/PgJSON.pm »