blib/lib/Langertha/Knarr/TokenStore/Pg.pm

filter: Statementstmt 17.5% 70 / 400 Branchbran 2.4% 5 / 206 Conditioncond 0.0% 0 / 83 MC/DCmcdc 0.0% 0 / 52 Subroutinesub 25.9% 14 / 54 Podpod 71.4% 5 / 7 totaltotal 11.7% 94 / 802 CC 141 SCAR 40.7
CC
141
Cov
11%
_tcp_target
240.0
_pump
210.0
poll
90.0
CRAP 58.8
« blib/lib/Langertha/Knarr/Admin.pm ↑ Summary blib/lib/Langertha/Knarr/Handler/ACPClient.pm »
1 package Langertha::Knarr::TokenStore::Pg;
2 # ABSTRACT: PostgreSQL token store for token_auth, asynchronous under IO::Async
3 1 our $VERSION = '1.103';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Async::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::Async::Timer::Periodic;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Module::Runtime qw( require_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 1 my %DEFAULT_COLUMNS = (
16 hash => 'token_hash',
17 model => 'model',
18 provider => 'provider',
19 expires => 'expires_at',
20 );
21  
22 1 my $UNAVAILABLE = "token store unavailable\n";
23  
24 1 has dsn => ( is => 'ro', isa => 'Str', required => 1 );
25  
26 1 has user => ( is => 'ro', isa => 'Maybe[Str]' );
27  
28 1 has password_env => ( is => 'ro', isa => 'Maybe[Str]' );
29  
30 1 has relation => ( is => 'ro', isa => 'Str', required => 1 );
31  
32 1 has listen => ( is => 'ro', isa => 'Str', required => 1 );
33  
34 has columns => (
35 is => 'ro',
36 isa => 'HashRef',
37 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 1 );
39  
40 has loop => (
41 is => 'ro',
42 lazy => 1,
43 0 ▶ default => sub { IO::Async::Loop->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 1 );
45  
46 1 has connect_timeout => ( is => 'ro', isa => 'Num', default => 3 );
47  
48 1 has query_timeout => ( is => 'ro', isa => 'Num', default => 10 );
49  
50 # connection state, all of it owned by the process in _pid
51 1 has _dbh => ( is => 'rw', clearer => '_clear_dbh' );
52 1 has _pid => ( is => 'rw', isa => 'Int', default => 0 );
53 1 has _watch => ( is => 'rw', clearer => '_clear_watch' );
54 1 has _ticker => ( is => 'rw', clearer => '_clear_ticker' );
55 1 has _inflight => ( is => 'rw', clearer => '_clear_inflight' );
56 1 ▶has _queue => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 has _sql => ( is => 'rw', isa => 'Str' );
58  
59 # reconnect state
60 1 has _use_loop => ( is => 'rw', isa => 'Bool', default => 0 );
61 1 has _backoff => ( is => 'rw', isa => 'Num', default => 0 );
62 1 has _next_attempt => ( is => 'rw', isa => 'Num', default => 0 );
63 1 has _reconnect_f => ( is => 'rw', clearer => '_clear_reconnect_f' );
64 1 has _connecting_f => ( is => 'rw', clearer => '_clear_connecting_f' );
65  
66 1 with 'Langertha::Knarr::TokenStore';
67  
68 1 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=16.0
Pod undocumented
69 1 my ($self) = @_;
70 0 my $ok = eval { require_module('DBI'); require_module('DBD::Pg'); 1 };
71 1 ▶ croak "token_auth backend postgresql needs DBD::Pg" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 0 1
72 0 my @unknown = grep { !exists $DEFAULT_COLUMNS{$_} } sort keys %{ $self->columns };
73 0 ▶ croak __PACKAGE__.": unknown column key(s) ".join(', ', @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 0 0
74 0 return;
75 }
76  
77 1 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.0
Pod undocumented
78 1 my ($self, $in_global_destruction) = @_;
79 1 ▶ return if $in_global_destruction;
Branch: 1 branch
BranchTrueFalseTotal
if $in_global_destruction 0 1 1
80 1 $self->_teardown;
81 1 return;
82 }
83  
84 0 ▶sub _column {
Subroutine _column: not called CC=3 SCAR=24.8
85 0 my ($self, $key) = @_;
86 0 my $name = $self->columns->{$key};
87 0 ▶ return defined $name && length $name ? $name : $DEFAULT_COLUMNS{$key};
Branch: 1 branch
BranchTrueFalseTotal
defined $name && length $name ? : 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
88 }
89  
90 0 ▶sub available {
Subroutine available: not called CC=4 SCAR=30.0
Pod documented
91 0 my ($self) = @_;
92 0 ▶ my $dbh = $self->_dbh or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return 0 0 0 0
93 0 ▶ return 0 unless $self->_pid == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_pid == $$ 0 0 0
94 0 ▶ return $dbh->{pg_socket} >= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->{'pg_socket'} >= 0 ? : 0 0 0
95 }
96  
97 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
98 0 my ($self) = @_;
99 0 $self->_use_loop(1);
100 0 $self->_loop_state;
101 0 return;
102 }
103  
104 0 ▶sub lookup_f {
Subroutine lookup_f: not called CC=5 SCAR=34.0
Pod documented
105 0 my ($self, $hash) = @_;
106 0 $hash = lc $hash;
107 0 $self->_use_loop(1);
108 0 my $state = $self->_loop_state;
109 0 ▶ return $self->_unavailable_f if $state eq 'down';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "down" 0 0 0
110 0 for my $q ( grep { defined } $self->_inflight, @{ $self->_queue } ) {
111 0 ▶ next unless $q->{hash} eq $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $q->{"hash"} eq $hash 0 0 0
112 0 my $f = $self->loop->new_future;
113 0 push @{ $q->{futures} }, $f;
114 0 return $f;
115 }
116 0 my $f = $self->loop->new_future;
117 0 push @{ $self->_queue }, { hash => $hash, futures => [ $f ] };
118 0 ▶ $self->_send_next if $state eq 'up';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "up" 0 0 0
119 0 return $f;
120 }
121  
122 0 ▶sub lookup {
Subroutine lookup: not called CC=4 SCAR=30.0
Pod documented
123 0 my ($self, $hash) = @_;
124 0 $self->poll;
125 0 ▶ my $dbh = $self->_ensure_connected or croak $UNAVAILABLE;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_ensure_connected or croak($UNAVAILABLE) 0 0 0
126 0 my $r;
127 0 my $ok = eval { $r = $dbh->selectrow_arrayref( $self->_sql, undef, lc $hash ); 1 };
128 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
129 0 $self->_log_db_error( 'lookup failed', $@ );
130 0 ▶ $self->_lost('lookup failed on a closed connection') unless $self->_connection_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_connection_ok 0 0 0
131 0 croak $UNAVAILABLE;
132 }
133 0 return $self->_row($r);
134 }
135  
136 0 ▶sub poll {
Subroutine poll: not called CC=9 SCAR=45.0
Pod documented
137 0 my ($self) = @_;
138 0 $self->_forget_inherited;
139 0 ▶ my $dbh = $self->_dbh or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return 0 0 0
140 # At the connection's end the first read may still succeed; the socket
141 # then stays readable and the next one fails.
142 0 for ( 1 .. 16 ) {
143 0 my @notes;
144 0 my $ok = eval {
145 0 ▶ while ( my $n = $dbh->pg_notifies ) { push @notes, $n }
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $dbh->pg_notifies) 0 0 0
146 0 1;
147 };
148 0 $self->_notified($_) for @notes;
149 0 ▶ return $self->_lost('connection closed') unless $ok && $self->_connection_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok and $self->_connection_ok 0 0 0
Condition0%0/3
$ok and $self->_connection_ok
!ll&&!rl&&r
000
MC/DC0%0/2
$ok and $self->_connection_ok
$ok $self->_connection_ok
Truth table0%0/3
A and B
A= $okB= $self->_connection_ok
ABresult
0X0
100
111
150 0 ▶ last unless @notes || $self->_readable_now;
Branch: 1 branch
BranchTrueFalseTotal
unless @notes or $self->_readable_now 0 0 0
Condition0%0/3
@notes or $self->_readable_now
l!l&&r!l&&!r
000
MC/DC0%0/2
@notes or $self->_readable_now
@notes $self->_readable_now
Truth table0%0/3
A or B
A= @notesB= $self->_readable_now
ABresult
1X1
011
000
151 }
152 0 return;
153 }
154  
155 0 ▶sub _unavailable_f { Future->fail( $UNAVAILABLE, 'token_store' ) }
Subroutine _unavailable_f: not called CC=1 SCAR=6.9
156  
157 0 ▶sub _row {
Subroutine _row: not called CC=3 SCAR=24.8
158 0 my ($self, $r) = @_;
159 0 ▶ return undef unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
160 return {
161 0 ▶ model => $r->[0],
Condition0%0/2
$r->[2] // 0
l!l
00
MC/DC0%0/1
$r->[2] // 0
$r->[2]
Truth table0%0/2
A
A= $r->[2]
Aresult
11
00
162 provider => $r->[1],
163 expires => 0 + ( $r->[2] // 0 ),
164 };
165 }
166  
167 0 ▶sub _connection_ok {
Subroutine _connection_ok: not called CC=5 SCAR=34.0
168 0 my ($self) = @_;
169 0 ▶ my $dbh = $self->_dbh or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return 0 0 0 0
170 0 ▶ my $state = $dbh->state // '';
Condition0%0/2
$dbh->state // ""
l!l
00
MC/DC0%0/1
$dbh->state // ""
$dbh->state
Truth table0%0/2
A
A= $dbh->state
Aresult
11
00
171 0 ▶ return 0 if $state =~ /^(08|57P0)/;
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /^(08|57P0)/ 0 0 0
172 0 ▶ return $dbh->{pg_socket} >= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->{'pg_socket'} >= 0 ? : 0 0 0
173 }
174  
175 #### Connecting
176  
177 0 ▶sub _dsn {
Subroutine _dsn: not called CC=8 SCAR=42.8
178 0 my ($self) = @_;
179 0 my $dsn = $self->dsn;
180 0 my %has = map { lc($_) => 1 } $dsn =~ /([A-Za-z_]+)\s*=/g;
181 0 my @add;
182 push @add, 'connect_timeout='.int( $self->connect_timeout )
183 0 ▶ if !$has{connect_timeout} && $self->connect_timeout > 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $has{"connect_timeout"} and $self->connect_timeout > 0 0 0 0
Condition0%0/3
not $has{"connect_timeout"} and $self->connect_timeout > 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $has{"connect_timeout"} and $self->connect_timeout > 0
not $has{"connect_timeout"} $self->connect_timeout > 0
Truth table0%0/3
not(A) and B
A= not $has{"connect_timeout"}B= $self->connect_timeout > 0
ABresult
0X0
100
111
184 0 ▶ push @add, 'keepalives_idle=30' unless $has{keepalives_idle};
Branch: 1 branch
BranchTrueFalseTotal
unless $has{"keepalives_idle"} 0 0 0
185 0 ▶ push @add, 'keepalives_interval=10' unless $has{keepalives_interval};
Branch: 1 branch
BranchTrueFalseTotal
unless $has{"keepalives_interval"} 0 0 0
186 0 ▶ push @add, 'keepalives_count=3' unless $has{keepalives_count};
Branch: 1 branch
BranchTrueFalseTotal
unless $has{"keepalives_count"} 0 0 0
187 0 ▶ return $dsn unless @add;
Branch: 1 branch
BranchTrueFalseTotal
unless @add 0 0 0
188 0 ▶ return $dsn.( $dsn =~ /[:;]\z/ ? '' : ';' ).join(';', @add);
Branch: 1 branch
BranchTrueFalseTotal
$dsn =~ /[:;]\z/ ? : 0 0 0
189 }
190  
191 0 ▶sub _password {
Subroutine _password: not called CC=4 SCAR=30.0
192 0 my ($self) = @_;
193 0 my $env = $self->password_env;
194 0 ▶ return undef unless defined $env && length $env;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env and length $env 0 0 0
Condition0%0/3
defined $env and length $env
!ll&&!rl&&r
000
MC/DC0%0/2
defined $env and length $env
defined $env length $env
Truth table0%0/3
A and B
A= defined $envB= length $env
ABresult
0X0
100
111
195 0 my $password = $ENV{$env};
196 0 ▶ $log->warnf('token store: password_env %s is not set', $env)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password 0 0 0
197 unless defined $password;
198 0 return $password;
199 }
200  
201 # Under the loop: up, connecting (lookups wait in the queue) or down
202 # (lookups fail at once). Starts a connect attempt when one is due.
203 0 ▶sub _loop_state {
Subroutine _loop_state: not called CC=8 SCAR=42.8
204 0 my ($self) = @_;
205 0 $self->_forget_inherited;
206 0 ▶ if ( $self->_dbh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_dbh) 0 0 0
207 0 ▶ return 'up' if $self->_connection_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_connection_ok 0 0 0
208 0 $self->_lost('connection closed');
209 0 return 'down';
210 }
211 0 ▶ return 'connecting' if $self->_connecting_f;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_connecting_f 0 0 0
212 0 ▶ return 'down' if $self->_reconnect_f || time < $self->_next_attempt;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_reconnect_f or time < $self->_next_attempt 0 0 0
Condition0%0/3
$self->_reconnect_f or time < $self->_next_attempt
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_reconnect_f or time < $self->_next_attempt
$self->_reconnect_f time < $self->_next_attempt
Truth table0%0/3
A or B
A= $self->_reconnect_fB= time < $self->_next_attempt
ABresult
1X1
011
000
213 0 $self->_start_connect;
214 0 ▶ return $self->_dbh ? 'up' : $self->_connecting_f ? 'connecting' : 'down';
Branch: 2 branches
BranchTrueFalseTotal
$self->_dbh ? : 0 0 0
$self->_connecting_f ? : 0 0 0
215 }
216  
217 # The DBI connect blocks, so under the loop it only runs once a
218 # non-blocking TCP connect to the server got through: a server that is down
219 # or unreachable costs the loop nothing, only a failed attempt.
220 0 ▶sub _start_connect {
Subroutine _start_connect: not called CC=1 SCAR=6.9
221 0 my ($self) = @_;
222 0 weaken( my $weak = $self );
223 my $f = $self->_probe_f->then(
224 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
225 0 ▶ return Future->fail("token store gone\n") unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 0 0
226 0 ▶ return $weak->_connect ? Future->done : Future->fail("connect failed\n");
Branch: 1 branch
BranchTrueFalseTotal
$weak->_connect ? : 0 0 0
227 },
228 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
229 0 ▶ return Future->fail("token store gone\n") unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 0 0
230 0 my ( $host, $port ) = $weak->_tcp_target;
231 0 $log->warnf( 'token store: %s port %s not reachable', $host, $port );
232 0 $weak->_retry_later;
233 0 return Future->fail("not reachable\n");
234 },
235 0 );
236 0 $self->_connecting_f($f);
237 0 ▶ $f->on_ready( sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
238 0 my ($done) = @_;
239 0 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 0 0
240 0 $weak->_clear_connecting_f;
241 0 ▶ $done->is_done ? $weak->_send_next : $weak->_fail_pending;
Branch: 1 branch
BranchTrueFalseTotal
$done->is_done ? : 0 0 0
242 0 });
243 0 return;
244 }
245  
246 # host and port the DSN connects to over TCP; empty for a Unix socket, a
247 # service file or a list of hosts, which are not probed.
248 0 ▶sub _tcp_target {
Subroutine _tcp_target: not called CC=15 SCAR=54.8
249 0 my ($self) = @_;
250 0 my $dsn = $self->dsn;
251 0 $dsn =~ s/\Adbi:Pg:?//i;
252 0 my %p;
253 0 for my $pair ( split /;/, $dsn ) {
254 0 ▶ my ( $k, $v ) = $pair =~ /\A\s*([A-Za-z_]+)\s*=\s*(.*?)\s*\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $pair =~ /\A\s*([A-Za-z_]+)\s*=\s*(.*?)\s*\z/ or next 0 0 0
255 0 $p{ lc $k } = $v;
256 }
257 0 ▶ return if defined $p{service};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p{"service"} 0 0 0
258 my $host = defined $p{hostaddr} && length $p{hostaddr} ? $p{hostaddr}
259 : defined $p{host} && length $p{host} ? $p{host}
260 0 ▶ : $ENV{PGHOST};
Branch: 2 branches
BranchTrueFalseTotal
defined $p{'hostaddr'} && length $p{'hostaddr'} ? : 0 0 0
defined $p{'host'} && length $p{'host'} ? : 0 0 0
Condition0%0/6
defined $p{"hostaddr"} and length $p{"hostaddr"}
!ll&&!rl&&r
000
defined $p{"host"} and length $p{"host"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p{"hostaddr"} and length $p{"hostaddr"}
defined $p{"hostaddr"} length $p{"hostaddr"}
MC/DC0%0/2
defined $p{"host"} and length $p{"host"}
defined $p{"host"} length $p{"host"}
Truth table0%0/3
A and B
A= defined $p{"hostaddr"}B= length $p{"hostaddr"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $p{"host"}B= length $p{"host"}
ABresult
0X0
100
111
261 0 ▶ return unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 0 0
Condition0%0/3
defined $host and length $host
!ll&&!rl&&r
000
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table0%0/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
262 0 ▶ return if $host =~ m{\A/} || $host =~ /,/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ m[\A/] or $host =~ /,/ 0 0 0
Condition0%0/3
$host =~ m[\A/] or $host =~ /,/
l!l&&r!l&&!r
000
MC/DC0%0/2
$host =~ m[\A/] or $host =~ /,/
$host =~ m[\A/] $host =~ /,/
Truth table0%0/3
A or B
A= $host =~ m[\A/]B= $host =~ /,/
ABresult
1X1
011
000
263 0 ▶ my $port = $p{port} // $ENV{PGPORT} // 5432;
Condition0%0/5
($p{"port"} // $ENV{"PGPORT"}) // 5432
l!l
00
$p{"port"} // $ENV{"PGPORT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($p{"port"} // $ENV{"PGPORT"}) // 5432
$p{"port"} $ENV{"PGPORT"}
Truth table0%0/3
A // B
A= $p{"port"}B= $ENV{"PGPORT"}
ABresult
1X1
011
000
264 0 ▶ return if $port =~ /,/;
Branch: 1 branch
BranchTrueFalseTotal
if $port =~ /,/ 0 0 0
265 0 return ( $host, $port );
266 }
267  
268 0 ▶sub _probe_f {
Subroutine _probe_f: not called CC=3 SCAR=24.8
269 0 my ($self) = @_;
270 0 my ( $host, $port ) = $self->_tcp_target;
271 0 ▶ return Future->done unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 0 0
272 0 ▶ my $after = $self->connect_timeout > 0 ? $self->connect_timeout : 3;
Branch: 1 branch
BranchTrueFalseTotal
$self->connect_timeout > 0 ? : 0 0 0
273 return Future->wait_any(
274 $self->loop->connect(
275 host => $host,
276 service => $port,
277 socktype => 'stream',
278 0 ▶ )->then( sub { my ($sock) = @_; close $sock if $sock; Future->done } ),
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $sock 0 0 0
279 $self->loop->timeout_future( after => $after ),
280 );
281 }
282  
283 0 ▶sub _ensure_connected {
Subroutine _ensure_connected: not called CC=5 SCAR=34.0
284 0 my ($self) = @_;
285 0 $self->_forget_inherited;
286 0 ▶ if ( $self->_dbh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_dbh) 0 0 0
287 0 ▶ return $self->_dbh if $self->_connection_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_connection_ok 0 0 0
288 0 $self->_lost('connection closed');
289 0 return;
290 }
291 0 ▶ return if $self->_reconnect_f;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_reconnect_f 0 0 0
292 0 ▶ return if time < $self->_next_attempt;
Branch: 1 branch
BranchTrueFalseTotal
if time < $self->_next_attempt 0 0 0
293 0 return $self->_connect;
294 }
295  
296 # A handle from before a fork belongs to the parent: leave its connection
297 # alone, drop it here, and invalidate, since this process missed every
298 # notification since the fork.
299 0 ▶sub _forget_inherited {
Subroutine _forget_inherited: not called CC=3 SCAR=24.8
300 0 my ($self) = @_;
301 0 ▶ return unless $self->_dbh && $self->_pid != $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_dbh and $self->_pid != $$ 0 0 0
Condition0%0/3
$self->_dbh and $self->_pid != $$
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_dbh and $self->_pid != $$
$self->_dbh $self->_pid != $$
Truth table0%0/3
A and B
A= $self->_dbhB= $self->_pid != $$
ABresult
0X0
100
111
302 0 $self->_dbh->{InactiveDestroy} = 1;
303 0 $self->_teardown;
304 # the parent's pending lookups: their callbacks would answer its clients
305 0 $self->_clear_inflight;
306 0 @{ $self->_queue } = ();
307 0 $self->_clear_reconnect_f;
308 0 $self->_clear_connecting_f;
309 0 $self->_backoff(0);
310 0 $self->_next_attempt(0);
311 0 $self->invalidate('disconnect');
312 0 return;
313 }
314  
315 0 ▶sub _connect {
Subroutine _connect: not called CC=4 SCAR=30.0
316 0 my ($self) = @_;
317 0 my $password = $self->_password;
318 0 my $dbh;
319 0 my $ok = eval {
320 0 $dbh = DBI->connect( $self->_dsn, $self->user, $password, {
321 RaiseError => 1,
322 PrintError => 0,
323 PrintWarn => 0,
324 AutoCommit => 1,
325 AutoInactiveDestroy => 1,
326 });
327 0 $dbh->do( 'LISTEN '.$dbh->quote_identifier( $self->listen ) );
328 0 1;
329 };
330 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
331 0 $self->_log_db_error( 'connect failed', $@, $password );
332 0 ▶ eval { $dbh->disconnect } if $dbh;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh 0 0 0
333 0 $self->_retry_later;
334 0 return;
335 }
336 0 $self->_dbh($dbh);
337 0 $self->_pid($$);
338 0 $self->_backoff(0);
339 0 $self->_next_attempt(0);
340 0 $self->_sql( $self->_build_sql($dbh) );
341 0 ▶ $self->_watch_socket if $self->_use_loop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_use_loop 0 0 0
342 0 $log->infof( 'token store: connected, listening on %s', $self->listen );
343 0 return $dbh;
344 }
345  
346 0 ▶sub _build_sql {
Subroutine _build_sql: not called CC=3 SCAR=24.8
347 0 my ($self, $dbh) = @_;
348 0 ▶ my ($schema, $name) = $self->relation =~ /\A([^.]+)\.(.+)\z/
Branch: 1 branch
BranchTrueFalseTotal
$self->relation =~ /\A([^.]+)\.(.+)\z/ ? : 0 0 0
349 ? ( $1, $2 ) : ( undef, $self->relation );
350 0 ▶ my $relation = defined $schema
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 0 0 0
351 ? $dbh->quote_identifier( undef, $schema, $name )
352 : $dbh->quote_identifier($name);
353 0 my %c = map { $_ => $dbh->quote_identifier( $self->_column($_) ) } keys %DEFAULT_COLUMNS;
354 return 'SELECT '.$c{model}.', '.$c{provider}.', extract(epoch from '.$c{expires}.')'
355 .' FROM '.$relation
356 0 .' WHERE '.$c{hash}.' = ? AND '.$c{expires}.' > now()';
357 }
358  
359 0 ▶sub _watch_socket {
Subroutine _watch_socket: not called CC=2 SCAR=17.9
360 0 my ($self) = @_;
361 0 weaken( my $weak = $self );
362 # A dup of libpq's socket: the watch closes its own descriptor, never
363 # libpq's, and still sees the end of the connection.
364 open( my $fh, '<&', $self->_dbh->{pg_socket} )
365 0 ▶ or return $self->_lost('cannot watch the connection');
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<&", $self->_dbh->{"pg_socket"} or return $self->_lost("cannot watch the connection") 0 0 0
366 my $watch = IO::Async::Handle->new(
367 read_handle => $fh,
368 0 ▶ on_read_ready => sub { $weak->_pump if $weak },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
369 0 );
370 my $ticker = IO::Async::Timer::Periodic->new(
371 interval => 1,
372 0 ▶ on_tick => sub { $weak->_tick if $weak },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
373 0 );
374 0 $self->loop->add($watch);
375 0 $self->loop->add($ticker);
376 0 $self->_watch($watch);
377 0 $self->_ticker($ticker);
378 0 return;
379 }
380  
381 1 ▶sub _teardown {
Subroutine _teardown: called CC=7 SCAR=31.8
382 1 my ($self) = @_;
383 1 ▶ if ( my $watch = $self->_watch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $watch = $self->_watch) 0 1 1
384 0 eval { $watch->close };
385 0 $self->_clear_watch;
386 }
387 1 ▶ if ( my $ticker = $self->_ticker ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ticker = $self->_ticker) 0 1 1
388 0 ▶ eval { $ticker->stop if $ticker->is_running; $ticker->loop->remove($ticker) if $ticker->loop };
Branch: 2 branches
BranchTrueFalseTotal
if $ticker->is_running 0 0 0
if $ticker->loop 0 0 0
389 0 $self->_clear_ticker;
390 }
391 1 ▶ if ( my $dbh = $self->_dbh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = $self->_dbh) 0 1 1
392 0 ▶ $self->_pid == $$ ? eval { $dbh->disconnect } : ( $dbh->{InactiveDestroy} = 1 );
Branch: 1 branch
BranchTrueFalseTotal
$self->_pid == $$ ? : 0 0 0
393 0 $self->_clear_dbh;
394 }
395 1 return;
396 }
397  
398 0 ▶sub _retry_later {
Subroutine _retry_later: not called CC=4 SCAR=30.0
399 0 my ($self) = @_;
400 0 ▶ my $delay = $self->_backoff ? $self->_backoff * 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_backoff ? : 0 0 0
401 0 ▶ $delay = 30 if $delay > 30;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 30 0 0 0
402 0 $self->_backoff($delay);
403 0 $self->_next_attempt( time + $delay );
404 0 ▶ return unless $self->_use_loop;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_use_loop 0 0 0
405 0 weaken( my $weak = $self );
406 0 ▶ $self->_reconnect_f( $self->loop->delay_future( after => $delay )->on_done( sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
407 0 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 0 0
408 0 $weak->_clear_reconnect_f;
409 0 ▶ $weak->_start_connect unless $weak->_dbh || $weak->_connecting_f;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak->_dbh or $weak->_connecting_f 0 0 0
Condition0%0/3
$weak->_dbh or $weak->_connecting_f
l!l&&r!l&&!r
000
MC/DC0%0/2
$weak->_dbh or $weak->_connecting_f
$weak->_dbh $weak->_connecting_f
Truth table0%0/3
A or B
A= $weak->_dbhB= $weak->_connecting_f
ABresult
1X1
011
000
410 0 }));
411 0 $log->infof( 'token store: connecting again in %ds', $delay );
412 0 return;
413 }
414  
415 #### Connection loss
416  
417 0 ▶sub _lost {
Subroutine _lost: not called CC=2 SCAR=17.9
418 0 my ($self, $why) = @_;
419 0 $self->_teardown;
420 0 $log->warnf( 'token store: %s, refusing tokens until it is back', $why );
421 0 $self->invalidate('disconnect');
422 0 $self->_fail_pending;
423 0 ▶ $self->_retry_later unless $self->_reconnect_f;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_reconnect_f 0 0 0
424 0 return;
425 }
426  
427 0 ▶sub _fail_pending {
Subroutine _fail_pending: not called CC=3 SCAR=24.8
428 0 my ($self) = @_;
429 0 my @q = grep { defined } $self->_inflight, @{ $self->_queue };
430 0 $self->_clear_inflight;
431 0 @{ $self->_queue } = ();
432 0 for my $f ( map { @{ $_->{futures} } } @q ) {
433 0 ▶ $f->fail( $UNAVAILABLE, 'token_store' ) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
434 }
435 0 return;
436 }
437  
438 #### Queries
439  
440 0 ▶sub _send_next {
Subroutine _send_next: not called CC=8 SCAR=42.8
441 0 my ($self) = @_;
442 0 ▶ return if $self->_inflight || !$self->_dbh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_inflight or not $self->_dbh 0 0 0
Condition0%0/3
$self->_inflight or not $self->_dbh
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_inflight or not $self->_dbh
$self->_inflight not $self->_dbh
Truth table0%0/3
A or not(B)
A= $self->_inflightB= not $self->_dbh
ABresult
1X1
011
000
443 0 ▶ my $q = shift @{ $self->_queue } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $q = shift @{$self->_queue;} or return 0 0 0
444 0 my $dbh = $self->_dbh;
445 0 my $ok = eval {
446 0 $q->{sth} = $dbh->prepare( $self->_sql, { pg_async => DBD::Pg::PG_ASYNC() } );
447 0 $q->{sth}->execute( $q->{hash} );
448 0 1;
449 };
450 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
451 0 my $error = $@;
452 0 $self->_log_db_error( 'lookup failed', $error );
453 0 ▶ return $self->_lost('lookup failed on a closed connection')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_connection_ok 0 0 0
454 unless $self->_connection_ok;
455 0 $q->{failed} = 1;
456 0 $self->_settle($q);
457 0 return $self->_send_next;
458 }
459 0 $q->{sent} = time;
460 0 $self->_inflight($q);
461 0 ▶ $self->_ticker->start if $self->_ticker && !$self->_ticker->is_running;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ticker and not $self->_ticker->is_running 0 0 0
Condition0%0/3
$self->_ticker and not $self->_ticker->is_running
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_ticker and not $self->_ticker->is_running
$self->_ticker not $self->_ticker->is_running
Truth table0%0/3
A and not(B)
A= $self->_tickerB= not $self->_ticker->is_running
ABresult
0X0
100
111
462 0 return;
463 }
464  
465 # Collects what the socket brought: notifications, the running query's
466 # result. Every libpq call here reads from the socket, so a pass may leave
467 # input behind for the other call; passes repeat while they find something
468 # or the socket stays readable (at its end it stays readable, and the next
469 # call fails).
470 0 ▶sub _pump {
Subroutine _pump: not called CC=14 SCAR=53.5
471 0 my ($self) = @_;
472 0 ▶ my $dbh = $self->_dbh or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return 0 0 0
473 0 ▶ return $self->_forget_inherited if $self->_pid != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_pid != $$ 0 0 0
474 0 for ( 1 .. 16 ) {
475 0 my ( @notes, $done );
476 0 my $ok = eval {
477 0 ▶ while ( my $n = $dbh->pg_notifies ) { push @notes, $n }
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $dbh->pg_notifies) 0 0 0
478 0 ▶ if ( $self->_inflight && $dbh->pg_ready > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_inflight and $dbh->pg_ready > 0) 0 0 0
Condition0%0/3
$self->_inflight and $dbh->pg_ready > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_inflight and $dbh->pg_ready > 0
$self->_inflight $dbh->pg_ready > 0
Truth table0%0/3
A and B
A= $self->_inflightB= $dbh->pg_ready > 0
ABresult
0X0
100
111
479 0 $done = $self->_inflight;
480 0 $self->_clear_inflight;
481 0 $self->_collect($done);
482 }
483 0 1;
484 };
485 # delivered outside the eval: a listener's failure is not the connection's
486 0 $self->_notified($_) for @notes;
487 0 ▶ $self->_settle($done) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
488 0 ▶ return $self->_lost('connection closed') unless $ok && $self->_connection_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok and $self->_connection_ok 0 0 0
Condition0%0/3
$ok and $self->_connection_ok
!ll&&!rl&&r
000
MC/DC0%0/2
$ok and $self->_connection_ok
$ok $self->_connection_ok
Truth table0%0/3
A and B
A= $okB= $self->_connection_ok
ABresult
0X0
100
111
489 0 ▶ last unless @notes || $done || $self->_readable_now;
Branch: 1 branch
BranchTrueFalseTotal
unless @notes or $done or $self->_readable_now 0 0 0
Condition0%0/6
@notes or $done or $self->_readable_now
l!l&&r!l&&!r
000
@notes or $done
l!l&&r!l&&!r
000
MC/DC0%0/3
@notes or $done or $self->_readable_now
@notes $done $self->_readable_now
Truth table0%0/4
A or B or C
A= @notesB= $doneC= $self->_readable_now
ABCresult
1XX1
01X1
0011
0000
490 }
491 0 $self->_send_next;
492 0 return;
493 }
494  
495 0 ▶sub _readable_now {
Subroutine _readable_now: not called CC=5 SCAR=34.0
496 0 my ($self) = @_;
497 0 ▶ my $dbh = $self->_dbh or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return 0 0 0 0
498 0 my $fd = $dbh->{pg_socket};
499 0 ▶ return 0 unless defined $fd && $fd >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd and $fd >= 0 0 0 0
Condition0%0/3
defined $fd and $fd >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fd and $fd >= 0
defined $fd $fd >= 0
Truth table0%0/3
A and B
A= defined $fdB= $fd >= 0
ABresult
0X0
100
111
500 0 my $rin = '';
501 0 vec( $rin, $fd, 1 ) = 1;
502 0 ▶ return select( my $rout = $rin, undef, undef, 0 ) > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
select(my $rout = $rin, undef, undef, 0) > 0 ? : 0 0 0
503 }
504  
505 0 ▶sub _tick {
Subroutine _tick: not called CC=6 SCAR=37.4
506 0 my ($self) = @_;
507 0 my $q = $self->_inflight;
508 0 ▶ unless ($q) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($q) 0 0 0
509 0 ▶ $self->_ticker->stop if $self->_ticker && $self->_ticker->is_running;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ticker and $self->_ticker->is_running 0 0 0
Condition0%0/3
$self->_ticker and $self->_ticker->is_running
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_ticker and $self->_ticker->is_running
$self->_ticker $self->_ticker->is_running
Truth table0%0/3
A and B
A= $self->_tickerB= $self->_ticker->is_running
ABresult
0X0
100
111
510 0 return;
511 }
512 return $self->_lost('lookup timed out')
513 0 ▶ if $self->query_timeout > 0 && time - $q->{sent} > $self->query_timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $self->query_timeout > 0 and time - $q->{"sent"} > $self->query_timeout 0 0 0
Condition0%0/3
$self->query_timeout > 0 and time - $q->{"sent"} > $self->query_timeout
!ll&&!rl&&r
000
MC/DC0%0/2
$self->query_timeout > 0 and time - $q->{"sent"} > $self->query_timeout
$self->query_timeout > 0 time - $q->{"sent"} > $self->query_timeout
Truth table0%0/3
A and B
A= $self->query_timeout > 0B= time - $q->{"sent"} > $self->query_timeout
ABresult
0X0
100
111
514 0 $self->_pump;
515 0 return;
516 }
517  
518 0 ▶sub _collect {
Subroutine _collect: not called CC=3 SCAR=24.8
519 0 my ($self, $q) = @_;
520 0 my $r;
521 0 my $ok = eval {
522 0 $q->{sth}->pg_result;
523 0 $r = $q->{sth}->fetchrow_arrayref;
524 0 ▶ $r = [ @$r ] if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
525 0 $q->{sth}->finish;
526 0 1;
527 };
528 0 ▶ if ($ok) { $q->{row} = $self->_row($r) }
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 0 0
529 else {
530 0 $self->_log_db_error( 'lookup failed', $@ );
531 0 $q->{failed} = 1;
532 }
533 0 delete $q->{sth};
534 0 return;
535 }
536  
537 0 ▶sub _settle {
Subroutine _settle: not called CC=4 SCAR=30.0
538 0 my ($self, $q) = @_;
539 0 for my $f ( @{ $q->{futures} } ) {
540 0 ▶ next if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 0 0
541 0 ▶ $q->{failed} ? $f->fail( $UNAVAILABLE, 'token_store' ) : $f->done( $q->{row} );
Branch: 1 branch
BranchTrueFalseTotal
$q->{'failed'} ? : 0 0 0
542 }
543 0 return;
544 }
545  
546 0 ▶sub _notified {
Subroutine _notified: not called CC=3 SCAR=24.8
547 0 my ($self, $n) = @_;
548 0 ▶ return unless defined $n->[0] && $n->[0] eq $self->listen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n->[0] and $n->[0] eq $self->listen 0 0 0
Condition0%0/3
defined $n->[0] and $n->[0] eq $self->listen
!ll&&!rl&&r
000
MC/DC0%0/2
defined $n->[0] and $n->[0] eq $self->listen
defined $n->[0] $n->[0] eq $self->listen
Truth table0%0/3
A and B
A= defined $n->[0]B= $n->[0] eq $self->listen
ABresult
0X0
100
111
549 0 $log->debug('token store: notification, invalidating');
550 0 $self->invalidate('notify');
551 0 return;
552 }
553  
554 # DBI errors name host, port and user; the password and token hashes must
555 # not reach even the log.
556 0 ▶sub _log_db_error {
Subroutine _log_db_error: not called CC=6 SCAR=37.4
557 0 my ($self, $what, $error, $password) = @_;
558 0 ▶ my $text = defined $error ? "$error" : 'unknown error';
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 0 0 0
559 0 ▶ $text = ( split /\n/, $text )[0] // '';
Condition0%0/2
(split(/\n/, $text, 0))[0] // ""
l!l
00
MC/DC0%0/1
(split(/\n/, $text, 0))[0] // ""
(split(/\n/, $text, 0))[0]
Truth table0%0/2
A
A= (split(/\n/, $text, 0))[0]
Aresult
11
00
560 0 $text =~ s/ at \S+ line \d+\.?\z//;
561 0 ▶ $password = $self->_password_quiet unless defined $password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password 0 0 0
562 0 ▶ $text =~ s/\Q$password\E/***/g if defined $password && length $password;
Branch: 1 branch
BranchTrueFalseTotal
if defined $password and length $password 0 0 0
Condition0%0/3
defined $password and length $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $password and length $password
defined $password length $password
Truth table0%0/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
563 0 $text =~ s/\b[0-9a-fA-F]{64}\b/<hash>/g;
564 0 $log->errorf( 'token store: %s: %s', $what, $text );
565 0 return;
566 }
567  
568 0 ▶sub _password_quiet {
Subroutine _password_quiet: not called CC=3 SCAR=24.8
569 0 my ($self) = @_;
570 0 my $env = $self->password_env;
571 0 ▶ return defined $env && length $env ? $ENV{$env} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $env && length $env ? : 0 0 0
Condition0%0/3
defined $env and length $env
!ll&&!rl&&r
000
MC/DC0%0/2
defined $env and length $env
defined $env length $env
Truth table0%0/3
A and B
A= defined $envB= length $env
ABresult
0X0
100
111
572 }
573  
574 1 __PACKAGE__->meta->make_immutable;
575 1 1;
576  
577 __END__
« blib/lib/Langertha/Knarr/Admin.pm ↑ Summary blib/lib/Langertha/Knarr/Handler/ACPClient.pm »